Clicking Piezo

I have a very simple project - Im detecting a 12V voltage (I will step it down to 5v) For the purpose of the project Im using a POT to test the code in analogue 0 - It simply allows me to turn the voltage in on and off.

What should happen is that the tone gets generated for 1 second when the voltage is over a certain amount, then programme sits there doing nothing until the voltage stops to zero and resets. The voltage comes back and you get another beep.

Should be really simple but I must have some logic flow wrong

I can make it work with the following code -- However I get a click from the piezo every time I go around the loop even though Im putting in the NoTone command.

Any ideas on how to get rid of the little click sound ?

Much appreciation if I can get a pointer on this ..


int checkState = 0;

void setup() { // put your setup code here, to run once: Serial.begin(9600);


void loop() { // put your main code here, to run repeatedly: int sensorReading = analogRead(A0);

if (checkState >=1) tone(9, 2000, 1000); if (checkState >=2) noTone(9); delay (1000); if (sensorReading > 1000) checkState = checkState + 1; else checkState = 0; Serial.println(checkState);


if (checkState >=1) tone(9, 2000, 1000);
if (checkState >=2) noTone(9);

if checkState is greater than 1, both of these lines will be true. You probably want to use "==" instead of ">=".

Of course - good spot - I could not see it for looking at it -- Thanks