bump
What have you tried?
You want, under some circumstances, to turn the noise maker on.
You want, under some circumstances, to turn the noise maker off.
Turning the noise maker off if it is not on is not a problem. Testing that it is on is simple, though, so turning it off only when it is on is easy.
Notice that turning the noise maker off has little to do with turning it on.
Create a function, makeSomeNoise(). Call it from loop().
Create another function, shutUpStupid(). Call it from loop().
In one, turn the noise maker on, it appropriate.
In the other, turn the noise maker off, if appropriate (and if needed).
This way, you won't be tempted to try to put too much stuff in one function, which seems to be the problem you are having now.