Using 2 buttons simultaneously to make a 3rd action
hello,
i've been trying google answer question think lack knowledge phrase properly.
i have rgb led shine red when 1 button pressed, green when button pressed , blue when both buttons pressed. tried messing creating buttonstate had 2 separate buttons in , doesn't seem answer. when verifying code error forces me add ";" in between digitalreads presume means buttonstate ignores second digitalread.
in code there third button firing 3 leds simultaneously , working expected.
sorry if incredibly obvious, said, lack knowledge of terms necessary hunt down answer.
thanks
i've been trying google answer question think lack knowledge phrase properly.
i have rgb led shine red when 1 button pressed, green when button pressed , blue when both buttons pressed. tried messing creating buttonstate had 2 separate buttons in , doesn't seem answer. when verifying code error forces me add ";" in between digitalreads presume means buttonstate ignores second digitalread.
code: [select]
// constants won't change. they're used here set pin numbers:
const int buttonpin1 = 2;// number of pushbutton pin
const int buttonpin2 = 3;
const int buttonpin3 = 4;
const int ledpin1 = 10; // number of led pin
const int ledpin2 = 11;
const int ledpin3 = 12;
// variables change:
int buttonstate1 = 0; // variable reading pushbutton status
int buttonstate2 = 0;
int buttonstate3 = 0;
int buttonstate4 = 0;
void setup() {
// put setup code here, run once:
// initialize led pin output:
pinmode(ledpin1, output);
pinmode(ledpin2, output);
pinmode(ledpin3, output);
// initialize pushbutton pin input:
pinmode(buttonpin1, input);
pinmode(buttonpin2, input);
pinmode(buttonpin3, input);
}
void loop() {
// put main code here, run repeatedly:
// read state of pushbutton value:
buttonstate1 = digitalread(buttonpin1);
buttonstate2 = digitalread(buttonpin2);
buttonstate3 = digitalread(buttonpin3);
buttonstate4 = digitalread(buttonpin2);digitalread(buttonpin3);
// check if pushbutton pressed. if is, buttonstate 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 code there third button firing 3 leds simultaneously , working expected.
sorry if incredibly obvious, said, lack knowledge of terms necessary hunt down answer.
thanks
Arduino Forum > Using Arduino > Programming Questions > Using 2 buttons simultaneously to make a 3rd action
arduino
Comments
Post a Comment