1 Button 2 LEDS

Hello,

Im trying to turn on 2 Leds separately, one at a time, but with the same button, the idea here is to click the button and then the LED1 turns on and off(after a second or something, even if the button is still pushed down) and then when i click the same button again the LED2 turns on and off(after a second or something, once again,even if the button is still pushed down), all that must be in loop.

Heres the code i came up with untill now, hope you guys can help me. Thanks in advance.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  12;      // the number of the LED pin
const int ledPin2 =  6;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int var = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);    
  pinMode(ledPin2, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
  }

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (buttonState==HIGH && var==0) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
    digitalWrite(ledPin2, LOW);
    var=1;
  } 
  else {
   digitalWrite(ledPin2, HIGH); 
   digitalWrite(ledPin, LOW);
   var=0;
  }
}

Nice start:
I guess var is intended to keep track of the current state.

However, in your case it simply switches the leds while button is pressed, so fast that it seems both are on in parallel.
When button is released, it ends in the else branch: Led1 off, led2 on.

  • correct ?

There’s something missing:

  • You need to notice when button pressing happens: Was LOW, now is HIGH. Only this should trigger something.
    Perhaps you use the[b]buttonstate[/b] variable for this purpose. That requires some change to the line
    buttonState = digitalRead(buttonPin); (only set it to the current value after your “state change” detection)
  • Your goal “once a LED ON period is started, it should continue no matter what happens to the switch”,
    looks like one of the rare cases for a delay() .

Good luck

spelling only visible after posting. Edit required;)

Thanks for the Reply And you are right, that var is to keep track of what LED is on, and thanks to your reply i managed to get something close to perfect, but its kinda buggy, it works only sometimes, maybe you can give me a hand, i must be missing something.

Heres the updated code:

void loop(){
  // read the state of the pushbutton value:
    buttonState = digitalRead(buttonPin);
    
    
    //estado botao
     if(buttonState==LOW){
     digitalWrite(ledPin, LOW);
     digitalWrite(ledPin2,LOW);
     if (var==0){
      var=1;
                 }
                 else{
                     var=0;
                      } 
                       }   
                       
    //turn LED on:    
    if(buttonState==HIGH && var==0){
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);  
    }
    else if (buttonState==HIGH && var==1){ 
    digitalWrite(ledPin2,HIGH);
    delay(100);
    digitalWrite(ledPin2,LOW);  
    }

I think i got it all figured out, heres the complete code to light up 2 leds(one at a time) using only one button

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  6;      // the number of the LED pin
const int ledPin2 =  8;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int var = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);    
  pinMode(ledPin2, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
  Serial.begin(9600);
  }

void loop(){
  // read the state of the pushbutton value:
     buttonState = digitalRead(buttonPin);  
     //estado botao
     if (buttonState==HIGH && var==0){
      var=1;
      Serial.println(var);
                 }
                 else if(buttonState==HIGH && var==1){
                     var=0;
                     Serial.println(var);
                      }
    //turn LED on:    
    if(buttonState==HIGH && var==0){
    digitalWrite(ledPin, HIGH);
    delay(400);
    digitalWrite(ledPin, LOW); 
    buttonState=LOW; 
    }
    else if (buttonState==HIGH && var==1){ 
    digitalWrite(ledPin2,HIGH);
    delay(400);
    digitalWrite(ledPin2,LOW); 
    buttonState=LOW;
    }  
   if(buttonState==LOW){
     digitalWrite(ledPin, LOW);
     digitalWrite(ledPin2,LOW);
     }
   
  

    }