Two buttons to control two different set of leds

Hi all,

I'm a beginner.

I have two buttons to control two group of led.
group 1 has two led
group 2 has three led
when press button 1, group 1 led1 will be on, press again, led1 and 2 will be on, press again, all off.
button 2 is the same as group 1 but just have 1 more light.
however, what ever I change, press button 1 or 2, both group led will be on.

can someone please advise?

Thank you in advance.

int switchPin = 9;              // switch is connected to pin 9
int switchPin2 = 10;         // switch2 is connected to pin 10
int led1Pin = 2;
int led2Pin = 3;
int led3Pin = 4;
int led4Pin = 5;
int led5Pin = 6;

int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int val3;
int val4;
int buttonState;                // variable to hold the button state
int buttonState2;
int Mode = 0;              // What mode is the light in?

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(switchPin2, INPUT);    // Set the switch pin as input
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(led5Pin, OUTPUT);
  buttonState = digitalRead(switchPin);   // read the initial state
  buttonState2 = digitalRead(switchPin2);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(100);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (Mode == 0) {          
          Mode = 1;               
        } else {
            if (Mode == 1) {        
            Mode = 2;           
        } else {
            if (Mode == 2) {      
            Mode = 0;           
        } 
       }
      }
     }
    }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (Mode == 0) { // all-off
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
  }

  if (Mode == 1) { 
    digitalWrite(led1Pin, HIGH);
    digitalWrite(led2Pin, LOW);
  }

  if (Mode == 2) { 
    digitalWrite(led1Pin, HIGH);
    digitalWrite(led2Pin, HIGH);
  }
  if (Mode == 3)  { 
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
  }    
     
val3 = digitalRead(switchPin2);      // read input value and store it in val
  delay(100);                         // 10 milliseconds is a good amount of time
  val4 = digitalRead(switchPin2);     // read the input again to check for bounces
  if (val3 == val4) {                 // make sure we got 2 consistant readings!
    if (val3 != buttonState2) {          // the button state has changed!
      if (val3 == LOW) {                // check if the button is pressed
        if (Mode == 0) {          
          Mode = 1;               
        } else {
            if (Mode == 1) {        
          Mode = 2;           
        } else {
            if (Mode == 2) {      
            Mode = 3;
        } else {
            if (Mode == 3) {  
             Mode =0;        
              }
        } 
       }
      }
     }
    }
    buttonState2 = val3;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (Mode == 0) { // all-off
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, LOW);
  }

  if (Mode == 1) { 
    digitalWrite(led3Pin, HIGH);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, LOW);
  }

  if (Mode == 2) { 
    digitalWrite(led3Pin, HIGH);
    digitalWrite(led4Pin, HIGH);
    digitalWrite(led5Pin, LOW);
  }
  if (Mode == 3)  { 
    digitalWrite(led3Pin, HIGH);
    digitalWrite(led4Pin, HIGH);
    digitalWrite(led5Pin, HIGH);
  }    
}

Welcome to the forum.

If you have two independent groups, then why do they share the same 'Mode' variable ?

I made a Wokwi project for you, but it has your sketch without any changes: https://wokwi.com/projects/328699072519078483

There are libraries to debounce a button. That will make the sketch easier to read.

1 Like

Not helping with your problem but this could be a lot easier / more readable

Something like

Mode++;
if(Mode == 3)
{
  Mode = 0;
}
1 Like

Hello tomwei1028
As specified you have to groups of LED. Take some time and study the usage of array´s.
The ussage of a arrays will simplify the code to have an array per LED group. In next step you code a struct per button to condense all information for a service=method to take action if a button is pressed. BTW pressed: A debouncer or button scanner is needed too.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

You have mutliple things to manage

  • debouncing a button

  • detecting state-change

  • increasing a variable for LED-group 1 circular 0,1,2,0,1,2,0,1,2,0,1,2,..

  • switching LEDs of group 1 on/off according to the value of the variable

applying the same principle for LED-group 2 which has 3 LEDs

  • increasing a variable for LED-group 2 circular 0,1,2,3,0,1,2,3,0,1,2,3,...
  • switching LEDs of group 2 on/off according to the value of the variable

all these functionalities can be developped on its own separately.
developing software this way is - in the long run - faster that trying to do all at once
because the number of lines of code that contain a bug is always small

Be the change you want to see in the world
best regards Stefan

@tomwei1028

we either need a clear schematic how you have wired your components or we need to guess.
I took the schematic of @Koepel (thank you for the pre work) and ACTIVATED THE INTERNAL PULLUPs as I see in his schematic the buttons are connected to GND(!).
Than I modified slightly the sketch and came up with this:

https://wokwi.com/projects/328721332423361108

if you have connected the buttons to GND you MUST use either external pullup resistors or the internal pullup - otherwise you will not get a reliable result.

1 Like

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