Need to activate a State after holding button for 2 seconds

Hello.
I have 5 LEDs that need to loop through 6 different states of flashing/holding.
I had written the code originally using Delay(); instead of millis();
Now I need to have the LED's swap into a 7th state if a button is held down for more than 2 seconds.
I'm really struggling to get my head around how to use the millis() function to do this.
I have been reading documentation and other peoples projects / code to try and figure it out and get what I want.. To no avail for about 3 days straight now.
I'm about to rip my hair out.. Please help.

Review State Machine Programming.

What are you not understanding re: millis( ) delays ?


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

1 Like

Save the value of millis() when the button becomes pressed, then next time you check its state you can determine whether 2 seconds has elapsed

1 Like

use a button library, for example OneButton from Matthias Hertel.

1 Like

The key is to rephrase, "the button has been held down for two seconds" as "the last time the button was released was two seconds ago or longer". That's easier to code. As in reply #3.

1 Like

Hello
Did we see your sketch, didn´t we?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.