I would like to do an action if both buttons on my arduino pro micro are depressed simultaneously. I'm getting some strange results:
// Buttons
Bounce button0 = Bounce();
Bounce button1 = Bounce();
void setup() {
Serial.begin(115200);
while (!Serial) {
;
}
pinMode(2, INPUT_PULLUP);
button0.attach(2);
button1.interval(1);
pinMode(3, INPUT_PULLUP);
button1.attach(3);
button1.interval(1);
Serial.println("Setup Complete");
}
void loop() {
button0.update();
button1.update();
if (button0.fell()){
Serial.println("button0 fell");
} else if (button0.rose()) {
Serial.println("button0 rose");
}
if (button1.fell()){
Serial.println("button1 fell");
} else if (button1.rose()) {
Serial.println("button1 rose");
}
if (button0.fell() & button1.fell()){
Serial.println("buttons fell");
} else if (button0.rose() && button1.rose()) {
Serial.println("buttons rose");
}
}
Here is the output to the Serial console:
Setup Complete
button0 fell
button1 fell
button0 rose
button1 rose
Why isn't this branch being executed? button0.fell() & button1.fell()
For the life of me I cannot figure this out. Thank you!
EDIT
This doesn't work either, which is crazy:
void loop() {
button0.update();
button1.update();
if (button0.fell()) {
if (button1.fell()) {
Serial.println("buttons fell");
}
}
if (button0.rose()) {
if (button1.rose()) {
Serial.println("buttons rose");
}
}
}
EDIT
Thank you for the answers! Unless the person was to push both buttons in the exact same iteration of the loop, my code would never work. Here is what I ended up using:
if ((button0.fell() && !button1.read()) || (button1.fell() && !button0.read())) {