source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/10.StarterKit_BasicKit/p08_DigitalHourglass/p08_DigitalHourglass.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

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