-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspark.ino
212 lines (162 loc) · 5.11 KB
/
spark.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Flare spark demo - https://github.com/baird/flare
////////////////////////////////////////////////////////////////////////////////////////////////////////
// color value storage
int rval = 0;
int gval = 0;
int bval = 0;
// color into string storage
char colorStr[64];
char color[64];
// score into string storage
char scoreStr[64];
int score = 0;
// individual color increments
int r = 0;
int g = 0;
int b = 0;
// timekeeping
unsigned long prevMillis = 0;
unsigned long currentMillis = 0;
// score level ints
const int levelSeven = 150; // purple
const int levelSix = 125; // blue
const int levelFive = 100; // aqua
const int levelFour = 75; // green
const int levelThree = 50; // yellow
const int levelTwo = 25; // orange
const int levelOne = 0; // red (zero so it starts off red instead of dark)
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Setup
////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
pinMode(A6, OUTPUT);
pinMode(A5, OUTPUT);
pinMode(A4, OUTPUT);
// set exposed spark variables
Spark.variable("rval", &rval, INT);
Spark.variable("gval", &gval, INT);
Spark.variable("bval", &bval, INT);
Spark.variable("score", &score, INT);
// delays for 20 seconds to give system led time to show connected wifi status (breathing cyan)
delay(20000);
// take control of system RGB
RGB.control(true);
// set system LED to zero
RGB.color(rval, gval, bval);
}
void loop() {
////////////////////////////////////////////////////////////////////////////////////////////////////////
// add to score to test
////////////////////////////////////////////////////////////////////////////////////////////////////////
if (score < 5000) {
score += 5;
delay(5000);
// push score
sprintf(colorStr, "%s", color);
Spark.publish("currentColor", colorStr);
sprintf(scoreStr, "%i", score);
Spark.publish("currentScore", scoreStr);
// console if needed
// Serial.println("Score");
// Serial.println(score);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check on score once every 5 seconds and update LED
////////////////////////////////////////////////////////////////////////////////////////////////////////
currentMillis = millis();
if ((currentMillis - prevMillis) >= 5000) {
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Score to color rules
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rules for LevelOne (Red)
if (((score >= levelOne) && (score < levelTwo)) && ( rval != 255 )) {
for (int r=0; r <= 255; r++) {
RGB.color(rval, gval, bval);
rval = r;
delay(20);
prevMillis = currentMillis;
strcpy(color, "Red!");
}
}
// Rules for LevelTwo (Orange)
else if (((score >= levelTwo) && (score < levelThree)) && (gval != 40) ) {
for (int g=0; g <= 40; g++) {
RGB.color(rval, gval, bval);
gval = g;
delay(20);
prevMillis = currentMillis;
strcpy(color, "Orange!");
}
}
// Rules for LevelThree (Yellow)
else if (((score >= levelThree) && (score < levelFour)) && (gval != 140) ) {
for (int g=40; g <= 140; g++) {
RGB.color(rval, gval, bval);
gval = g;
delay(20);
prevMillis = currentMillis;
strcpy(color, "Yellow!");
}
}
// Rules for LevelFour (Green)
else if (((score >= levelFour) && (score < levelFive)) && (gval != 255) ) {
for (int g=140; g <= 255; g++) {
RGB.color(rval, gval, bval);
gval = g;
delay(20);
prevMillis = currentMillis;
strcpy(color, "Green!");
}
for (int r=255; r >= 0; r--) {
RGB.color(rval, gval, bval);
rval = r;
delay(20);
prevMillis = currentMillis;
}
}
// Rules for LevelFive (Aqua)
else if (((score >= levelFive) && (score < levelSix)) && (bval != 253) ) {
for (int b=0; b <= 253; b++) {
RGB.color(rval, gval, bval);
bval = b;
delay(20);
prevMillis = currentMillis;
strcpy(color, "Aqua!");
}
}
// Rules for LevelSix (Blue)
else if (((score >= levelSix) && (score < levelSeven)) && (bval != 254) ) {
for (int b=253; b <= 254; b++) {
RGB.color(rval, gval, bval);
bval = b;
delay(20);
prevMillis = currentMillis;
strcpy(color, "Blue!");
}
for (int g=255; g >= 0; g--) {
RGB.color(rval, gval, bval);
gval = g;
delay(20);
prevMillis = currentMillis;
}
}
// Rules for LevelSeven (Purple)
else if ((score >= levelSeven) && (bval != 255)) {
for (int b=254; b <= 255; b++) {
RGB.color(rval, gval, bval);
bval = b;
delay(20);
prevMillis = currentMillis;
strcpy(color, "Purple!");
}
for (int r=0; r <= 105; r++) {
RGB.color(rval, gval, bval);
rval = r;
delay(20);
prevMillis = currentMillis;
}
}
}
}