]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Chapter4/APFD_Chapter4.ino
first commit
[arduino] / books / pdummies / APFD_Chapter4 / APFD_Chapter4.ino
1 /* Arduino Projects for Dummies
2  * by Brock Craft 
3  *
4  * Chapter 4: The All-Seeing Eye
5  * Sequentially lights up a series of LEDs
6  *
7  * v0.3 02.02.2015
8  * Changed delayTime variable so that is is consistent with the 
9  * text of the book. (Used to be timeDelay.)
10  * v0.2 03.07.2013
11  * Fixed error: timeChanged = millis(); not within if loop brackets
12 */
13
14 // A variable to set a delay time between each LED
15 int delayTime = 40; 
16
17 // A variable to store which LED we are currently working on
18 int currentLED = 4;
19
20 // A variable to store the direction of travel of LEDs
21 int dir = 1;
22
23 // A variable to store the last time we changed something
24 unsigned long timeChanged = 0;
25
26 // Create an array to hold the value for each LED pin
27 byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
28
29 void setup() {
30   // Set all pins for OUTPUT
31   for (int x=0; x<10; x++) {
32     pinMode(ledPin[x], OUTPUT); 
33   }
34
35  // Set up the 
36   timeChanged = millis();
37 }
38
39 void loop() {
40   // Check whether it has been long enough
41   if ((millis() - timeChanged) > delayTime) {
42
43     // Turn off all of the LEDs
44     for (int x=0; x<10; x++) {
45       digitalWrite(ledPin[x], LOW);
46     }
47
48     // Turn on the current LED
49     digitalWrite(ledPin[currentLED], HIGH);
50
51     // Increment by the direction value
52     currentLED += dir;
53
54     // If we are at the end of a row, change direction
55     if (currentLED == 9) {
56       dir = -1;
57     }
58     if (currentLED == 0) {
59       dir = 1;
60     }
61   
62
63   // Store the current time as the time we last changed LEDs
64   timeChanged = millis();
65   }
66 }
67
68