# IF Statement continues execute even when false

**URL:** <https://forum.arduino.cc/t/if-statement-continues-execute-even-when-false/306426>\
**Category:** Programming\
**Created:** [April 22, 2015, 12:44am UTC](https://forum.arduino.cc/t/if-statement-continues-execute-even-when-false/306426 "2015-04-22T00:44:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2015, 12:44am UTC](https://forum.arduino.cc/t/if-statement-continues-execute-even-when-false/306426/1 "2015-04-22T00:44:29Z")

</div>

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:

```cpp
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.

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [April 22, 2015, 12:52am UTC](https://forum.arduino.cc/t/if-statement-continues-execute-even-when-false/306426/2 "2015-04-22T00:52:00Z")

</div>

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.

```cpp
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.

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [April 22, 2015, 12:54am UTC](https://forum.arduino.cc/t/if-statement-continues-execute-even-when-false/306426/3 "2015-04-22T00:54:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michinyon](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@michinyon](https://forum.arduino.cc/u/michinyon)\
**Post date:** [April 22, 2015, 12:54am UTC](https://forum.arduino.cc/t/if-statement-continues-execute-even-when-false/306426/4 "2015-04-22T00:54:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2015, 6:29pm UTC](https://forum.arduino.cc/t/if-statement-continues-execute-even-when-false/306426/5 "2015-04-22T18:29:51Z")

</div>

@ 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 --

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 22, 2015, 6:39pm UTC](https://forum.arduino.cc/t/if-statement-continues-execute-even-when-false/306426/6 "2015-04-22T18:39:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:14pm UTC](https://forum.arduino.cc/t/if-statement-continues-execute-even-when-false/306426/7 "2021-05-05T21:14:59Z")

</div>

Closed on May 5, 2021, 9:14pm UTC


