-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledsaber.ino
69 lines (52 loc) · 1.55 KB
/
ledsaber.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
// This #include statement was automatically added by the Particle IDE.
#include "MPU6050.h"
int ledRPin = D3;
int ledGPin = WKP;
int ledBPin = D2;
String mpu = "{\"x\":\"0\",\"y\":\"0\",\"z\":\"0\"}";
String rgb = "{\"r\":\"0\",\"g\":\"255\",\"b\":\"255\"}";
void readMPU() {
MPU6050();
mpu = "{\"x\":\"" + String(get_last_x_angle(),2) + "\"," +
"\"y\":\"" + String(get_last_y_angle(),2) + "\"," +
"\"z\":\"" + String(get_last_z_angle(),2) + "\"}";
}
Timer MPU6050_task(20, readMPU);
void setup() {
Wire.begin();
MPU6050_init();
MPU6050_task.start();
RGB.control(true);
pinMode(ledRPin, OUTPUT);
pinMode(ledGPin, OUTPUT);
pinMode(ledBPin, OUTPUT);
setRGB("0,255,255");
Particle.variable("mpu", mpu);
Particle.variable("rgb", rgb);
Particle.function("setrgb", setRGB);
}
void loop() {
}
int setRGB(String command) {
int commaIndex[2];
if(command == "")
return -1;
commaIndex[0] = command.indexOf(',');
for(int i = 1; i < 2; i++)
{
commaIndex[i] = command.indexOf(',', commaIndex[i-1] + 1);
if(commaIndex[i] == -1)
return -1;
}
int r = (command.substring(0, commaIndex[0])).toInt();
int g = (command.substring(commaIndex[0] + 1, commaIndex[1])).toInt();
int b = (command.substring(commaIndex[1] + 1)).toInt();
RGB.color(r, g, b);
analogWrite(ledRPin, 255-r);
analogWrite(ledGPin, 255-g);
analogWrite(ledBPin, 255-b);
rgb = "{\"r\":\"" + String(r) + "\"," +
"\"g\":\"" + String(g) + "\"," +
"\"b\":\"" + String(b) + "\"}";
return 0;
}