I want to do LED toggeling so I wrote this:

int led=13, button=12;

void setup()
    // put your setup code here, to run once:

void loop()
    // put your main code here, to run repeatedly:
    if(digitalRead(button)==HIGH && digitalRead(led)==LOW)

    if(digitalRead(button)==HIGH && digitalRead(led)==HIGH)

But It doesn't work well, sometimes I need to press the button many times in order to turn on or of...and some times with just one press.

I saw this video but I didn't understand it well: https://www.youtube.com/watch?v=aMato4olzi8&t=415s

Anyway I don't know why my way doesn't work well.

  • One word: bounce. – Majenko Apr 29 at 17:24
  • I want to do LED toggeling ... you did not explain how you want the program to function – jsotola Apr 29 at 18:56
  • you fail to realize that loop() repeats 10000 times per second .... you are checking if the button is being pressed, which happens over and over .... you need to wait for the transition between "not pressed" and "pressed" .... that happens only once when you press the button – jsotola Apr 29 at 19:08
  • nice...that helped...thnx – sumer fattoum May 2 at 0:27
  • yes right so at the second when button is pressed it tries to make LED on and off the same second – sumer fattoum May 3 at 15:16

There are a few problems with your approach.

  1. You seem to have the button backwards. You should wire a button so that it is between the pin and ground. This way it should read LOW when pressed, not HIGH. It should also have pinMode(pin, INPUT_PULLUP) instead of INPUT. With the button wired the way you have it to read HIGH then you will need an extra resistor for each button. With it wired the right way where it reads LOW then it needs nothing extra.

  2. If the first if statement is true and the button is pressed and it turns on the led, what do you think are the chances that you are fast enough to get your finger off the button in the 62.5 nanoseconds it takes to get to that next if statement? It's not going to happen, so the button is still pressed there and now the led is on so this line turns it right back off. Only in the rare case where you happen to get your finger off the button at just the right instant will the led stay lit. To solve this, look at the "State Change Example" that comes with the IDE. It shows how to keep track of the last state of the button in a variable so you can act only once when the button actually changes state.

  3. Once you get that worked out you will notice that it seems like for every time you press your button it registers tens of presses. This is switch bounce. That's a term you should google. The contacts are making and breaking contact really fast in the in-between pushed and not-pushed state. But you need to get the first two issues solved before you worry about bounce.

| improve this answer | |
  • nice...that helped...thnx – sumer fattoum May 2 at 0:27
  • I checked the state change example, but I didnt get why the delay for bounce is used in that specific place ? why to set lastButtonState = buttonState; since button state will anyway return to 0 when button is released , any lastButtonState was 0 anyway..so there is no change in it? – sumer fattoum May 3 at 23:06
  • You do that so you can tell when the state changed from on loop to the next. – Delta_G May 4 at 0:01
  • So the little 50ms delay is there to catch switch bounce. Do you know what switch bounce is? When you press a button there is a small period of time where the contacts are almost made and they sort of make and then break and then make and break over and over as they're coming closer together and almost touching. Then once they touch all the way closed they're closed. Same thing happens as the switch just starts to open. So you put a little delay there so that you miss all those rapid changes. – Delta_G May 4 at 0:46
  • You are right that buttonState will have a 0 when released, but it will have a 1 when pressed. So the button is not pressed and buttonState and lastButtonState are both 0. Then you press down the button. When the button is read now it's HIGH, but lastButtonState is still LOW, now we've detected a change of state. So we can take some action. The next time through the button still reads HIGH, but lastButtonState is now also HIGH because we set it to match buttonState at the end of loop. So nothing happens since they are the same. – Delta_G May 4 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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