Hi Everyone,
I am new and this is my first post to the forum.arduino.cc family. I enjoy reading through your posts on the many complexities Arduino Uno offers and would like to ask a question of my own.
This question may seem simple for some to answer, here it goes...
I have 4 buttons and 4 lights on my breadboard that I am trying to make work independently of one another. For example, when button 1 (pin2) is pressed I would like light 1 (pin6) to illuminate, and the same for button 2 (pin3) / light 2 (pin4), and so on...
I have verified the code to be correct and get no errors when verified/uploaded to the Arduino Uno. But it just doesn't seem to illuminate the lights properly when each button is pressed. Could one of your please verify my code is type correctly? I will post the code below.
Thank you,
Daniel
/*
// 4 Lights, 4 Buttons
*/
int switchstate = 0;
void setup(){
// declare the LED pins as outputs
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
// declare the switch pins as an input
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
}
void loop(){
// read the value of the switch
// digitalRead() checks to see if there is voltage
// on the pin or not
switchstate = digitalRead(2);
switchstate = digitalRead(3);
switchstate = digitalRead(4);
switchstate = digitalRead(5);
// if the button is not pressed
// blink the red LEDs
if (switchstate == LOW) {
digitalWrite(6, LOW); // turn the red LED on pin 6 off
digitalWrite(7, LOW); // turn the red LED on pin 7 off
digitalWrite(8, LOW); // turn the red LED on pin 8 off
digitalWrite(9, LOW); // turn the red LED on pin 9 off
}
// this else is part of the above if() statement.
// if the switch is not LOW (the button is pressed)
// the code below will run
else {
digitalWrite(6, HIGH); // turn the red LED on pin 6 off
digitalWrite(7, HIGH); // turn the red LED on pin 7 off
digitalWrite(8, HIGH); // turn the red LED on pin 8 off
digitalWrite(9, HIGH); // turn the red LED on pin 9 off
// wait for a quarter second before changing the light
delay(2000);
}
}