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