2

I have been using https://wokwi.com/ to learn Arduino and I am having a problem that seems to be happening no matter what I do. I am trying to set up a push button and an LED. The end goal here is to set it up so that when I tap the push button down and release it that the LED blinks like a turn signal in a car. When I push it down and release it again, the LED should stop blinking. I have tried tons of different code, even using code copied and pasted directly from Arduino's website. What ends up happening is that the button is unreliable. Sometimes the button will click, other times it won't register. It is very bizarre behavior. I read about the bouncing effect but any code I put in to ignore bouncing doesn't seem to have any effect on the system. Why are push buttons so unreliable? I know in real life they aren't because I am able to type this using presumably push buttons on my keyboard. What am I doing wrong??

Here is an example. The push button is totally unpredictable. Why is it behaving like this?

enter image description here

int state = 0;

void setup()
{
  pinMode(9, OUTPUT);
  pinMode(6, INPUT);
}

void loop()
{
  if (digitalRead(6)) {
    state = !state;
  }
  digitalWrite(9, state);
}
7
  • the problem is on line 20
    – jsotola
    Commented Jul 19, 2023 at 15:15
  • I am counting 15 lines
    – wgm
    Commented Jul 19, 2023 at 15:33
  • 2
    no other guess was possible without seeing the code
    – jsotola
    Commented Jul 19, 2023 at 15:53
  • you have a logic error in your code ... the code is similar to if it's dark outside, then flip the light switch ... you'd be turning the lights on and off all night long, and the light stands a chance to be left on all day
    – jsotola
    Commented Jul 19, 2023 at 16:47
  • if (!digitalRead(6)) { state = !state; } Is that correct?
    – wgm
    Commented Jul 19, 2023 at 18:53

2 Answers 2

5

You've got the button wired wrong. Your resistor is inline with the signal going to the pin. It should be going from the pin to 5V. The button should be wired directly to the pin and ground. Or you can use INPUT_PULLUP in pinMode instead of INPUT and eliminate the resistor entirely.

Your code should test for the button pin being LOW, not HIGH. When you press the button it will read LOW. When unpressed it will read HIGH. This is the normal way that buttons are wired up.

Also note that your loop() function will repeat thousands of times per second. As long as you still have your finger on the button it will keep cycling the state. That means that every time you press the button, the state gets cycled a few tens of thousands of times and there's no real way of knowing which state it will be on when you finally get your finger off the button.

You should look at the "State Change Example" in the Arduino IDE. It shows how to keep track of the previous state of the button so you can react once to each press.

4
  • Do you know what I can Google to find that State Change Example? Having trouble finding it. Thanks for the explanation
    – wgm
    Commented Jul 19, 2023 at 17:59
  • I think Delta_G means this one: docs.arduino.cc/built-in-examples/digital/StateChangeDetection
    – chrisl
    Commented Jul 19, 2023 at 19:14
  • @wgm, open the Arduino IDE. Go to the File menu. File->Examples->Digital. State Change Detection will be towards the middle. If you google "Arduino State Change Example" you will be flooded with information and tutorials. Surely one of them is your speed.
    – Delta_G
    Commented Jul 19, 2023 at 20:19
  • Do you know what I can Google to find that State Change Example - see my website: gammon.com.au/switches - it also talks about transitions
    – Nick Gammon
    Commented Jul 20, 2023 at 7:22
1

This is my first answer on this platform so pardon my mistakes :P

I too faced this problem. I don't know if my answer will be still helpful but I did find the solution to this problem. Please refer to how I have plugged in my buttons -

Buttons Wiring Diagram

Ignore everything other than the buttons. They are not relevant to this question. All black wires are going to gnd pin.

Code for the buttons -

#define btnPinYellow 6
#define btnPinGreen 7
#define btnPinBlue 8
#define btnPinRed 9
#define btnPinBlack 10

void setup() {
 pinMode(btnPinYellow, INPUT_PULLUP);
 pinMode(btnPinGreen, INPUT_PULLUP);
 pinMode(btnPinBlue, INPUT_PULLUP);
 pinMode(btnPinRed, INPUT_PULLUP);
 pinMode(btnPinBlack, INPUT_PULLUP);
 Serial.begin(9600);
}

By defining the button input pin to INPUT_PULLUP, the pin is now in an active HIGH state. When we press the button, the button will go to LOW state. This should also remove the button's erratic behaviour which you were encountering. You can test this using the serial monitor.

Hope this solves your problem ^_^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.