| 1 |
/* |
| 2 |
Arduino Starter Kit example |
| 3 |
Project 8 - Digital Hourglass |
| 4 |
|
| 5 |
This sketch is written to accompany Project 8 in the Arduino Starter Kit |
| 6 |
|
| 7 |
Parts required: |
| 8 |
- 10 kilohm resistor |
| 9 |
- six 220 ohm resistors |
| 10 |
- six LEDs |
| 11 |
- tilt switch |
| 12 |
|
| 13 |
created 13 Sep 2012 |
| 14 |
by Scott Fitzgerald |
| 15 |
|
| 16 |
https://store.arduino.cc/genuino-starter-kit |
| 17 |
|
| 18 |
This example code is part of the public domain. |
| 19 |
*/ |
| 20 |
|
| 21 |
// named constant for the switch pin |
| 22 |
const int switchPin = 8; |
| 23 |
|
| 24 |
unsigned long previousTime = 0; // store the last time an LED was updated |
| 25 |
int switchState = 0; // the current switch state |
| 26 |
int prevSwitchState = 0; // the previous switch state |
| 27 |
int led = 2; // a variable to refer to the LEDs |
| 28 |
|
| 29 |
// 600000 = 10 minutes in milliseconds |
| 30 |
long interval = 600000; // interval at which to light the next LED |
| 31 |
|
| 32 |
void setup() { |
| 33 |
// set the LED pins as outputs |
| 34 |
for (int x = 2; x < 8; x++) { |
| 35 |
pinMode(x, OUTPUT); |
| 36 |
} |
| 37 |
// set the tilt switch pin as input |
| 38 |
pinMode(switchPin, INPUT); |
| 39 |
} |
| 40 |
|
| 41 |
void loop() { |
| 42 |
// store the time since the Arduino started running in a variable |
| 43 |
unsigned long currentTime = millis(); |
| 44 |
|
| 45 |
// compare the current time to the previous time an LED turned on |
| 46 |
// if it is greater than your interval, run the if statement |
| 47 |
if (currentTime - previousTime > interval) { |
| 48 |
// save the current time as the last time you changed an LED |
| 49 |
previousTime = currentTime; |
| 50 |
// Turn the LED on |
| 51 |
digitalWrite(led, HIGH); |
| 52 |
// increment the led variable |
| 53 |
// in 10 minutes the next LED will light up |
| 54 |
led++; |
| 55 |
|
| 56 |
if (led == 7) { |
| 57 |
// the hour is up |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// read the switch value |
| 62 |
switchState = digitalRead(switchPin); |
| 63 |
|
| 64 |
// if the switch has changed |
| 65 |
if (switchState != prevSwitchState) { |
| 66 |
// turn all the LEDs low |
| 67 |
for (int x = 2; x < 8; x++) { |
| 68 |
digitalWrite(x, LOW); |
| 69 |
} |
| 70 |
|
| 71 |
// reset the LED variable to the first one |
| 72 |
led = 2; |
| 73 |
|
| 74 |
//reset the timer |
| 75 |
previousTime = currentTime; |
| 76 |
} |
| 77 |
// set the previous switch state to the current state |
| 78 |
prevSwitchState = switchState; |
| 79 |
} |