Led doesn't work after 3 sec while button pressed

Hello everyone,
I have a problem with my program code i think.
My goals are to turn 1 led on if i press that button and if i press the same button again or the other button, then goes my led off and so one with the other led.
Now the problem is it doesn’t work anymore and also i’ve been trying to set my led on if i pressed my button for 3 seconds. The following components i use are: Arduino Uno rev3, 2x 220 ohm resistors, 3 red led’s, 2 buttons. Can you help me with this problem?
NOTE : I’m still just a beginner, however i have some specific things that i want
- I want to use a timer and NOT a delay
- Can you explain me each code that you wrote, in case if you found my problem?
- Also, there are code that i put in comment.

Thank you,

led_test.zip (1.26 KB)

Can you please read How to use the forum and format the post accordingly? Then I’ll inline the image :slight_smile:

IMG_5642 (1).JPG

PS, a schematic would be more useful (which is NOT a Fritzing breadboard view :wink: ). Pen and paper will do.

You should not use delay() in an ISR. The ISR has to be finished in less than a few hundred microseconds (less than half a millisecond!).

You should almost never use interrupts for switch/button inputs.

It sounds like you want either both lights off or one light on. That gives you three states. I would call the three states Stopped, Up, and Down.

In loop() check to see if either button has just been pressed. See examples.

If the state is Stopped:
If the Up button has just been pressed, turn on the Up light and set the state to Up.
else
If the Down button has just been pressed, turn on the Down light and set the state to Down.

If the state is Up:
If the Up button has just been pressed, turn off the Up light and set the state to Stopped.
else
If the Down button has just been pressed, turn off the Up light, turn on the Down light and set the state to Down.

If the state is Down:
If the Down button has just been pressed, turn off the Down light and set the state to Stopped.
else
If the Up button has just been pressed, turn off the Down light, turn on the Up light and set the state to Up.

johnwasser:
You should not use delay() in an ISR. The ISR has to be finished in less than a few hundred microseconds (less than half a millisecond!).

You should almost never use interrupts for switch/button inputs.

It sounds like you want either both lights off or one light on. That gives you three states. I would call the three states Stopped, Up, and Down.

In loop() check to see if either button has just been pressed. See examples.

If the state is Stopped:
If the Up button has just been pressed, turn on the Up light and set the state to Up.
else
If the Down button has just been pressed, turn on the Down light and set the state to Down.

If the state is Up:
If the Up button has just been pressed, turn off the Up light and set the state to Stopped.
else
If the Down button has just been pressed, turn off the Up light, turn on the Down light and set the state to Down.

If the state is Down:
If the Down button has just been pressed, turn off the Down light and set the state to Stopped.
else
If the Up button has just been pressed, turn off the Down light, turn on the Up light and set the state to Up.

Dear Johnwasser,

Your explanation solved my problem.
Thank you very much!

johnwasser:
You should not use delay() in an ISR. The ISR has to be finished in less than a few hundred microseconds (less than half a millisecond!).

You should almost never use interrupts for switch/button inputs.

It sounds like you want either both lights off or one light on. That gives you three states. I would call the three states Stopped, Up, and Down.

In loop() check to see if either button has just been pressed. See examples.

If the state is Stopped:
If the Up button has just been pressed, turn on the Up light and set the state to Up.
else
If the Down button has just been pressed, turn on the Down light and set the state to Down.

If the state is Up:
If the Up button has just been pressed, turn off the Up light and set the state to Stopped.
else
If the Down button has just been pressed, turn off the Up light, turn on the Down light and set the state to Down.

If the state is Down:
If the Down button has just been pressed, turn off the Down light and set the state to Stopped.
else
If the Up button has just been pressed, turn off the Down light, turn on the Up light and set the state to Up.

Hi Johnwasser,

However, i think i still didn’t understand my problem because i tried it earlier but it still won’t work.
What should I use to read input from my buttons and limit switches(this will be implemented later), when I want the input so fast as possible and act on the change?

Interrupts are reasonable for emergency stop and hardware limit switches, but never call delay() in
an ISR, nor any Serial I/O, the system can hang forever.

KT_x3:
However, i think i still didn't understand my problem because i tried it earlier but it still won't work.
What should I use to read input from my buttons and limit switches(this will be implemented later), when I want the input so fast as possible and act on the change?

If you don't put delays in your sketch the loop() should run hundreds or thousands of times per second. Reading the inputs at the top of loop() should be sufficient.