-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspark-web.ino
99 lines (59 loc) · 2.8 KB
/
spark-web.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
//PUT YOUR VARIABLES HERE
void setup(){
Spark.function("my-main", myMain);
//PUT YOUR SETUP CODE HERE. Note: Only three more functions allowed!
// test everything using the return int from a function!
//RGB.control(true);
//RGB.color(0, 255, 255); //cyan
RGB.brightness(1); // 1=very low light, 255 = max
}
void loop(){
//PUT YOUR LOOP CODE HERE
}
int myMain(String myCode) {
myCode.toUpperCase(); // set argument to uppercase--remove for better security
// used send instead of write since I needed it to be 4 characters long.
// d7-send-1 or d7-send-high or d7-send-on to turn on D7
// d7-send-0 or d7-send-low or d7-send-off to tuurn off D7
// d5-read read D5
// a0-send-0 turn A0 off
// a0-send-255 turn A0 maximum
// a1-read read A1
// Block sets pinNumber for digital 0-7 or analog 10-17 from the number
int mySetWrite = 0;
int pinNumber = myCode.charAt(1) - '0';
if (pinNumber< 0 || pinNumber >7) return -1;
if (myCode.startsWith("A")){pinNumber = pinNumber+10;} //+10 is for analog numbers
String myActivity = myCode.substring(3,7); // take 4 characters starting at the 3rd.
//Following sets the 7 and on characters to integers
String myOptional = myCode.substring(8);
if(myOptional == "HIGH") {mySetWrite = 1;}
else if(myOptional == "LOW") {mySetWrite = 0; }
else if(myOptional == "ON") {mySetWrite = 1;}
else if(myOptional == "OFF") {mySetWrite = 0; }
else {mySetWrite = myOptional.toInt(); } // sets write value
// myCode parsing complete
if (pinNumber < 9) { // digital pins activated
if (myActivity == "READ"){ //digital read
pinMode(pinNumber, INPUT_PULLDOWN);
return digitalRead(pinNumber);
}
if (myActivity == "SEND"){ //digital write
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, mySetWrite);
return mySetWrite;
}
} else { // analog pins activated
if (myActivity == "READ"){ //Analog read // pinMode(pinNumber, INPUT_PULLUP); // sets unknown to max 4095 analog read
// pinMode(pinNumber, INPUT); // sets to input
// pinMode(pinNumber, INPUT_PULLDOWN); // sets unknown to 0 min analog read
// trying nothing, since with the new version of tinker this works
return analogRead(pinNumber);
}
if (myActivity == "SEND"){ //Analog Write
pinMode(pinNumber, OUTPUT);
analogWrite(pinNumber, mySetWrite);
return mySetWrite;
}
}
}