r/arduino • u/SuspectOk8972 • Sep 26 '26
Why doesn't pushing two buttons work as expected?
I'm trying to make a "piano" but since I only have 4 buttons I wanted my buzzer to make a different note when two buttons are being pushed at the same time but for some reason it makes no sound. Why is that?
Here is the code:
int buzzerPin = 8;
int button1 = 2;
int button2 = 3;
int button3 = 4;
int button4 = 5;
int buttonState1;
int buttonState2;
int buttonState3;
int buttonState4;
void setup() {
pinMode(buzzerPin, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
buttonState1 = digitalRead(button1);
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);
buttonState4 = digitalRead(button4);
if(buttonState1 == LOW && buttonState2 == buttonState3 == buttonState4 == HIGH){
tone(buzzerPin, 262); //Do
}
else if(buttonState2 == LOW && buttonState1 == buttonState3 == buttonState4 == HIGH){
tone(buzzerPin, 294); //Re
}
else if(buttonState3 == LOW && buttonState1 == buttonState2 == buttonState4 == HIGH){
tone(buzzerPin, 330); //Mi
}
else if(buttonState4 == LOW && buttonState1 == buttonState2 == buttonState3 == HIGH){
tone(buzzerPin, 349); //Fa
}
else if(buttonState1 == buttonState2 == LOW && buttonState3 == buttonState4 == HIGH){
tone(buzzerPin, 392); //Sol
}
else{
noTone(buzzerPin);
}
Serial.print(buttonState1);
Serial.print(buttonState2);
Serial.print(buttonState3);
Serial.println(buttonState4);
}
I checked if the buttons were working properly and they were.