]> git.piffa.net Git - arduino/blob - books/pdummies/APFD_Web_Extras_Part_2/APFD_Web_Extras_Part_2.ino
first commit
[arduino] / books / pdummies / APFD_Web_Extras_Part_2 / APFD_Web_Extras_Part_2.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.1 30.04.2013
8 */
9
10 // A variable to set a delay time between each LED
11 int timeDelay(40); 
12
13 // A variable to store which LED we are currently working on
14 int currentLED = 4;
15
16 // A variable to store the direction of travel of LEDs
17 int dir = 1;
18
19 // A variable to store the last time we changed something
20 unsigned long timeChanged = 0;
21
22 // Create an array to hold the value for each LED pin
23 byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
24
25 void setup() {
26   // Set all pins for OUTPUT
27   for (int x=0; x<10; x++) {
28     pinMode(ledPin[x], OUTPUT); 
29   }
30
31  // Set up the 
32   timeChanged = millis();
33 }
34
35 void loop() {
36   // Check whether it has been long enough
37   if ((millis() - timeChanged) > timeDelay) {
38
39     // Turn off all of the LEDs
40     for (int x=0; x<10; x++) {
41       digitalWrite(ledPin[x], LOW);
42     }
43
44     // Turn on the current LED
45     digitalWrite(ledPin[currentLED], HIGH);
46
47     // Increment by the direction value
48     currentLED += dir;
49
50     // If we are at the end of a row, change direction
51     if (currentLED == 9) {
52       dir = -1;
53     }
54     if (currentLED == 0) {
55       dir = 1;
56     }
57   }
58
59   // Store the current time as the time we last changed LEDs
60   timeChanged = millis();
61 }
62
63