Turn off piezospeaker(alarm) permanently when push button is pressed

Hello,

I'm trying to figure out a way to turn off the tone being played by the speaker, which acts an alarm, when the push button is pressed. As of now, I am using the example sketch called ToneMelody in the void loop() so that the tone, which acts as an alarm continue to repeat itself. I added an if statement where if the push button is pressed, then the alarm will turn off. It works, but once the push button is released, the tone begins to play again. Any advice on how to make sure the tone doesn't play again after the push button is pressed?

Thanks

Make a boolean variable and set it to true when you want the speaker to play and false when you don't want the speaker to play. Separate the speaker code from the button code. The button code should set that variable to true or false. The speaker code needs an if to check if it is true and if so play the speaker.

Pseudocode:

boolean playSpeaker = false;  // at global scope


loop:
   if the alarm condition is true set playSpeaker to true
   if the button is pressed set playSpeaker to false

   if playSpeaker == true play the speaker

another way is to have the bit of code that runs when you press the button change a value
then next time the script loops it checks to see if silence is 0 or 1 before making the sound. So if button has been pressed to silence it, it will not play the sound.

Below is psudo-code but should be easly set up.

In code that runs when button is pressed:

if button == high
silence = 1

and have the part that plays the alarm sound check at it's start:

if silence == 0
{
code that plays the melody/noise/alarm
}

another way

That sounds like the same way to me.