-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRgbStrip.ino
252 lines (233 loc) · 5.71 KB
/
RgbStrip.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
*
* Copyright (C) 2016 Pedro Ruiz
*
* This file is part of rgbStrip.
*
* rgbStrip is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* rgbStrip is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rgbStrip. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <Arduino.h>
#include "RgbController.h"
#include "LedController.h"
//Initializate strip
RgbController strip = RgbController(3,9,10);
LedController led = LedController(4);
rgbColor white = {255,255,255};
rgbColor black = {0,0,0};
//Full rainbow colors -
#define CHROMATICCOUNT 6
rgbColor chromaticCircle[CHROMATICCOUNT] =
{
{0,255,0}, //Green
{255,255,0}, //Yellow
{255,0,0}, //Red
{255,0,255}, //Magenta
{0,0,255}, //Blue
{0,255,255}, //Cyan
};
enum rgbModes
{
M_STATICCOLOR,
M_RAINBOW,
M_STRIKE,
M_RANDOMSTRIKE,
M_BREATHING,
M_MAXMODES
};
void setup()
{
Serial.begin(9600);
//Switch off the strip when starting.
strip.setRGB(black);
//Setting frequency to max for no blinks on led.
TCCR1B = TCCR1B & 0b11111000 | 0x01; //10 / 9 -> 31250Hz
TCCR2B = TCCR2B & 0b11111000 | 0x01; //3 -> 31250Hz
}
//Used for process modes
int step = 0;
unsigned long time = 0;
rgbColor lcolor;
long duration = 1200;
rgbModes mode = M_STATICCOLOR;
void commandParser()
{
while(Serial.available() > 0)
{
switch(Serial.read())
{
//Turn on/off secondary light
case 'l':
{
switch(Serial.parseInt())
{
case 0:
led.stopStrike();
led.turnOff();
break;
case 1:
led.stopStrike();
led.turnOn();
break;
case 2:
led.strike(duration);
break;
case 3:
if(!led.getState())
led.turnOn();
else
led.turnOff();
break;
}
}break;
//Colors , send a complete rgb color and with smoothchange mode if selected
case 'c':
{
rgbColor motion;
if(Serial.parseInt() == 1)
{
if(strip.isAnimationRunning() == A_NOTHING)
{
motion.r = Serial.parseInt();
motion.g = Serial.parseInt();
motion.b = Serial.parseInt();
strip.startAnimation(A_SMOOTHCHANGE, duration, motion);
}
}else{
motion.r = Serial.parseInt();
motion.g = Serial.parseInt();
motion.b = Serial.parseInt();
strip.setRGB(motion);
}
}break;
//duration of all animations or things that depend on time
case 'd':
{
duration = Serial.parseInt();
}break;
//Change brithgness of the strip
case 'a':
{
strip.setBrightness((byte)Serial.parseInt());
}break;
//Set color red
case 'r':
{
strip.setRed((byte)Serial.parseInt());
mode = M_STATICCOLOR;
}break;
//Set color green
case 'g':
{
strip.setGreen((byte)Serial.parseInt());
mode = M_STATICCOLOR;
}break;
//Set color blue
case 'b':
{
strip.setBlue((byte)Serial.parseInt());
mode = M_STATICCOLOR;
}break;
//Set a mode
case 'm':
{
step = 0;
time = 0;
lcolor.r = strip.getRGB().r;
lcolor.b = strip.getRGB().b;
lcolor.g = strip.getRGB().g;
mode = Serial.parseInt();
strip.stopAnimation();
}break;
//Set mode to STATICCOLOR and turnoff the strip
case 's':
{
mode = M_STATICCOLOR;
strip.startAnimation(A_SMOOTHCHANGE, duration, black);
}break;
}
}
}
void loop()
{
//Check if is necesary to process some animation
//non-block
strip.processAnimation();
//Check if necesary update the state of led
led.processStrike();
//Read and parse commands from the bluetooth module
commandParser();
//Execute mode
switch(mode)
{
case M_RAINBOW:
{
if(strip.isAnimationRunning() == A_NOTHING)
{
strip.startAnimation(A_SMOOTHCHANGE, duration, chromaticCircle[step++]);
if(step > CHROMATICCOUNT-1)
step = 0;
}
}break;
case M_STRIKE:
{
//If it is the time for update
if(time < millis())
{
if(step)
{
step = 0;
time = (2.0/3.0)*duration+ millis(); //Time off is 2/3 the time on
strip.setRGB(lcolor);
}else{
step = 1;
time = duration + millis();
strip.setRGB(black);
}
}
}break;
case M_RANDOMSTRIKE:
{
//If it is the time for update
if(time < millis())
{
int newcolor;
//Get when the next step is needed
time = millis()+duration;
//Check if it is not the same color
do
{
newcolor = random(CHROMATICCOUNT-1);
}while(step == newcolor);
step = newcolor;
strip.setRGB(chromaticCircle[step]);
}
}break;
case M_BREATHING:
{
if(strip.isAnimationRunning() == A_NOTHING)
{
if(step)
{
strip.startAnimation(A_SMOOTHCHANGE, duration, lcolor);
step = 0;
}else
{
strip.startAnimation(A_SMOOTHCHANGE, duration, black);
step = 1;
}
}
}break;
}
}