int greenLed = 9; // set pin 9 to a variable
int yellowLed = 10; // set pin 10 to a variable
int redLed = 11; // set pin 11 to a variable
int buttonA = 4; // set pin 4 to a variable
int buttonB = 2; // set pin 2 to a variable
void setup ()
{
pinMode (greenLed, OUTPUT); // set Green LED pin to an output
pinMode (yellowLed, OUTPUT); // set yellow LED pin to an output
pinMode (redLed, OUTPUT); // set red LED pin to an output
pinMode (buttonA, INPUT_PULLUP); // set buttoA to an pullup intput
pinMode (buttonB, INPUT_PULLUP); // set buttonB to an pullup input
}
void loop()
{
if (digitalRead(buttonA) == LOW) {
analogWrite(greenLed, 50); // Turn on Green LED Dim setting
}
else {
digitalWrite(greenLed, LOW); // Turn off Green LED
}
if (digitalRead(buttonB) == LOW) {
analogWrite(redLed, 200); // Turn on Red LED Med setting
}
else {
digitalWrite(redLed, LOW); // Turn off Red LED
}
if (digitalRead(buttonA) == LOW && digitalRead(buttonB) == LOW) { // if BOTH the switches read LOW
// statements
analogWrite(yellowLed, 250); // Turn on Yellow LED Brightnes setting
}
else {
digitalWrite(yellowLed, LOW); // Turn off yellow LED
}
}
When you detect both switches are pushed, turn OFF the red and green LEDs.
Example
if (digitalRead(buttonA) == LOW && digitalRead(buttonB) )
. . .
if (digitalRead(buttonB) == LOW && digitalRead(buttonA) ) {
. . .
if (digitalRead(buttonA) == LOW && digitalRead(buttonB) == LOW ) {
. . .
Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.
Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project See About the Installation & Troubleshooting category.
The problem with your current code, other than you not posting it correctly is that your conditions are not mutually exclusive.
If you think about the 2 buttons, there are 4 possibilities:
- Only A pressed
- Only B pressed
- A & B pressed
- None pressed
Currently when both are pressed all 3 of your if statements are true. And while you could reset the RED & GREEN LEDs if A & B are both are pressed, at the top of the next loop you will turn them on again.
So you are best to construct an IF statement that keeps these 4 situations separate.
If (A pressed) && (Not B pressed)
turn GREEN ON
turn RED OFF
turn YELLOW OFF
else if (A Not pressed) && (B Pressed)
turn GREEN OFF
turn RED ON
turn YELLOW OFF
else if (A pressed) && (B Pressed)
turn GREEN OFF
turn RED OFF
turn YELLOW ON
else
turn GREEN OFF
turn RED OFF
turn YELLOW OFF
I did this but the yellow led did not come on.
*/
int greenLed = 9; // set pin 9 to a variable
int yellowLed = 10; // set pin 10 to a variable
int redLed = 11; // set pin 11 to a variable
int buttonA = 4; // set pin 4 to a variable
int buttonB = 2; // set pin 2 to a variable
void setup ()
{
pinMode (greenLed, OUTPUT); // set Green LED pin to an output
pinMode (yellowLed, OUTPUT); // set yellow LED pin to an output
pinMode (redLed, OUTPUT); // set red LED pin to an output
pinMode (buttonA, INPUT_PULLUP); // set buttoA to an pullup intput
pinMode (buttonB, INPUT_PULLUP); // set buttonB to an pullup input
}
void loop()
{
if (digitalRead(buttonA) == LOW && digitalRead(buttonB) ) {
analogWrite(greenLed, 50); // Turn on Green LED Dim setting
}
else {
digitalWrite(greenLed, LOW); // Turn off Green LED
}
if (digitalRead(buttonB) == LOW && digitalRead(buttonA) ) {
analogWrite(redLed, 200); // Turn on Red LED Med setting
}
else {
digitalWrite(redLed, LOW); // Turn off Red LED
}
if (digitalRead(buttonA) == LOW && (buttonB) == LOW) {
analogWrite(yellowLed, 200); // Turn on Yellow LED Brightnes setting
}
}
Apart from these errors...
I don't think you read my post...#4
You would make life easier for yourself if you have single If statement, and set/unset all the LEDs for each option.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.