Hello,
I've been trying to google an answer to this question but think I lack the knowledge to phrase it properly.
I have an RGB led that I would like to shine red when one button is pressed, green when another button is pressed and blue when both buttons are pressed. I tried messing with creating a buttonState that had two separate buttons in it and that doesn't seem to be the answer. When verifying my code I get an error that forces me to add a ";" in between the digitalReads which I presume means that the buttonState ignores the second digitalRead.
// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 2;// the number of the pushbutton pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int ledPin1 = 10; // the number of the LED pin
const int ledPin2 = 11;
const int ledPin3 = 12;
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
void setup() {
// put your setup code here, to run once:
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin2);digitalRead(buttonPin3);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED1 on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
} else {
// turn LED1 off:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
}
if (buttonState2 == HIGH) {
// turn LED2 on:
digitalWrite(ledPin2, HIGH);
} else {
// turn LED2 off:
digitalWrite(ledPin2, LOW);
}
if (buttonState3 == HIGH) {
// turn LED3 on:
digitalWrite(ledPin3, HIGH);
} else {
// turn LED3 off:
digitalWrite(ledPin3, LOW);
}
if (buttonState4 == HIGH) {
// turn LED3 on:
digitalWrite(ledPin1, HIGH);
} else {
// turn LED3 off:
digitalWrite(ledPin1, LOW);
}
}
In this code there is also a third button that is firing all three leds simultaneously and that is working as expected.
Sorry if this is incredibly obvious, as I said, I lack the knowledge of the terms necessary to hunt down the answer.
Thanks