I have been trying to find a way to use millis() instead of delay(), in order to press 2 buttons to turn on and off a blue led.


const int right_button = 8, left_button = 9, yellow_led = 7, blue_led = 6, green_led = 5, red_led = 4;
int previous_left_button_status = LOW, previous_right_button_status = LOW, blue_status = LOW, green_status = LOW;
unsigned long time_setup = millis();
const int delay_task = 250;

void setup()
  pinMode(right_button, INPUT_PULLUP);
  pinMode(left_button, INPUT_PULLUP);
  pinMode(blue_led, OUTPUT);
  pinMode(green_led, OUTPUT);
  pinMode(red_led, OUTPUT);
  pinMode(yellow_led, OUTPUT);

void loop()
  int right_button_status = digitalRead(right_button);
  int left_button_status = digitalRead(left_button);
  unsigned long time_passes = millis();
  if (time_passes - time_setup >= delay_task)
    time_setup = time_passes;
    if (right_button_status == previous_right_button_status)
      green_status = !green_status;
      digitalWrite(green_led, green_status);
  • start with the arduino debounce example sketch ..arduino.cc/en/Tutorial/Debounce
    – jsotola
    Commented Apr 7, 2020 at 14:22
  • 1
    this code does not work is a very poor description of the program behavior ... what actually happens when you run the sketch?
    – jsotola
    Commented Apr 7, 2020 at 14:26
  • @jstola, my bad for not being specific ;/ my problem here is with both buttons, right/left_button_press are not working as I want them to turn on and off a LED using only delay Commented Apr 7, 2020 at 14:30
  • you have logic errors in your code .. . using the else keyword creates code that excludes one of the buttons if the other button is pressed ... each button should have a separate if statement
    – jsotola
    Commented Apr 7, 2020 at 14:35
  • @jstola, so "else if" here means exactly the same as "else", if a condition is met then do this, and if not(else) do the other one but do not execute the first condition, correct? Commented Apr 7, 2020 at 14:41

1 Answer 1


You've declared the variable for the millis function inside your loop correct and you've declared the delay duration you want to use. Two things you've missed are you've declared 2 variables with the millis function and to use it to control the program flow based on some condition to execute some code, in your case, to press 2 buttons to turn on and off an LED.

The way to to this is to use an if-else statement inside the loop function with one variable declared with the millis function, delay_duration as your reference, and 1 variable declared with a zero value.

unsigned long storeMillis = 0;
const int delay_duration = 500;

void setup() {

void loop() {
unsigned long currentMillis = millis();
if (currentMillis - storeMillis > 500) {
storeMillis = currentMillis; //update and store the value of storeMillis for the next iteration
//code you want to do
  • Thank you for your answer, I did somewhat understand your millis(). First, you have an unsigned long which has not been used, you used firstMillis, I think you meant storeMillis in that if condition, correct? Second, how do I use that in a button debounce, I still do not understand the button debounce part using millis() Commented Apr 7, 2020 at 16:33
  • I suggest that you first change the variables that you used in storing the states of the buttons from true/false to high/low. You've set your variable "left_state" as a boolean wherein you gave it a pin level of "LOW" which is a big no no. Remember that boolean is used in defining logical levels to represent a truth or not while HIGH/LOW is used to represent pin levels in Arduino.
    – pfhermosa
    Commented Apr 7, 2020 at 17:32
  • thanks for your explanation, do you mind taking a look at my edited code? millis() is not working in my EDITED part, I still do not know why my right button is not debouncing (going with the flow, everytime I press it) it misses, waits or not turn a led ON Commented Apr 7, 2020 at 22:39
  • Great, now you need to carefully check if you're comparing the right variables in your condition. Remember that the millis() function starts keeping track of time where you declared it in your code. The variable "time_setup"' is the one that will start counting, say 0 to 5000ms, until the loop function starts. Then that will be the time your variable "time_passes" will start counting, therefore the time value of it must be lesser than the time value of "time_setup".
    – pfhermosa
    Commented Apr 8, 2020 at 18:26
  • I suggest that you print these values in the serial monitor for you to know what's actually going on and how it's storing the time in milliseconds.
    – pfhermosa
    Commented Apr 8, 2020 at 18:27

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.