Hello,
I've tried to find what I could on the forums regarding this without spending my whole night, so I apologize if this is repetitive.
I am trying to build a musical keyboard that produces tones from push buttons. When no buttons are pressed, I would like no sound. I am only using two buttons at the moment for testing my code.
I am using the code below for testing:
void setup()
{
Serial.begin(9600);
pinMode(52, INPUT);
pinMode(53, INPUT);
}
void loop()
{
Serial.print(digitalRead(53));
Serial.print(digitalRead(52));
Serial.println(" ");
if (digitalRead(53) == HIGH){
tone(12, 261);
}
if (digitalRead(52) == HIGH){
tone(12, 392);
}
}
The operation goes as follows:
-Initially, when the buttons are unpressed, no sound is produced. Perfect --
-When I press either of the two buttons, the tone corresponding to that buttonstates IF statement is executed - again perfect.
-However, when I let go, the tone continues to play until I either press the on board RESET button, or until I press the other push button. And when I press the other push button the same problem occurs.
What I've already tried:
- A different button.
- I've verified the button state with Serial.print - all good.
- I've tried adding short delays after the button state is read in (by assigning the digitalRead(x) to a variable, delaying, and then checking the state of the variable).
- Else statements were successful at stopping the tone when the button was released, but created a bias to one button over the other when played simultaneously, or "rolling the keys" where the next button is pressed before the other is released.
Any suggestions? Thanks in advance.