Two leds with one button and "delay"


I have some code and I am stuck, so if you can help me Idea is to have one button and two leds with arduino,
one push button first led on and off in 2 sec,
second push button second led on and off in two second,

I can make to turn it on but delay of 2 sec I did not manage
here is code, I try with delay function not working, and millis function try but no result

const int buttonPin = 3; // the number of the pushbutton pin

const int Pin1 = 13; // the number of the relay or led
const int Pin2 = 11;

// variables will change:
int initial = 0; //hold current initial
int ostate = 0; //hold last initial
int buttonstate = 0; // variable for reading the pushbutton status

void setup() {
pinMode(Pin1, OUTPUT); // initialize the LED pin as an output:
pinMode(PIN2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:
void loop(){
//debouncing routline to read button
buttonstate = digitalRead(buttonPin); //state the initial of button
if(buttonstate == HIGH){ //check if it has been pressed
buttonstate = digitalRead(buttonPin);//state button again
if(buttonstate == LOW){ //if it is 0 considered one press
initial = ostate + 1; //increase initial by 1
}else{ //check if it has been NOT pressed
switch (initial){ //react to button press a initial
case 1: //if initial is 1
digitalWrite(Pin1, HIGH);//on
digitalWrite(Pin2, LOW);//off

   ostate =  initial;               //set oldstate  initial as current  initial
 case 2:
   digitalWrite(Pin1, LOW);
   digitalWrite(Pin2, HIGH);
   ostate =  0;


a small Finite-State-Machine will help you. There are tons of on line tutorial on how to go for this, just search for “arduino Finite-State-Machine tutorial”

My standard recipe:
As a basis, study the IOP model and then take a bag of data structures, timers and a small finite state machine and stir everything together to the desired functionality. And don’t forget the seasoning.

1 Like

your requirements do not describe what happens if you press the button when one of the LED is going through its cycle.

It is simple one button press state1 LED1 goes on, off in 2 sec, other button press state2 LED2 goes on, off in two sec
I gues some delay is needed also if button press appear in led cycle

I found example with millis function - Finite-State-Machine for single time delay, I am trying to put that in place in code where is case1 and case2

const int switchPin = 3;
const int ledPin = LED_BUILTIN;

unsigned long previousMillis;
const unsigned long interval = 2500;
bool enabled = false;

void setup()
pinMode( switchPin, INPUT_PULLUP);
pinMode( ledPin, OUTPUT);

void loop()
unsigned long currentMillis = millis();

if( digitalRead( switchPin) == LOW) // low is active
digitalWrite( ledPin, HIGH); // turn on led
previousMillis = currentMillis;
enabled = true; // turn on software timer

if( enabled) // software timer is active ?
if( currentMillis - previousMillis >= interval)
digitalWrite( ledPin, LOW); // turn off led
enabled = false; // stop software timer

that’s a good start, but you need a variable also to remember if it’s a first press or a second press

you also need to decide if the second press arrives during the time the first LED is on, do you activate the second one as well for 2 s or you have to wait until the first one is off?

You see in first code up Initial variable gives me 1 or 2 value I checked with serial monitor so that is the in loop only 1 and 2, now I am trying to get led off after specific time, then I will see for cycle

The Meal is ready :slight_smile:
This sketch is tested. You have to change the pin address for your application simply.
I have all information and data organized in a struct{} to have an easy access to this inside the sketch.
Don´t hesitate if you have further questions.

const byte Button1 {A0};
const byte Led1 {2};
const byte Led2 {3};
enum {One,Two};

struct LEDBUTTON {
  int ledPin[2];
  int buttonPin;
  bool state;
  int counter;
  unsigned long timeStamp[2];
  unsigned long duration[2];
} ledButton {{Led1,Led2},Button1 ,1, 0,{0,0},{2500, 2500}};

void debounce() {
  static unsigned long debounceMillis;
  if (millis() - debounceMillis >= 20) {
    bool state=digitalRead(ledButton.buttonPin);
    if (ledButton.state!= state) {
      ledButton.state= state;
      if (!ledButton.state) {
void ledButtonTimer() {
  for (int num=0; num<2 ; num++) {
    if (millis()- ledButton.timeStamp[num] > ledButton.duration[num] && ledButton.timeStamp[num]) {
void setup() {
  // put your setup code here, to run once:
    pinMode (ledButton.ledPin[One],OUTPUT);
    pinMode (ledButton.ledPin[Two],OUTPUT);
    pinMode (ledButton.buttonPin, INPUT_PULLUP);
void loop() {
  // put your main code here, to run repeatedly:
1 Like

Ok, this looks good I need to study it there is some functions I did not use previously, thank you for effort and support

I manage to make working code in my version also just need test

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