I am working on the projects from the arduino starter kit book. I am on project 8 the hour glass and at the end it suggests adding in a buzzer. The problem I am having is when i use a while loop within the given code, the reset code no longer works.
const int switchPin = 8;
unsigned long previousTime = 0;
int buzzer = 9;
int switchState = 0;
int prevSwitchState = 0;
int led = 2;
long interval = 1000;
void setup() {
for(int x = 2;x<8;x++){
pinMode(x, OUTPUT);
}
pinMode(buzzer, OUTPUT);
pinMode(switchPin, INPUT);
}
void loop(){
unsigned long currentTime = millis();
if(currentTime - previousTime > interval) {
previousTime = currentTime;
digitalWrite(led, HIGH);
led++;
while(led == 8){
tone(9, 1000, 260);
delay(0);
tone(9, 1000, 260);
delay(3000);
}
}
switchState = digitalRead(switchPin);
if(switchState != prevSwitchState){
for(int x = 2;x<8;x++){
digitalWrite(x, LOW);
noTone(buzzer);
}
led = 2;
previousTime = currentTime;
}
prevSwitchState = switchState;
}
My main goal is to have the buzzer go off every 3 seconds after the last led is on. If i change the while to an If(led == 8) then the reset works how it is supposed to but the buzzer does not continue like Id like it too.
Very new to using arduino and codding but Thank you!!