IF Statement continues execute even when false

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.

How do you have the buttons wired? I presume the buttons are connected to 5v. Do you have pulldown resistors on those pins also?

And you also need to call noTone() to stop the tone.

if (digitalRead(53) == HIGH){
  tone(12, 261);
}
else if (digitalRead(52) == HIGH){
   tone(12, 392);
}
else noTone(12);

edit: My bad. I forgot the pin on noTone.

So where in your code do you ever do anything to turn the tone off? You turn it on when you see a button press, but nowhere do you turn it off when the button is no longer pressed. The IF is doing exactly what your code says to do....

Regards,
Ray L.

It might not be a problem with your buttons. You need to check what that tone( ) function is doing.

@ SurferTim:
The buttons are wired with 5V and pull down resistors at a value of 10 K ohm. I'll try the code you posted in a bit - but I seem to remember the else if's and else statements producing that bias I mentioned when "rolling the keys"

@ RayLivingston:
Introducing the noTone() function causes a very bad distortion, making the tone unrecognizable. I've tried it inside an IF block, in a separate IF block (if digialRead(52) == LOW), and also at the very end of the loop. In each case the distortion occurs.

@michinyon:
Beyond changing the parameters of tone() and observing what happens, what else could I do to check what that function is doing?

Thanks guys --

So, you changed your code, and you want us to guess what changes you made. I'll pass.