Could you please review the code? It appears to have a malfunctioning function. According to the code, the buzzer is designed to sound only when the Threshold value is high, and it will stop when the value is low. However, after the Threshold value has been high once, the buzzer does not stop sounding. Initially, everything works normally, but the problem arises when the Threshold value returns to normal, and the buzzer continues to sound. Can you suggest a solution to this issue?
Do you have a data sheet for the buzzer or a link to where you bought it
Can you post a simple schematic of how you have the buzzer connected to the Mega
Connect the red wire to pin 7 and the black wire to GND.
Use digitalWrite(7, HIGH) to turn on and digitalWrite(7, LOW) to turn off
Don't forget to uncomment the pinMode (7, OUTPUT)
That is your (other) problem. You have it wired to be on when the pin is LOW. So when you call noTone(7), you are actually turning it on, not off.
You have some code problems. After auto formatting it becomes obvious that you have extra brackets, also you can factor some code since it's in both the 'if' and 'else' clause.
With an active buzzer, yes.
A passive piezo doesn't care if a DC voltage is left on it.
But yes, it's better to wire the "whatever it is" between pin and ground.
Leo..
Not so, as far as I remember... at least not all piezo. Some don't like extended periods of DC.
Notice (Handling)
...
If DC voltage is applied to the component, silver migration
may occur. Please pay full attention to avoid subjecting
the component to DC voltage for long periods