Using 2 buttons simultaneously to make a 3rd action

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

1 Like
  buttonState4 = digitalRead(buttonPin2);digitalRead(buttonPin3);

What is this supposed to do?

I was hoping that it would have the arduino simultaneously use the values for buttonPin2 and buttonPin3 as a 4th state that I could use to create a 4th specific action. It doesn't do that, and I'm at a loss for how to phrase a question to learn how to make that happen.

Read the 2 button state then the logic is like this

if button1State is pressed and button2State is pressed
  turn on blue LED
else
if button1State is pressed
  turn on red LED
else
if button2State is pressed
  turn on green LED
end if

I will leave it to you to fill in the real code

Ok! That makes sense! I think I can get it figured out from here.

Thank you very much!

Or read them both together.

void loop(){

buttonState = PORTD; // read D7 to D0 at once
button32State = buttonState & 0b00011000; // mask off the 2 bits in question
switch (button32State){
case 0b00000000:
// none on
digitalWrite (ledPin2, LOW);
digitalWrite (ledPin3, LOW);
break;
case 0b00001000:
// one on
digitalWrite (ledPin2, HIGH);
digitalWrite (ledPin3, LOW);
break;
case 0b00010000:
// other one on
digitalWrite (ledPin2, LOW);
digitalWrite (ledPin3, HIGH);
break;
case 0b00011000:
// bith on
digitalWrite (ledPin2, HIGH);
digitalWrite (ledPin3, HIGH);
break;
} // end of switch case
} // end of loop

I don't think the op is up to using PORT yet.

However, thik about this:

int state = 0;

if(button 1 is pressed) {
  state = state + 1;
}

if(button 2 is pressed) {
  state = state + 2;
}

switch(state) {
case 0: // no buttons pressed
  break;
case 1: // button 1 pressed
  break;
case 2: // button 2 pressed
  break;
case 3: // both buttons pressed
  break;
}

Keep in mind that it’s unlikely that you’ll push both buttons exactly at the same moment.
This means you’ll get a tiny flicker of incorrect colour for the couple of milliseconds between pushing button one and then the second button.

To address this you may want to put in a tiny hold-off period to wait for the button inputs to stabilise - then act on the logical combination of 1-2-3 states... or off!