I'm having trouble getting basically an alarm clock to keep beeping until my switch is pressed

I am trying to code an alarm clock/hourglass. I have essentially everything, all 6 LEDS lighting up in the allotted time, the buzzer buzzing for how long I want it to, but I can only either get it to buzz once or never ending. I have tried while, for loops, moving around the switchstate code into the while loop. I've exhausted all my resources. Could anyone help?

const int switchPin = 8;
'
unsigned long previousTime = 0; // store the last time an LED was updated
int switchState = 0; // the current switch state
int prevSwitchState = 0; // the previous switch state
int led = 2; // a variable to refer to the LEDs
'
// 600000 = 10 minutes in milliseconds
long interval = 10000; // interval at which to light the next LED
'
void setup() {
// set the LED pins as outputs
for (int x = 2; x < 8; x++) {
pinMode(x, OUTPUT);
}
// set the tilt switch pin as input
pinMode(switchPin, INPUT);
}
'
void loop() {
// store the time since the Arduino started running in a variable
unsigned long currentTime = millis();
'
// compare the current time to the previous time an LED turned on
// if it is greater than your interval, run the if statement
if (currentTime - previousTime > interval) {
// save the current time as the last time you changed an LED
previousTime = currentTime;
// Turn the LED on
digitalWrite(led, HIGH);
// increment the led variable
// in 10 minutes the next LED will light up
led++;
'
if (led == 8) {
tone(12,75,3000)
(1000)
noTone(12)
delay(1000)
// the hour is up
}
}
'
// read the switch value
switchState = digitalRead(switchPin);
'
// if the switch has changed
if (switchState != prevSwitchState) {
// turn all the LEDs low
for (int x = 2; x < 8; x++) {
digitalWrite(x, LOW);
}
'
// reset the LED variable to the first one
led = 2;
'
//reset the timer
previousTime = currentTime;
}
// set the previous switch state to the current state
prevSwitchState = switchState;
}

Please add code tags to your sketch.

Yea, code tags. Please.

Which version of the code will you be posting in code tags?

if (led == 8) {
tone(12,75,3000)
(1000)
noTone(12)
delay(1000)
// the hour is up
}

That code complies?

Sorry, Larry, I'm a new user. I saw the warning about it and thought I did it right, must not have.

Sorry, I'm not familiar with the code tags. trying to figure that out right now. That code only allows it to beep once and won't repeat.

I tried changing that exact code to a while loop and it wont read the button being pressed to reset it

Does OP mean:

if (digitalRead(8) == HIGH) {
     Shut off buzzer;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.