Using Millis for 3 inputs

I am trying to control 3 digital inputs. First input will turn on a led and will keep it on for a certain time. Second button will do the same for a second led. Third button will turn up both leds for a second determined time.

The code that I'am using works fine for the first two inputs. When I add the third input somehow it goes wrong.

I am sure there is another way to arrange what I am trying to do but I am unable to find more examples of using millis() I've read something about using steps but not sure how.

Any suggestions ?

This is the code for two inputs and its variation to 3 imputs.

int buttonState1 = 0;
int buttonState2 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;
int inPin1 = 5;   
int inPin2 = 6;
int ledPin1 = 8;
int ledPin2 = 9;
unsigned long triggerTime1 = 0;
unsigned long triggerTime2 = 0;
unsigned long interval = 8000;


void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
}

void loop() {

  unsigned long currentTime = millis();

  buttonState1 = digitalRead(inPin1);
  buttonState2 = digitalRead(inPin2);

      if (buttonState1 != lastButtonState1){
        if (buttonState1 == HIGH){
          digitalWrite(ledPin1, HIGH);
          triggerTime1 = millis();
        }
        delay(50);
      }
      lastButtonState1 = buttonState1;
    
    
      if (buttonState2 != lastButtonState2){
          if (buttonState2 == HIGH){
            digitalWrite(ledPin2, HIGH);
            triggerTime2 = millis();
          }
          delay(50);
        }
        lastButtonState2 = buttonState2;
    
    
    
      if (currentTime - triggerTime1 >= interval){
        digitalWrite(ledPin1, LOW);
      }
      
      if (currentTime - triggerTime2 >= interval){
        digitalWrite(ledPin2, LOW);
      }




}








int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;
int lastButtonState3 = 0;
int inPin1 = 5;   
int inPin2 = 6;
int inPin3 = 7;
int ledPin1 = 8;
int ledPin2 = 9;
unsigned long triggerTime1 = 0;
unsigned long triggerTime2 = 0;
unsigned long triggerTime3 = 0;
unsigned long interval = 8000;
unsigned long interval2 = 12000;


void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
  pinMode(inPin3, INPUT);
}

void loop() {

  unsigned long currentTime = millis();

  buttonState1 = digitalRead(inPin1);
  buttonState2 = digitalRead(inPin2);

      if (buttonState1 != lastButtonState1){
        if (buttonState1 == HIGH){
          digitalWrite(ledPin1, HIGH);
          triggerTime1 = millis();
        }
        delay(50);
      }
      lastButtonState1 = buttonState1;
    
    
      if (buttonState2 != lastButtonState2){
          if (buttonState2 == HIGH){
            digitalWrite(ledPin2, HIGH);
            triggerTime2 = millis();
          }
          delay(50);
        }
        lastButtonState2 = buttonState2;

      if (buttonState3 != lastButtonState3){
          if (buttonState3 == HIGH){
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, HIGH);
            triggerTime3 = millis();
          }
          delay(50);
        }
        lastButtonState3 = buttonState3;
    
    
    
      if (currentTime - triggerTime1 >= interval){
        digitalWrite(ledPin1, LOW);
      }
      
      if (currentTime - triggerTime2 >= interval){
        digitalWrite(ledPin2, LOW);
      }

      if (currentTime - triggerTime3 >= interval2){
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        
      }


}

When I add the third input somehow it goes wrong.

You have to describe what it does and what you want it to do. The code will do exactly what you have written but we can't tel where this is wrong because we don't know your expectations.

All 3 inputs are activated by a push, program is waiting fo a HIGH to trigger the outputs. First button should activate ledPin1, and keep it ON for time1. Second button should activate ledPin2 and keep it on for the same time1. Third button should set ON both ledPin1 and ledPin2 and keep them ON for a second determined time, time2.

I posted two codes one for the first two actions, up until there the code is doing what I am expecting. Both leds can be started independently and keep on during duration of time1.

When I add the sentences for third action I mean having both led on for the duration of time2. Program is doing what I programmed but not what I expected, I mean, first and second button work fine some time, other times just flashes one third button work fine just one time when Arduino is started.

