]> git.piffa.net Git - sketchbook_andrea/blob - advanced_projects/kit_knight_rider_10LED_array_iteration_1/kit_knight_rider_10LED_array_iteration_1.ino
RTOS
[sketchbook_andrea] / advanced_projects / kit_knight_rider_10LED_array_iteration_1 / kit_knight_rider_10LED_array_iteration_1.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 = 50; 
16
17 // A variable to store which LED we are currently working on
18 int currentLED = 0;
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[] = {
28   4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
29
30 void setup() {
31   // Set all pins for OUTPUT  with a counter
32   for (int x=0; x<10; x++) {
33     pinMode(ledPin[x], OUTPUT); 
34   }
35
36   // Set all pins for OUTPUT iterating a pointer
37   // byte *ptr = ledPin ;
38   // while (*ptr) {
39   //   pinMode(*ptr++, OUTPUT);
40   // }
41
42   // Set up the 
43   timeChanged = millis();
44 }
45
46 void loop() {
47   // Check whether it has been long enough
48   if ((millis() - timeChanged) > delayTime) {
49     // Turn off all of the LEDs, see next comment
50     for (int x=0; x<10; x++) {
51       digitalWrite(ledPin[x], LOW);
52     }
53     // Turning off just one LED instead on 10 would be more efficient
54     // digitalWrite(ledPin[currentLED - dir], LOW);
55
56     // Turn on the current LED
57     digitalWrite(ledPin[currentLED], HIGH);
58
59     // Increment by the direction value
60     currentLED += dir;
61
62     // If we are at the end of a row, change direction
63     if (currentLED == 9) {
64       dir = -1;
65     }
66     if (currentLED == 0) {
67       dir = 1;
68     }
69
70     // Let's change the speed wit a Pot on pin A5
71     // delayTime = map(analogRead(5),0,1024,20,5 00);
72     // delay(2);
73
74     // Store the current time as the time we last changed LEDs
75     timeChanged = millis();
76   }
77 }
78
79
80
81
82
83
84