Having 2 Buttons Control 2 Groups of LEDs

Hello everyone,

I'm a complete beginner still figuring out many things. I've been working on turning on/off 10 LEDs with 2 buttons. The idea is to have 1 button control 5 LEDs. I'm able to have button 1 control 5 LEDs, but button 2 has no response. I'm sure that all the LEDs are plugged in correctly as they responded to a different code.

Please see current code:

int ledPins[] = {2,3,4,5,6,7,8,9,10,11};
int BUTTON1 = 13;
int BUTTON2 = 12;

int val1 = 0; // val will be used to store the state of the input pin
int old_val1 = 0; // stores the previous value of 'val'
int state1 = 0; // 0 = LED off, 1 = LED on

int val2 = 0;
int old_val2 = 0;
int state2 = 0;

void setup() {

  pinMode (BUTTON1, INPUT);
  pinMode (ledPins[0], OUTPUT);
  pinMode (ledPins[1], OUTPUT);
  pinMode (ledPins[2], OUTPUT);
  pinMode (ledPins[3], OUTPUT);
  pinMode (ledPins[4], OUTPUT);

  pinMode (BUTTON2, INPUT);
  pinMode (ledPins[5], OUTPUT);
  pinMode (ledPins[6], OUTPUT);
  pinMode (ledPins[7], OUTPUT);
  pinMode (ledPins[8], OUTPUT);
  pinMode (ledPins[9], OUTPUT);
  
}

void loop() {
  
  val1 = digitalRead(BUTTON1);
  //check if there was a transition

  if ((val1 == HIGH) && (old_val1 == LOW)){
    state1 = 1 - state1;
    delay (10);
  }
  old_val1 = val1; // val is now old -> store it

  if (state1 == 1){
    digitalWrite(ledPins[0], HIGH);
    digitalWrite(ledPins[1], HIGH);
    digitalWrite(ledPins[2], HIGH);
    digitalWrite(ledPins[3], HIGH);
    digitalWrite(ledPins[4], HIGH);
 
  } else{
    digitalWrite(ledPins[0], LOW);
    digitalWrite(ledPins[1], LOW);
    digitalWrite(ledPins[2], LOW);
    digitalWrite(ledPins[3], LOW);
    digitalWrite(ledPins[4], LOW);

}

  val2 = digitalRead(BUTTON2);

  if ((val2 == HIGH) && (old_val2 == LOW)){
    state2 = 1 - state2;
    delay(10);
  }
  old_val2 = val2;

  if (state2 == 1){
  digitalWrite(ledPins[5], HIGH);
  digitalWrite(ledPins[6], HIGH);
  digitalWrite(ledPins[7], HIGH);
  digitalWrite(ledPins[8], HIGH);
  digitalWrite(ledPins[9], HIGH);

} else {
  digitalWrite(ledPins[5], LOW);
  digitalWrite(ledPins[6], LOW);
  digitalWrite(ledPins[7], LOW);
  digitalWrite(ledPins[8], LOW);
  digitalWrite(ledPins[9], LOW);
}
}

I realize that my code is very lengthy. I've tried to incorporate Arrays but not quite successfully. Can anyone tell me what I might have done incorrectly? Can anyone also help me condense my code?

The original code is from the book Getting Started with Arduino by Massimo Banzi:

//Example 03C: Turn on LED when the button is pressed
//and keep it on after it's released
//including simple de-bouncing
//now with another new and improved formula!

const int LED = 13; //the pin for the lED
const int BUTTON = 7; //the input pin where the 
                      //pushbutton is connected
int val = 0; //val will be used to store the state of the input pin
int old_val = 0; //this variable stores the previous value of "val"
int state = 0; //0 = LED off and 1 = LED on


void setup() {
 pinMode(LED,OUTPUT); //tell Arduino LED is an output
 pinMode(LED,INPUT);  //and BUTTON is an input
}

