-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ino
More file actions
267 lines (203 loc) · 5.58 KB
/
main.ino
File metadata and controls
267 lines (203 loc) · 5.58 KB
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
//#include "I2C_LED.h"
//Library from BlinkM
#include "BlinkM_funcs.h"
// ===============================================
String statusArray [3] = {"off", "on", "error"};
String colorArray [8] = {"white", "red", "green", "blue", "cyan", "magenta", "yellow", "black"};
String actionArray [3]= {"solid", "blink", "breathe"};
// defining array of "status"
struct channel {
String status[3];
String color[3];
String action[3];
};
channel IPC_LED_1 = {
{"off", "on", "error"},
{"white", "green", "red"},
{"solid", "blink", "breathe"}
};
// Serial: this is the COMMAND variable
// it holds the most recent command from the serial_1 port
String serial1Command = "";
// ===============================================
void setup() {
// Open serial port (via USB port)
Serial.begin(9600);
// Open serial port 1 (via Tx/Rx pins)
Serial1.begin(9600);
// start I2C com with the BlinkM smart LED
BlinkM_begin();
BlinkM_stopScript(0x00);
}
// ===============================================
// runs continuously
void loop() {
}
// ===============================================
// Interrupt for serial 0 (USB)
// called when data is rady to be read
void serialEvent()
{
char c = Serial.read();
//Serial.println(c);
Serial1.write(c);
}
// Interrupt for serial 1 (Tx/Rx)
// called when data is rady to be read
void serialEvent1()
{
char c = Serial1.read();
if (c != ';')
{
serial1Command = serial1Command + c;
Serial.print(c);
}
else
{
parseSerialCommand(serial1Command);
serial1Command = "";
Serial.println("");
}
}
// Parse the command read over serial1
void parseSerialCommand(String command) {
Serial.print("\nNew command is: ");
Serial.print(command);
Serial.print("\n");
if (command == "on")
{
BlinkM_fadeToRGB(0x00, 0x00, 0xff, 0xff);
}
else if (command == "off")
{
BlinkM_fadeToRGB(0x00, 0x00, 0x00, 0x00);
}
else if (command == "error")
{
BlinkM_fadeToRGB(0x00, 0xff, 0x00, 0x00);
}
else
{
BlinkM_fadeToRGB(0x00, 0x00, 0x00, 0xff);
}
}
/*
// defining array of colors
// MY: defining random array of colors
//String myColors[2] = {"blue", "red"};
// define the LEDcolor variable
String LEDcolor = "";
int reading = 0;
double volts = 0.0;
// define the A5 as the photoresistor
int photoR = A0;
// runs once at the beginning
void setup() {
// Open serial port
Serial.begin(9600);
// Defining IoT functions
Particle.function("color", changeColor);
// Defining IoT variables
// Particle.variable("newColor", &LEDcolor, STRING);
// Publish LED chanage of color
// particle.publish("newColor")
// Defining internet variables for reading the light level
Spark.variable("analog", &reading, INT);
Spark.variable("volts", &volts, DOUBLE);
// join i2c bus
Wire.begin();
// turn off any blinkm scripts
Wire.beginTransmission(0x00);
Wire.write('o');
Wire.endTransmission();
// Running a built-in scripts
Wire.beginTransmission(0x00);
// fade to a color
// Wire.write('c');
// set fade speed
BlinkM_setFadeSpeed(0x00, 0xFF);
//Wire.write('f');
// set the RGB bytes
//Wire.write(0x09);
// set tiem adjust
Wire.write('t');
// set the RGB bytes
Wire.write(0xAA);
Wire.write('p');
// set the RGB bytes
Wire.write(0x05);
Wire.write(0x00);
Wire.write(0x00);
// send the transmission
Wire.endTransmission();
}
// runs continuously
void loop() {
reading = analogRead(photoR);
volts = reading * 3.3 / 4096.0;
Serial.print(volts);
Serial.print(" - ");
delay(500);
}
// Print the color over serial port to the terminal
void printSerial(String color) {
Serial.println("The user selected the color ");
Serial.print(color);
Serial.print('\n');
}
// transmit to the I2C
void transmitColor(byte red, byte green, byte blue) {
// turn off any blinkm scripts
Wire.beginTransmission(0x00);
Wire.write('o');
Wire.endTransmission();
// begin transmission to 0x00, which will send to all connected I2Cs
Wire.beginTransmission(0x00);
// fade to a color
Wire.write('c');
// set the RGB bytes
Wire.write(red);
Wire.write(green);
Wire.write(blue);
// send the transmission
Wire.endTransmission();
}
// LED blinking
//void LEDblink(String color, int duration) {
//}
// LED breething
// set color based on name
int changeColor(String color_name) {
// publish
Particle.publish("newColor");
// console update
printSerial(color_name);
if (color_name == "red") {
transmitColor(0xff, 0x00, 0x00);
} else if (color_name == "green") {
transmitColor(0x00, 0x80, 0x00);
} else if (color_name == "blue") {
transmitColor(0x00, 0x00, 0xff);
} else if (color_name == "cyan") {
transmitColor(0x00, 0xff, 0xff);
} else if (color_name == "white") {
transmitColor(0xff, 0xff, 0xff);
} else if (color_name == "oldlace") {
transmitColor(0xfd, 0xf5, 0xe6);
} else if (color_name == "warmwhite") {
transmitColor(0xfd, 0xf5, 0xe6);
} else if (color_name == "purple") {
transmitColor(0x80, 0x00, 0x80);
} else if (color_name == "magenta") {
transmitColor(0xff, 0x00, 0xff);
} else if (color_name == "yellow") {
transmitColor(0xff, 0xff, 0x00);
} else if (color_name == "orange") {
transmitColor(0xff, 0xa5, 0x00);
} else if (color_name == "pink") {
transmitColor(0xff, 0xc0, 0xcb);
} else if (color_name == "dim") {
transmitColor(0x04, 0x04, 0x04);
}
}
*/