-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChristmasOrnament.ino
336 lines (292 loc) · 11.6 KB
/
ChristmasOrnament.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
/*
This sketch uses DHCP to obtain an IP address automatically and requires Arduino 1.0.
Network Requirements:
* Ethernet port on Router
* DHCP enabled on Router
* Unique MAC Address for Arduino
Getting a Globally Unique MAC Address from ThingSpeak:
* Sign Up for New User Account - https://www.thingspeak.com/users/new
* Register your Arduino by selecting Devices, Add New Device
* Once the Arduino is registered, click Generate Unique MAC Address
* Enter the new MAC Address in this sketch under "Local Network Settings"
Created: Novemeber 29, 2011 by Hans Scharler - http://www.iamshadowlord.com
Additional Credits:
http://www.digitalmisery.com
http://www.deepdarc.com
*/
#include <SPI.h>
#include <Ethernet.h>
//---bof---RGBL-Analog Preamble
//RGB LED pins
int ledAnalogOne[] = {3, 5, 6};
//the three pins of the first analog LED 3 = redPin, 5 = greenPin, 6 = bluePin
//These pins must be PWM
//Defined Colors (different RGB (red, green, blue) values for colors
const byte RED[] = {0, 255, 255};
const byte ORANGE[] = {172, 251, 255};
const byte YELLOW[] = {0, 200, 255};
const byte GREEN[] = {255, 0, 255};
const byte BLUE[] = {255, 255, 0};
const byte INDIGO[] = {200, 255, 220}; //?
const byte VIOLET[] = {232, 255, 233}; //?
const byte CYAN[] = {255, 0, 0};
const byte MAGENTA[] = {0, 255, 200};
const byte WHITE[] = {0, 50, 150};
const byte WARMWHITE[] = {0, 70, 220};
const byte BLACK[] = {255, 255, 255}; //?
const byte PURPLE[] = {100, 255, 160};
//---eof---RGBL-Analog Preamble
static uint16_t c;
// Local Network Settings
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String thingSpeakChannel = "1417"; // Channel number of the CheerLights Channel
String thingSpeakField = "1"; // Field number of the CheerLights commands
const int thingSpeakInterval = 16 * 1000; // Time interval in milliseconds to get data from ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
String lastCommandString = "";
byte lastCommand[] = {255, 255, 255};
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup() {
//delay(100);
for(int i = 0; i < 3; i++) {
pinMode(ledAnalogOne[i], OUTPUT); //Set the three LED pins as outputs
}
setColor(ledAnalogOne, BLACK); //Turn off led 1
// Setup Serial
Serial.begin(9600);
delay(100);
Serial.flush();
delay(100);
// Start Ethernet on Arduino
startEthernet();
}
void loop() {
// Process CheerLights Commands
if(client.available() > 0)
{
delay(100);
String response;
char charIn;
do {
charIn = client.read(); // read a char from the buffer
response += charIn; // append that char to the string response
} while (client.available() > 0);
// Light the tree
if (response.indexOf("white") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, WHITE, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = WHITE[i];
}
lastCommandString = "white";
}
else if (response.indexOf("black") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, BLACK, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = BLACK[i];
}
lastCommandString = "black";
}
else if (response.indexOf("red") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, RED, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = RED[i];
}
lastCommandString = "red";
}
else if (response.indexOf("green") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, GREEN, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = GREEN[i];
}
lastCommandString = "green";
}
else if (response.indexOf("blue") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, BLUE, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = BLUE[i];
}
lastCommandString = "blue";
}
else if (response.indexOf("cyan") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, CYAN, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = CYAN[i];
}
lastCommandString = "cyan";
}
else if (response.indexOf("magenta") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, MAGENTA, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = MAGENTA[i];
}
lastCommandString = "magenta";
}
else if (response.indexOf("yellow") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, YELLOW, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = YELLOW[i];
}
lastCommandString = "yellow";
}
else if (response.indexOf("purple") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, PURPLE, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = PURPLE[i];
}
lastCommandString = "purple";
}
else if (response.indexOf("orange") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, ORANGE, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = ORANGE[i];
}
lastCommandString = "orange";
}
else if (response.indexOf("warmwhite") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, WARMWHITE, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = WARMWHITE[i];
}
lastCommandString = "warmwhite";
}
else if (response.indexOf("black") > 0)
{
fadeToColor(ledAnalogOne, lastCommand, BLACK, 10);
delay(3000);
for(int i = 0; i < 3; i++) {
lastCommand[i] = BLACK[i];
}
lastCommandString = "black";
}
else
{
Serial.println("No match.");
//lastCommand = "(no match)";
}
// Echo command
delay(200);
Serial.println("CheerLight Command Received: "+lastCommandString);
Serial.println();
delay(200);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Subscribe to ThingSpeak Channel and Field
if(!client.connected() && (millis() - lastConnectionTime > thingSpeakInterval))
{
subscribeToThingSpeak(thingSpeakChannel, thingSpeakField);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {startEthernet();}
lastConnected = client.connected();
} // End loop
void subscribeToThingSpeak(String tsChannel, String tsField)
{
if (client.connect(thingSpeakAddress, 80))
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
client.println("GET /channels/"+tsChannel+"/field/"+tsField+"/last.txt HTTP/1.0");
client.println();
lastConnectionTime = millis();
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(1000);
}
/* Sets the color of the LED to any RGB Value led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin)) color - (byte array of three values defing an RGB color to display (color[0] = new Red value, color[1] = new Green value, color[2] = new Red value*/
void setColor(int* led, byte* color){
for(int i = 0; i < 3; i++){ //iterate through each of the three pins (red green blue)
analogWrite(led[i], 255 - color[i]); //set the analog output value of each pin to the input value (ie led[0] (red pin) to 255- color[0] (red input color)
//we use 255 - the value because our RGB LED is common anode, this means a color is full on when we output analogWrite(pin, 0)
//and off when we output analogWrite(pin, 255).
}
}
/* A version of setColor that takes a predefined color (neccesary to allow const int pre-defined colors */
void setColor(int* led, const byte* color) {
byte tempByte[] = {color[0], color[1], color[2]};
setColor(led, tempByte);
}
/* Fades the LED from a start color to an end color at fadeSpeed
led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin))
startCcolor - (byte array of three values defing the start RGB color (startColor[0] = start Red value, startColor[1] = start Green value, startColor[2] = start Red value
endCcolor - (byte array of three values defing the finished RGB color (endColor[0] = end Red value, endColor[1] = end Green value, endColor[2] = end Red value
fadeSpeed - this is the delay in milliseconds between steps, defines the speed of the fade*/
void fadeToNumColor(int* led, byte* startColor, byte* endColor, int fadeSpeed) {
int changeRed = endColor[0] - startColor[0]; //the difference in the two colors for the red channel
int changeGreen = endColor[1] - startColor[1]; //the difference in the two colors for the green channel
int changeBlue = endColor[2] - startColor[2]; //the difference in the two colors for the blue channel
int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); //make the number of change steps the maximum channel change
for(int i = 0 ; i < steps; i++) { //iterate for the channel with the maximum change
byte newRed = startColor[0] + (i * changeRed / steps); //the newRed intensity dependant on the start intensity and the change determined above
byte newGreen = startColor[1] + (i * changeGreen / steps); //the newGreen intensity
byte newBlue = startColor[2] + (i * changeBlue / steps); //the newBlue intensity
byte newColor[] = {newRed, newGreen, newBlue}; //Define an RGB color array for the new color
setColor(led, newColor); //Set the LED to the calculated value
delay(fadeSpeed); //Delay fadeSpeed milliseconds before going on to the next color
}
setColor(led, endColor); //The LED should be at the endColor but set to endColor to avoid rounding errors
}
/* A version of fadeToColor that takes predefined colors (neccesary to allow const int pre-defined colors */
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed) {
byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
fadeToNumColor(led, tempByte1, tempByte2, fadeSpeed);
}