void loop() {
  val = digitalRead(BUTTON); //read input value and store it
  //check if there was a transition
  if ((val == HIGH) && (old_val == LOW){
    state = 1 - state;
    delay(10);
  }
  old_val = val; //val is now old, let's store it

  if(state == 1){
    digitalWrite(LED, HIGH); //turn LED on
  } else{
    digitalWrite(LED, LOW);
  }
}

Any help is appreciated! Thank you!

Best,
Veronica Tsai

I've linked a short video of my situation here !

It looks like you have the inputs wired to go HIGH when the buttons are pressed. Do you have pulldown resistors on the inputs to keep them in a known state at all times ?

If not then consider using INPUT_PULLUP in the pinMode()s to activate the built in pullup resistors and change the wiring to take the inputs LOW when the buttons are pressed and the test logic to match.

If you are using a breadboard make sure that its power rails do not have a break in the centre which is very common.

Not the problem, but you can shorten the code by using for loops and shorten it even more by writing a function to turn the LEDs on or off without repeating code, but get the simple version working first.

Hello UKHeliBob, thank you for your reply.

The LEDs I have came with pull-down resistors already connected to their cathodes. I also made sure that there are no breaks in my breadboard.

I forgot to mention that sometimes, 1 or 2 LEDs from the unresponsive group 2 light up randomly but not controlled by the button.

Does my code's logic make sense? I tried creating integers val2, old_val2, state2 for group 2, but they don't seem to be doing anything.

Could you teach me how to use a for loop in my code? I'm not too familiar with it.

The LEDs I have came with pull-down resistors already connected to their cathodes

It is not the LEDs that need the pulldown resistors it is the switches. The LEDs do need resistors in series with them to limit the current through them and it sounds like this is what you have. Can you please post a link to the LEDs that you are using.

for loops can come later. Get the basic program working first.

The person who ordered the LEDs has already left her position. I am not sure where she ordered from. They look exactly like these, however.

An update - I used a different pin for button 2. The button successfully turned on and off the LEDs for a few times. Sometimes the LEDs just flicker and not turn off, and most of the time the LEDs still do not respond to the button.

I will try adding resistors to the buttons.

I have some suggestios:

  1. learn how to use for cycles
  2. learn other types of variaboes (like bytes, that can contiens numbers 0 <=x <255)
  3. use the world const for every variable declaration that mustn't change
  4. the digitalWrite can has witch the second parameter also a variable, so you can sobstitute the if/else witch less and simpler code.

All this suggestions CAN'T solv problem, but can help to find and solv it. They are also usefull informations for future projects.

I'd make a couple of changes:

pinMode (BUTTON1, INPUT);
to
pinMode (BUTTON1, INPUT_PULLUP); // turn on internal pullup

pinMode (BUTTON2, INPUT);
to
pinMode (BUTTON2, INPUT_PULLUP); // turn on internal pullup

wire the buttons to connect to Gnd when pressed.

Then corresponding logic change:

if ((val1 == LOW) && (old_val1 == HIGH)){ // button was not pressed before, now it is

and

if ((val2 == LOW) && (old_val2 == HIGH)){ // button was not pressed before, now it is

Hi CrossRoads,
I made the changes, and it works now! Thank you so much!
Could you explain what I did incorrectly? I have never used IINPUT_PULLUP before. I quickly read about it, but I still don't fully understand...

After three mentions of pull resistors, did it occur to you to try Google?

By just using

pinMode (BUTTON1, INPUT);

and no external resistor, the pin is left in an unknown state when the button in not pressed.

You could use an external 10K resistor and connect it to Gnd to hold it low, then wire the button to +5 to pull the pin High when pressed.; however, a miswired connection then shorts +5 to Gnd, resetting the board, or worse, breaking something.

You could use an external 10K resistor and connect it to +5 to hold it high, then wire the button to Gnd to pull the pin low when pressed, less chance of damaging anything, 10K only lets 0.5mA flow.
Or, you could use the internal pullup resistor, then wire the button to Gnd to pull the pin low when pressed, fewer wires, and no chance of damaging anything as there is no connection to +5 at all.

There is no internal pulldown resistor, only pullup.

Hi CrossRoads, thank you very much. I understand clearly now. I really appreciate your patience with a beginner like me.

septillion, why yes, it certainly did. If I had figured out what I did wrong using Google, I would not be here. Thank you for your suggestion.

If I had figured out what I did wrong using Google, I would not be here.

What a shame that you did not read reply #2

I apologize. I did read your comment, I just was not sure how to apply it. Thank you very much for your help as well.

vtsai01:
I apologize. I did read your comment, I just was not sure how to apply it. Thank you very much for your help as well.

Then ASK what you don't understand instead of rudely ignoring it :wink:

But I really doubt you tried Google (or I would suggest to invest in upgrading your Google skills) because the first link does explain it already...