i m interfacing 4 button to arduino uno with 4 LED respective. My program as follow:-
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int ledPin1 = 6; // the number of the LED pin
const int ledPin2 = 7;
const int ledPin3 = 8;
const int ledPin4 = 9;
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
}
else if(buttonstate2 == HIGH)
{
// turn LED on:
digitalWrite(ledPin2, HIGH);
}
else if(buttonstate3 ==HIGH)
{
// turn LED on:
digitalWrite(ledPin3, HIGH);
}
else if(buttonstate4 ==HIGH)
{
// turn LED on:
digitalWrite(ledPin4, HIGH);
}
}
I am getting error as below:-
C:\Users\Admin\Documents\Arduino\sketch_aug28a\Button\Button.ino: In function 'void loop()':
Button:71: error: 'buttonstate2' was not declared in this scope
else if(buttonstate2 == HIGH)
^
Button:76: error: 'buttonstate3' was not declared in this scope
else if(buttonstate3 ==HIGH)
^
Button:81: error: 'buttonstate4' was not declared in this scope
else if(buttonstate4 ==HIGH)
^
exit status 1
'buttonstate2' was not declared in this scope