-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonSparkCore.ino
More file actions
116 lines (86 loc) · 2.21 KB
/
Copy pathpythonSparkCore.ino
File metadata and controls
116 lines (86 loc) · 2.21 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
char resultstr[64];
int white = D0;
int blue = D1;
int red = D2;
int yellow = D3;
int green = D4;
void setup()
{
pinMode(A0, INPUT); // setup A0 as analog input
pinMode(A1, INPUT); // setup A1 as analog input
pinMode(white, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
Serial.begin(9600);
Spark.variable("result", &resultstr, STRING);
Spark.function("blink", blink);
Spark.function("allOff", allOff);
Spark.function("allOn", allOn);
}
void loop()
{
int data1 = analogRead(A0);
int data2 = analogRead(A1);
sprintf(resultstr, "{\"data1\":%d,\"data2\":%d}", data1, data2);
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
int blink(String args){
Serial.println();
Serial.println("args: " + args);
args.trim();
args.toLowerCase();
String state = getValue(args, ',', 0);
String color = getValue(args, ',', 1);
Serial.println("state: " + state);
Serial.println("color: " + color);
if (state == "on" ){
digitalWrite(map(color), HIGH);
} else if (state == "off") {
digitalWrite(map(color), LOW);
}
return 200;
}
int allOff(String args){
digitalWrite(white, LOW);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
return 200;
}
int allOn(String args){
digitalWrite(white, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
digitalWrite(yellow, HIGH);
digitalWrite(green, HIGH);
return 200;
}
int map(String color){
if(color == "white") {
return D0;
} else if (color == "blue"){
return D1;
} else if (color == "red"){
return D2;
} else if (color == "yellow"){
return D3;
} else if (color == "green"){
return D4;
}
}