/
Arduino-RGB-Mood-Lamp.ino
453 lines (378 loc) · 11.9 KB
/
Arduino-RGB-Mood-Lamp.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*********************************************************************************/
/* Moodlamp - Arduino Powered Moodlamp
/* by Alan Pullen
/* Copyright ©2015. Alan Pullen.
/* See LICENSE.MD for details
/*
/* Version 0.1
/*
/* Based on examples by Nico Hood / FastLED
/*
/*********************************************************************************/
#include <IRLremote.h> // Lightweight IR Remote Library
#include <FastLED.h> // FastLED Library: http://fastled.io
#include <MSGEQ7.h> // NicoHood MSGEQ7 Library: http://nicohood.wordpress.com/
//================================================================================
// IR Remote Control Config
//================================================================================
const int interruptIR = digitalPinToInterrupt(2); // Interrupt Pin
uint8_t IRProtocol = 0; // Stores IR Protocol ID Number
uint16_t IRAddress = 0; // Stores IR Command Address
uint32_t IRCommand = 0; // Stores IR Command Code
//================================================================================
// MSGEQ7 Config
//================================================================================
const int pinAnalogIn = A0; // DC Multiplexed Input for MSGEQ7
const int pinReset = 5; // Reset Pin for MSGEQ7
const int pinStrobe = 4; // Strobe Pin for MSGEQ7
//================================================================================
// IR Remote Control Codes (LED618 Remote Control, NEC Protocol)
//================================================================================
const uint16_t irAddr = 0xFF00; // Prevents over NEC remotes interfering
const uint32_t irCmdInc = 0xFA05;
const uint32_t irCmdDec = 0xFB04;
const uint32_t irCmdOff = 0xF906;
const uint32_t irCmdOn = 0xF807;
const uint32_t irCmdFlash = 0xF00F;
const uint32_t irCmdStrobe = 0xE817;
const uint32_t irCmdFade = 0xE41B;
const uint32_t irCmdSmooth = 0xEC13;
const uint32_t irCmdRed = 0xF609;
const uint32_t irCmdGreen = 0xF708;
const uint32_t irCmdBlue = 0xF50A;
const uint32_t irCmdWhite = 0xF40B;
const uint32_t irCmdOrangeA = 0xF20D;
const uint32_t irCmdOrangeB = 0xEA15;
const uint32_t irCmdOrangeC = 0xE619;
const uint32_t irCmdYellow = 0xEE11;
const uint32_t irCmdGreenA = 0xF30C;
const uint32_t irCmdBlueA = 0xEB14;
const uint32_t irCmdBlueB = 0xE718;
const uint32_t irCmdBlueC = 0xEF10;
const uint32_t irCmdBlueD = 0xF10E;
const uint32_t irCmdPurpleA = 0xE916;
const uint32_t irCmdPurpleB = 0xE51A;
const uint32_t irCmdPurpleC = 0xED12;
//================================================================================
// Modes - Not really needed - makes code easier to read
//================================================================================
const uint8_t modeStatic = 1;
const uint8_t modeFade = 2;
const uint8_t modeMusic = 3;
const uint8_t modeFire = 4;
const uint8_t modeSmooth = 5;
const uint8_t modeSunSet = 6;
const uint8_t modeSunRise = 7;
const uint8_t modeOn = 8;
const uint8_t modeOff = 9;
uint8_t currMode = 2; // Currently selected mode
//================================================================================
// FastLED Config
//================================================================================
const int numLeds = 24;
const int ledPin = 6;
const int brightness = 255;
CRGB leds[numLeds];
uint32_t myColour = CRGB::Black;
TBlendType currentBlending;
CRGBPalette16 currentPalette;
//================================================================================
// Other Vars
//================================================================================
const int pwrPin = 7;
const int pwrLed = 8;
const int sunriseTime = 3500; // Delay in ms between steps, 256 steps total.
const int sunsetTime = 2000; // Delay in ms between steps, 256 steps total.
const int fireCooling = 100;
const int fireSparking = 200;
int resetMode = 0; // Used to reset a mode or increment colour cycler
//================================================================================
// First Run / System Setup and Initialisation
//================================================================================
void setup()
{
//Serial.begin(9600);
//Serial.println("Starting up ...");
pinMode(pwrLed, OUTPUT);
digitalWrite(pwrLed, 1);
pinMode(pwrPin, OUTPUT);
digitalWrite(pwrPin, 0); // Keeps PWR pin low
// Start lightweight IR receiver code.
IRLbegin<IR_ALL>(interruptIR);
// Start MSGEQ7
MSGEQ7.begin(pinReset, pinStrobe, pinAnalogIn);
// Start FastLED Library and configure to use Adafruit Neopixels.
FastLED.addLeds<NEOPIXEL, ledPin>(leds, numLeds).setCorrection(TypicalSMD5050);
FastLED.setDither( 0 );
currentPalette = ForestColors_p;
currentBlending = BLEND;
}
// Cycles through hues on button press.
void ColourCycler(int dir = 1)
{
static uint8_t hue = 0;
if(dir > 0) {hue = hue + resetMode;}
else {hue = hue - resetMode;}
FastLED.showColor(CHSV(hue, 255, 255));
delay(25);
}
//================================================================================
// Main Program Loop
//================================================================================
void loop()
{
// Process IR Commands
resetMode = 0;
uint8_t oldSREG = SREG;
cli();
if (IRProtocol && (IRAddress == irAddr))
{
//Serial.println(IRProtocol, HEX);
//Serial.println(IRAddress, HEX);
//Serial.println(IRCommand, HEX);
switch(IRCommand)
{
case irCmdInc:
currMode = modeSunRise;
resetMode = 1;
break;
case irCmdDec:
currMode = modeSunSet;
resetMode = 1;
break;
case irCmdOff:
currMode = modeOff;
resetMode = 1;
break;
case irCmdOn:
currMode = modeOn;
resetMode = 1;
break;
case irCmdFlash:
currMode = modeFire;
break;
case irCmdStrobe:
currMode = modeMusic;
break;
case irCmdFade:
currMode = modeFade;
resetMode = 1;
break;
case irCmdSmooth:
currMode = modeSmooth;
resetMode = 1;
break;
case irCmdRed:
currMode = modeStatic;
myColour = CRGB::Red;
break;
case irCmdGreen:
currMode = modeStatic;
myColour = CRGB::Green;
break;
case irCmdBlue:
currMode = modeStatic;
myColour = CRGB::Blue;
break;
case irCmdWhite:
currMode = modeStatic;
myColour = CRGB::White;
break;
case irCmdOrangeA:
currMode = modeStatic;
myColour = CRGB::DarkOrange;
break;
case irCmdOrangeB:
currMode = modeStatic;
myColour = CRGB::OrangeRed;
break;
case irCmdOrangeC:
currMode = modeStatic;
myColour = CRGB::Orange;
break;
case irCmdYellow:
currMode = modeStatic;
myColour = CRGB::Yellow;
break;
case irCmdGreenA:
currMode = modeStatic;
myColour = CRGB::LightGreen;
break;
case irCmdBlueA:
currMode = modeStatic;
myColour = CRGB::LightBlue;
break;
case irCmdBlueB:
currMode = modeStatic;
myColour = CRGB::Cyan;
break;
case irCmdBlueC:
currMode = modeStatic;
myColour = CRGB::SkyBlue;
break;
case irCmdBlueD:
currMode = modeStatic;
myColour = CRGB::MidnightBlue;
break;
case irCmdPurpleA:
currMode = modeStatic;
myColour = CRGB::DarkViolet;
break;
case irCmdPurpleB:
currMode = modeStatic;
myColour = CRGB::MediumPurple;
break;
case irCmdPurpleC:
currMode = modeStatic;
myColour = CRGB::Purple;
break;
default:
///Serial.println("Command Not Recognised!!!");
break;
}
IRProtocol = 0;
}
SREG = oldSREG;
//Serial.println(currMode);
// Process Modes
switch(currMode)
{
// Static Colour Display
case modeStatic:
FastLED.showColor(myColour);
delay(100);
break;
// Rainbow Fader
case modeFade:
RainbowFader();
break;
case modeMusic:
Music();
break;
case modeFire:
Fire();
break;
case modeSmooth:
Smooth();
break;
case modeSunRise:
SunRise();
break;
case modeSunSet:
SunSet();
break;
case modeOn:
ColourCycler(1);
break;
case modeOff:
ColourCycler(0);
break;
}
//delay(1000);
}
void Fire()
{
currentPalette = CRGBPalette16( CRGB::Maroon, CRGB::Red, CRGB::OrangeRed, CRGB::DarkOrange);
for(uint8_t x = 0; x <numLeds; x++)
{
leds[x] = ColorFromPalette( currentPalette, random8(), brightness, currentBlending);
}
// Add some flickering
if( random8() < 64) {
leds[ random16(numLeds)] += CRGB::Yellow;
}
FastLED.show(); // display this frame
delay(75);
}
void Music()
{
// read without delay
static long prevMillis = 0;
unsigned long currentMillis = millis();
uint8_t bandData[6];
if (currentMillis - prevMillis > 20)
{
prevMillis = currentMillis;
MSGEQ7.read();
for(int i = 0; i < 6; i++)
{
bandData[i] = MSGEQ7.get(i,MSGEQ7_LEFT);
}
for (int i = 0; i < 8; i++)
{
leds[i].setHSV(HUE_RED, 255, max(bandData[0],bandData[1]));
leds[i + 8].setHSV(HUE_GREEN, 255, max(bandData[2],bandData[3]));
leds[i + 16].setHSV(HUE_BLUE, 255, max(bandData[4],bandData[5]));
}
FastLED.show();
}
}
void SunRise()
{
static uint8_t colourIndex = 0;
// If button pressed reset to start
if(resetMode){colourIndex = 0;}
//currentPalette = mySunrise_p;
currentPalette = CRGBPalette16( CRGB::Black, CRGB::OrangeRed, CRGB::DarkOrange, CRGB::LemonChiffon);
for(uint8_t i = 0; i < numLeds; i++)
{
leds[i] = ColorFromPalette( currentPalette, colourIndex, brightness, currentBlending);
}
// Holds on last colour at end of sunrise.
if (colourIndex != 240) {colourIndex++;}
FastLED.show();
delay(sunriseTime);
}
void SunSet()
{
static uint8_t colourIndex = 0;
// If button pressed reset to start
if(resetMode){colourIndex = 0;}
//currentPalette = mySunset_p;
currentPalette = CRGBPalette16( CRGB::LemonChiffon, CRGB::DarkOrange, CRGB::OrangeRed, CRGB::Black);
for(uint8_t i = 0; i < numLeds; i++)
{
leds[i] = ColorFromPalette( currentPalette, colourIndex, brightness, currentBlending);
}
// Holds on last colour at end of sunset.
if (colourIndex != 240) {colourIndex++;}
FastLED.show();
delay(sunsetTime);
}
void RainbowFader()
{
static uint8_t hue = 0;
if(resetMode){hue = 0;}
FastLED.showColor(CHSV(hue++, 255, 255));
delay(100);
}
void Smooth()
{
static uint8_t colourIndex = 0;
static uint8_t paletteIndex = 0;
if(resetMode){colourIndex = 0; paletteIndex = 0;}
if (paletteIndex == 0) {currentPalette = CloudColors_p;}
else if (paletteIndex == 1) {currentPalette = PartyColors_p;}
else if (paletteIndex == 2) {currentPalette = OceanColors_p;}
else if (paletteIndex == 3) {currentPalette = ForestColors_p;}
else if (paletteIndex == 4) {currentPalette = RainbowColors_p;}
else {paletteIndex = 0; currentPalette = CloudColors_p;}
for(uint8_t i = 0; i < numLeds; i++)
{
leds[i] = ColorFromPalette( currentPalette, colourIndex, brightness, currentBlending);
}
if(colourIndex == 255) {paletteIndex++;}
colourIndex++;
FastLED.show();
delay(100);
}
//================================================================================
// IR Interrup Event - Called on change. Do not use Serial inside this routine!!!
//================================================================================
void IREvent(uint8_t protocol, uint16_t address, uint32_t command)
{
// Updates global values to the newest valid input
IRProtocol = protocol;
IRAddress = address;
IRCommand = command;
}