Hi, I am struggling trying to figure out how to code something I otherwise thought would have been quite simple. I have a button connected to Pin 2 of the Arduino
I am trying to program it so that the button is pressed twice within 500 milliseconds an LED will light for one second and then the code will reset back to how it was before the button was pressed.
However, I cannot figure out how to do this. It doesn't appear to be as simple as modifying the code from pushing the button to make the led light while it is pressed.
Keep a counter of how many times the button has been pressed
Record a time using the millis() function of when the button was last pressed
Compare this value against the current time to see if it is a second press or not
When you detect the button state going from "not pressed" to "pressed"...
IF the current_time - last_pressed_time < 50
ignore then button press (see debounce)
ELSE IF the current_time - last_pressed_time < 500
increment button_counter
record the last_pressed_time
ELSE
set the button_counter = 1
record the last_pressed_time
In loop...
IF the current_time - last_pressed_time >= 500 && button_counter > 0
handle the number of button presses indicated by button_counter
set button_counter = 0