Controlling 4 leds with 2 buttons

What I want to do is have it so that when NONE of the buttons are pressed, a RED led is on, when the LEFT button is pressed, a BLUE led is on, when the RIGHT button is pressed, the YELLOW led is on, and when BOTH are pressed, a GREEN led is on.

How would the code be structured in this situation? Would you just use four IFs consecutively?

Any help is appreciated, thanks!

DylanInno3495:
How would the code be structured in this situation? Would you just use four IFs consecutively?

You'd use if-else statements. Google it, try making your own program and then reply if your code doesn't work. We don't code projects for free.

When do they turn off? You need to be very explicit in what you want to happen.

Hi,
Welcome to the Forum.
This will be your truth table

Button1 Button2 REDLED BLUELED YELLOWLED GREENLED
OFF OFF ON OFF OFF OFF
ON OFF OFF ON OFF OFF
OFF ON OFF OFF ON OFF
ON ON OFF OFF OFF ON

A set of IF statements will work, and is compact enough for two inputs.

Tom.... :slight_smile:

This is my code:

const int buttonL = 6;
const int buttonR = 7;
const int G = 13;
const int B = 12;
const int Y = 11;
const int R = 10;

int Rstate = 0;
int Lstate = 0;

void setup() {
// put your setup code here, to run once:
pinMode(buttonL, INPUT);
pinMode(buttonR, INPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(Y, OUTPUT);
pinMode(R, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
Rstate = digitalRead(buttonR);
Lstate = digitalRead(buttonL);

if (Rstate == LOW, Lstate == HIGH) {
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
digitalWrite(Y, LOW);
digitalWrite(R, LOW);
} else {
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(Y, LOW);
digitalWrite(R, HIGH);
}

if (Rstate == HIGH, Lstate == LOW) {
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(Y, HIGH);
digitalWrite(R, LOW);
} else {
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(Y, LOW);
digitalWrite(R, HIGH);
}

if (Rstate == HIGH, Lstate == HIGH) {
digitalWrite(G, HIGH);
digitalWrite(B, LOW);
digitalWrite(Y, LOW);
digitalWrite(R, LOW);
} else {
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(Y, LOW);
digitalWrite(R, HIGH);
}

}

Use code tags.

And lookup in the reference guide on what "&&" means.

You may also want to read up on INPUT vs INPUT_PULLUP for pinModes.

And if it's not behaving how you think it ought to, you need to show us your wiring.

INTP:
Use code tags.

And lookup in the reference guide on what "&&" means.

You may also want to read up on INPUT vs INPUT_PULLUP for pinModes.

And if it's not behaving how you think it ought to, you need to show us your wiring.

Comments would probably help.

I have replaced the commas with "&&"

I am using a knockoff, so I'm not sure if the board has the integrated pullup resistor.

It's still not working, so I'll make up a 123d circuit and take a screenshot of that.

Even 'knockoffs' use the same processor chip, which is where the pullup resistors would be. There is no such thing as an Arduino or clone thereof that doesn't have them.

No need to have three of the same else blocks. Put it in a separate if statement, or your LEDs will flicker (very fast, making the red one seem on all the time, together with the one from the button press). Alternatively, use "else if" statements and a final "else" for the red LED.
The way you wrote it way every time an if statement evaluates false (which two out of the three will always do) the red LED is switched on and the rest off.

I've gotten it to work!

Here is my code:

// the pins for the buttons and LEDs will not change
const int buttonL = 6;
const int buttonR = 7;
const int G = 13;
const int B = 12;
const int Y = 11;
const int R = 10;

int Rstate = 0;
int Lstate = 0;

void setup() {
// put your setup code here, to run once:
pinMode(buttonL, INPUT);
pinMode(buttonR, INPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(Y, OUTPUT);
pinMode(R, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
Rstate = digitalRead(buttonR);
Lstate = digitalRead(buttonL);

if (Rstate == LOW && Lstate == HIGH) { //when only the Left button is pressed, only the Blue LED will go on
digitalWrite(G, LOW);
digitalWrite(B, HIGH);
digitalWrite(Y, LOW);
digitalWrite(R, LOW);
} else if (Rstate == HIGH && Lstate == LOW) { //when only the Right button is pressed, only the Yellow LED will go on
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(Y, HIGH);
digitalWrite(R, LOW);
} else if (Rstate == HIGH && Lstate == HIGH) { //when both buttons are pressed, only the Green LED will go on
digitalWrite(G, HIGH);
digitalWrite(B, LOW);
digitalWrite(Y, LOW);
digitalWrite(R, LOW);
} else { //when neither of the buttons are pressed, only the Red LED will go on
digitalWrite(G, LOW);
digitalWrite(B, LOW);
digitalWrite(Y, LOW);
digitalWrite(R, HIGH);
}
}

Congrats, well done!
Now if you also learn how to use the code tags you'd get a perfect score :slight_smile: Using indentation in your code also helps a lot in making it readable.