Is this correct
// the number of the pushbutton pin
const int a = 3;
const int b = 4;
const int c = 5; // the number of the pushbutton pin
const int ledpina = 10;
const int ledpinb = 11;
const int ledpinc = 12;
// variables will change:
int buttonStatea = 0;
int buttonStateb = 0;// variable for reading the pushbutton status
int buttonStatec = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledpina, OUTPUT);
pinMode(ledpinb, OUTPUT);
pinMode(ledpinc, OUTPUT);
pinMode(a, INPUT);
pinMode(b, INPUT);
pinMode(c, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonStatea = digitalRead(a);
buttonStateb = digitalRead(b);
buttonStatec = digitalRead(c);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (a == HIGH) {
// turn LED on:
digitalWrite(ledpina, HIGH);
}
if (b == HIGH) {
// turn LED on:
digitalWrite(ledpinb, HIGH);
}
if (c == HIGH) {
// turn LED on:
digitalWrite(ledpinc, HIGH);
}
else {
// turn LED off:
digitalWrite(ledpina, LOW);
digitalWrite(ledpinb, LOW);
digitalWrite(ledpinc, LOW);
}
}
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
Two bugs:
1) it should probably be if (buttonStatea == HIGH) rather than if (a == HIGH)
2) I'm guessing but your logic probably isn't what you want. You currently turn all the leds off if button c is not pressed.
What I'm guessing you were aiming for is if button a is down turn led a on otherwise turn it off. That would be:
if (buttonStatea == HIGH)
digitalWrite(ledpina, HIGH);
else
digitalWrite(ledpina, LOW);
if (buttonStateb == HIGH)
digitalWrite(ledpinb, HIGH);
else
digitalWrite(ledpinb, LOW);
// repeat for c
If that is what you want then you can also further simplify things down to:
digitalWrite( ledpina, buttonStatea );