// Source :: Chapter08.Digital Hourglass
// Description :: Digital Hourglass, Chapter 8, Arduino Projects Book
// Programmed by :: Douglas Vieira Ferreira / 03-11-2024
/*
IN THIS PROJECT, I'VE BUILT A DIGITAL HOURGLASS THAT TURNS ON AN LED EVERY TEN MINUTES OR 5 SECONDS.
*/
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
int notes[] = { 330, 660, 750, 900}; // some buzzer notes to choose from
// 600000 = 10 minutes in milliseconds
// 5000 = 5 second intervals
long interval = 5000;
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 == 7){
tone(7, notes[2]);
}
}
// 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;
}