I have 10 illuminated push button with LED's (Button is button and LED is LED).
When I push Button 1 LED 1 needs to turn on HIGH and when im realesing the button LED 1 shall go back to low.
I can get it to Work with 2 buttons but when i am trying with 3 button it goes wrong (and I need it to Work with 10 buttons).
If im using this code:
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);//This is LED for Button 1
pinMode(5, OUTPUT);//This is LED for Button 2
pinMode(7, OUTPUT);//This is LED for Button 3
pinMode(2, INPUT);//This is Button 1
digitalWrite(2, HIGH);
pinMode(4, INPUT);
digitalWrite(4, HIGH);//This is Button 2
pinMode(6, INPUT);
digitalWrite(6, HIGH);//This is Button 3
}
void loop() {
if (digitalRead(2) == LOW)
{
digitalWrite(3, HIGH);
}
else if (digitalRead(4) == LOW)
{
digitalWrite(5, HIGH);
}
else if (digitalRead(6) == LOW)
{
digitalWrite(7, HIGH);
}
else
{
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(7, LOW);
}
}
"but when i am trying with 3 button it goes wrong "
What goes wrong? Code looks okay to me, perhaps a wiring error? LED flipped around perhaps, a switch not connected to Gnd when pressed?
try not to use the same pin number as a input and output may help
an array code will look something like this
byte ledButtonArray[] = {3, 5, 7, 9,};//these are my led pins
byte buttonArray[] = {2, 4, 6, 8,};//these are my input pins
//now if i say buttonArray[x] and x = 1 im saying use the second pin in the
//array as a array starts at 0 so 0 = pin 2, 1 = pin 4
void setup() {
for (byte x = 0; x < 4; x = x + 1) {
pinMode(buttonArray[x], INPUT_PULLUP);//set input pins
pinMode(ledButtonArray[x], OUTPUT);//set output pins
}
}
void loop() {
for (byte x = 0; x < 4; x = x + 1) {//x will be 0 then 1 then 2 then 3
//so if button 0 is low meaning the button connected to pin 2 then
//turn led on thats connected to pin 3 on. If not low then turn
//led off
//notice this requires a complete loop to turn the led back off
if (digitalRead(buttonArray[x]) == LOW) {
digitalWrite(ledButtonArray[x], HIGH);
} else {
digitalWrite(ledButtonArray[x], LOW);
}
}//end for
}