Hello all. Noob here.
I am trying to make the project 8 (digital hourglass) from the Starter Kit, and I'm adding a piezo to sound when the final led lights up.
And it works, except that after a few seconds, the piezo starts buzzing constantly, then wobbles a bit and turns off randomly.
My code is below. I've tried everything. Turning the pin it's connected to low, changing lots of other sections of the code, etc.
So when this runs, each led lights up one at a time, when the 7th is about to light up the piezo buzzes for a second, then stops..... Then if you leave it alone for a few more seconds the piezo starts buzzing again without stopping. This happens even when I set that pin output to LOW. At first I thought maybe there was a tiny bit of voltage slipping through and sort of 'building up' in the piezo, but that doesn't appear to be the case because if you leave the piezo unplugged for the whole time, then plug it in later it buzzes constantly.
So it's like the pin (pin 12 in this case) just decides to turn to HIGH after awhile. Even though nothing in the program is telling it to.
I used a multimeter on that pin, and it is basically 0 until the piezo would start buzzing, at which point it jumps to just under 5v and stays there. Why?
UPDATE!!: I removed the "led++" portion of the code, and replaced it with a series of write2, delay, write 3, delay, etc... This stopped the buzzing. Why? What is the led++ doing to make it turn the pins on?
Would appreciate any help!
const int piezoPin = 12;
unsigned long previousTime =0;
int led = 2;
long interval = 1000;void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);pinMode(piezoPin, OUTPUT);
}void loop(){
unsigned long currentTime = millis();
if(currentTime - previousTime > interval){
previousTime=currentTime;digitalWrite(led,HIGH);
led++;if(led == 7){
pinMode(piezoPin, HIGH);
tone(piezoPin,200,1000);
}
else if (led <=6){
pinMode(piezoPin,LOW);
}}
}