I am posting again the code when I using the third button in case I missed soemthing.

void loop() {

  unsigned long currentTime = millis();

  buttonState1 = digitalRead(inPin1);
  buttonState2 = digitalRead(inPin2);
  buttonState3 = digitalRead(inPin3);

      if (buttonState1 != lastButtonState1){
        if (buttonState1 == HIGH){
          digitalWrite(ledPin1, HIGH);
          triggerTime1 = millis();
        }
        delay(50);
      }
      lastButtonState1 = buttonState1;
    
    
      if (buttonState2 != lastButtonState2){
          if (buttonState2 == HIGH){
            digitalWrite(ledPin2, HIGH);
            triggerTime2 = millis();
          }
          delay(50);
        }
        lastButtonState2 = buttonState2;


////third button
      if (buttonState3 != lastButtonState3){
          if (buttonState3 == HIGH){
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, HIGH);
            triggerTime3 = millis();
          }
          delay(50);
        }
        lastButtonState3 = buttonState3;
    
    
    
      if (currentTime - triggerTime1 >= interval){
        digitalWrite(ledPin1, LOW);
      }
      
      if (currentTime - triggerTime2 >= interval){
        digitalWrite(ledPin2, LOW);
      }


////third button
      if (currentTime - triggerTime3 >= interval2){
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        
      }

The state machine will turn off the individual LEDs before the time from both on together has elapsed. So when you detect both buttons on, set a flag. Use that flag to not turn off individual LEDs by including it in the time out if statement. Then clear the flag when both LEDs are turned off at the same time.

Thanks for your help Mike, I tried to translate your idea into the code below. I have the feelling I could be more ellegant however, its working!. I added an extra variable that has a value just at the begining of the program to avoid turn on the leds at the begining.

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int inPin1 = 5;   
int inPin2 = 6;
int inPin3 = 7;
int ledPin1 = 8;
int ledPin2 = 9;
int ledState1 = 1;
int ledState2 = 1;
unsigned long triggerTime1 = 0;
unsigned long triggerTime2 = 0;
unsigned long triggerTime3 = 0;
unsigned long interval = 5000;
unsigned long step1 = 11000;
unsigned long interval2 = 10000;


void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
  pinMode(inPin3, INPUT);
}

void loop() {

  unsigned long currentTime = millis();

  buttonState1 = digitalRead(inPin1);
  buttonState2 = digitalRead(inPin2);
  buttonState3 = digitalRead(inPin3);

  
           if (buttonState1 == HIGH){
            triggerTime1 = millis();
            ledState1 = 1;
            step1 = 0;
          }
  
            if (buttonState2 == HIGH){
              ledState2 = 1;
              triggerTime2 = millis();
              step1 = 0;
            }
          
            if (buttonState3 == HIGH){
              ledState1 = 1;
              ledState2 = 1;
              triggerTime3 = millis();
              step1 = 0;
            }
   


      if ((ledState1 =1) && (currentTime - triggerTime1 + step1 >= interval)){
        digitalWrite(ledPin1, LOW);
        ledState1 = 0;
      }
      else{
        digitalWrite(ledPin1, HIGH);
      }
      

      if ((ledState2 =1) && (currentTime - triggerTime2 + step1 >= interval)){
        digitalWrite(ledPin2, LOW);
        ledState2= 0;
      }
      else{
        digitalWrite(ledPin2, HIGH);
      }


    
     if ((ledState1 = 1) && (ledState2 = 1) && (currentTime - triggerTime3 + step1 >= interval2)){
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, LOW);
        ledState1 = 0;
        ledState2 = 0;
      }
      else{
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, HIGH);
      }


}

If you start to add numbers after a variable name => array's :wink: Makes allllll the code that duplicates an action simpler.

And why now use a weird step1 and interval construction?

And now you droped the whole state change... So when you keep the button pushed you keep resetting the timer thus keeping it on.

And what is is whit that setp1 variable? You always just set it to 0....

You didn't alter the code. you made a complete new sketch but only kept the extreme indentation....