Please Help: How to run 3 leds by line with one Pushbutton

Hi everyone. I cant figure out how can I run 3 leds by line with ONE Pushbutton. Any help would be cool.

First, all leds should be off. (lets call led1, led2, led3)
One press to pushbutton should turn on led1, others will be remain off.
When second press, turn on led2 and rest(1,3) should be off.
When third press, turn on led3 and rest(1,2) should be off.

I did it with one led but couldnt with 3. Could you help me please?

Here you can see the circuit and code I made. I think circuit is ok. But code is not ok ofcourse because I dont know how can I add second and third properly :confused:

Code and circuit link: Circuit design How to run 3 leds by line with one Pushbutton -CDGenc | Tinkercad

//this code is not working well. something is wrong.
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPina = 13;       // the pin that the LED is attached to
const int ledPinb = 12;
const int ledPinc = 11;
// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPina, OUTPUT);
  pinMode(ledPinb, OUTPUT);
  pinMode(ledPinc, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPina, HIGH);
    digitalWrite(ledPinb, LOW); 
    digitalWrite(ledPinc, LOW);
  }
  if (buttonPushCounter % 3 == 0) {
    digitalWrite(ledPinb, HIGH);
    digitalWrite(ledPina, LOW); 
    digitalWrite(ledPinc, LOW);
  }
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPinc, HIGH);
    digitalWrite(ledPina, LOW); 
    digitalWrite(ledPinb, LOW);
  } 
  
  else {
    digitalWrite(ledPina, LOW);
    digitalWrite(ledPinb, LOW); 
    digitalWrite(ledPinc, LOW);
  }

}
//this code is not working well. something is wrong.

kodları buraya kopyalarsan yardımcı olalım.

arduinoist:
I did it with one led but couldnt with 3. Could you help me please?

Post your code and pictures here to make it easy to help you. Simple Image Guide

I would have a variable that increments when a button is pressed. Depending on the value of the variable the appropriate LED would light.

...R

Hey! Code here: Just added first leds program. only led 1 works on-off when pressed.

And link to tinkercard for circuit:

int led_a=2;
int led_b=4;
int led_c=6;
int buton=10;
int butonDurum;
int ledDurum=0;
int x=0;

void setup(){
  pinMode(led_a, OUTPUT);
  pinMode(buton, INPUT);
}

void loop(){
  butonDurum=digitalRead(buton);
  if(butonDurum==HIGH && x==0){
    x=1;
    if(ledDurum==0) ledDurum=1;
    else if(ledDurum==1) ledDurum=0;
  }
  else if(butonDurum==LOW && x==1){
    x=0;
  }
 
  if (ledDurum==1){
    digitalWrite(led_a, HIGH);
  }
  else{
    digitalWrite(led_a, LOW);
  }
}

You need to look at the state change detection example. It shows how to count switch presses.

Then, you can do something like:

   switch(pressCount)
   {
      case 0: // never been pressed; all LEDs should be off
         digitalWrite(led_a, LOW);
         digitalWrite(led_b, LOW);
         digitalWrite(led_c, LOW);
         break;
      case 1: // first press
         digitalWrite(led_a, HIGH);
         digitalWrite(led_b, LOW);
         digitalWrite(led_c, LOW);
         break;
   }

You can easily add the other two cases.

You will need to decide what happens when you press the switch for the 4th, and subsequent, times.

Thanks. Ill look at these but Im not good at "if else's" yet. ) : )
The system should do repeat. (should go to led1 on at 4. press and so on)

The system should do repeat. (should go to led1 on at 4. press and so on)

So, when you are counting presses, and you get to 4, change the value to 1. Next time, you'll increment to 1, then 2, etc.

Hi again. I tried but failed.
It fails because:
pressing 2 times makes led_a on, pressing 3 times makes led_b on, pressing 4 times makes led_c on.
Sounds good until now. But when pressing 4 times it makes led_c on (good :slight_smile: ) and also led_a on (bad :frowning: ) because: 4 = 2+2 and it makes led_a + led_c on at 4.press... loops fails so on.

Can anyone help me please?

Here is circuit image: Login | Tinkercad
Here is code:

//this code is not working well. something is wrong.
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPina = 13;       // the pin that the LED is attached to
const int ledPinb = 12;
const int ledPinc = 11;
// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPina, OUTPUT);
  pinMode(ledPinb, OUTPUT);
  pinMode(ledPinc, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPina, HIGH);
    digitalWrite(ledPinb, LOW); 
    digitalWrite(ledPinc, LOW);
  }
  if (buttonPushCounter % 3 == 0) {
    digitalWrite(ledPinb, HIGH);
    digitalWrite(ledPina, LOW); 
    digitalWrite(ledPinc, LOW);
  }
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPinc, HIGH);
    digitalWrite(ledPina, LOW); 
    digitalWrite(ledPinb, LOW);
  } 
  
  else {
    digitalWrite(ledPina, LOW);
    digitalWrite(ledPinb, LOW); 
    digitalWrite(ledPinc, LOW);
  }

}
//this code is not working well. something is wrong.
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);

This assumes that you have an external resistor wired with the switch? Do you? How IS the switch wired?

Until you know that you are reading the switch correctly, the rest of the code does not matter.

Your handling of buttonPushCounter is all wrong, or your understanding of the % operator is. The value on the right needs to be the same in all the statements. What you are comparing the result to will be different.

Hey. Thanks for reply. Yes I connected a resistor to pushbutton. Here is circuit image. I think its ok.

I dont know "buttonPushCounter" or "% operator" functions. I just know primary codes. I found these codes on an example and modified it but it didnt worked. :frowning:

arduinoist:
Hey. Thanks for reply. Yes I connected a resistor to pushbutton. Here is circuit image. I think its ok.

I dont know "buttonPushCounter" or "% operator" functions. I just know primary codes. I found these codes on an example and modified it but it didnt worked. :frowning:

Any help would be nice. :slight_smile:

//this code is not working well. something is wrong.
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPina = 13;       // the pin that the LED is attached to
const int ledPinb = 12;
const int ledPinc = 11;
// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPina, OUTPUT);
  pinMode(ledPinb, OUTPUT);
  pinMode(ledPinc, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledPina, HIGH);
    digitalWrite(ledPinb, LOW); 
    digitalWrite(ledPinc, LOW);
  }
  if (buttonPushCounter % 3 == 0) {
    digitalWrite(ledPinb, HIGH);
    digitalWrite(ledPina, LOW); 
    digitalWrite(ledPinc, LOW);
  }
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPinc, HIGH);
    digitalWrite(ledPina, LOW); 
    digitalWrite(ledPinb, LOW);
  } 
  
  else {
    digitalWrite(ledPina, LOW);
    digitalWrite(ledPinb, LOW); 
    digitalWrite(ledPinc, LOW);
  }

}
//this code is not working well. something is wrong.
  if (buttonPushCounter % 2 == 0) {

This says "if the number of switch presses is even...". Is that what you want?

  if (buttonPushCounter % 3 == 0) {

This says "if the number of switch presses is a multiple of 3...". Is that what you want?

It is generally far simpler to do something like:

  switch(buttonPushCounter)
  {
     case 0:
        // Nothing to do
        break;

     case 1:
        // Light first LED
        break;
 
     case 2:
        // Light second LED
        break;

     case 3:
        // Light third LED
        break;

     case 4:
        buttonPushCounter = 1;
        break;
  }

You might want to set back to 0 when it gets to 4, or you might want to set it back to 1. Only you know for sure.