-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlights.ino
157 lines (136 loc) · 4.29 KB
/
lights.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// constants won't change. Used here to set a pin number :
const int starttime = 100; // the amount of time that each delay is going to
// have on the startup script.
const int NUM_OUTPUTS = 14;
long randNumber;
long NumberCounter; // This is a counter to count how may times the program has
// looped before it triggers a function.
// Variables will change :
int ledState[NUM_OUTPUTS] = {LOW};
// Set what pins are enabled. (Low = Off, High = On)
int ledEnabled[NUM_OUTPUTS] = {LOW};
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 500; // interval at which to blink (milliseconds)
void setup() {
ledEnabled[8] = HIGH;
ledEnabled[9] = HIGH;
ledEnabled[10] = HIGH;
// Get a random seed from analog 0
randomSeed(analogRead(0));
// set the digital pin as output:
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
// This is the light test section, we will run though all of the different
// functions here.
// Chase both on and off.
Chase(starttime, HIGH);
Chase(starttime, LOW);
Blink(10, starttime); // Blink 10 times.
}
void loop() {
// Used for timed events.
if (NumberCounter >
1000) // Resets the number counter if it is larger then 1000.
{
NumberCounter = 0;
} else if ((NumberCounter == 100) || (NumberCounter == 300) ||
(NumberCounter == 500) || (NumberCounter == 700) ||
(NumberCounter == 900)) {
LightsOff();
Chase(starttime, HIGH);
Chase(starttime, LOW);
} else if ((NumberCounter == 200) || (NumberCounter == 400) ||
(NumberCounter == 600) || (NumberCounter == 800) ||
(NumberCounter == 1000)) {
LightsOff();
Blink(5, 1000);
}
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
NumberCounter++; // Add one to the number counter.
// set the LED with the ledState of the variable:
RandomLight();
}
}
void RandomLight() {
int randstatus = 0;
do {
randNumber = random(0, NUM_OUTPUTS); // Random number to generate what pin should be
// on/off. Add 1 to your final number. Eg. 13
// pins would be a total of 14.
for(int i=0; i < NUM_OUTPUTS; i++){
if ((randNumber == i) && (ledEnabled[i] == HIGH)) {
randstatus = 1;
if (ledState[i] == LOW) {
ledState[i] = HIGH;
} else {
ledState[i] = LOW;
}
digitalWrite(i, ledState[i]);
}
}
} while (randstatus == 0); // Keep looping until we have a random number that
// will turn an output on or off.
}
void LightsOn() // Turns all pins on.
{
int i;
for(i = 0; i < NUM_OUTPUTS; i++) {
if (ledEnabled[i] == HIGH) {
digitalWrite(i, HIGH);
}
}
}
void LightsOff() // Turns all pins off
{
int i;
for(i = 0; i < NUM_OUTPUTS; i++) {
if (ledEnabled[i] == HIGH) {
digitalWrite(i, LOW);
}
}
}
void Blink(int times, int delays) // Times = how many times the outputs should
// blink, delays = how long between the
// on/off
{
int i;
for (i = 0; i < times; i++) {
LightsOn();
delay(delays);
LightsOff();
delay(delays);
}
}
void Chase(int delays, int type) // Delays = How much of a delay between each
// pin, type = High/Low for On/Off
{
int i;
for(i = 0; i < NUM_OUTPUTS; i++) {
if (ledEnabled[i] == HIGH) {
digitalWrite(i, type);
delay(delays);
}
}
}