forked from vascop/Python-Arduino-Proto-API-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype.pde
51 lines (45 loc) · 1.23 KB
/
prototype.pde
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
#ifndef SERIAL_RATE
#define SERIAL_RATE 115200
#endif
#ifndef SERIAL_TIMEOUT
#define SERIAL_TIMEOUT 5
#endif
void setup() {
Serial.begin(SERIAL_RATE);
Serial.setTimeout(SERIAL_TIMEOUT);
int cmd = readData();
for (int i = 0; i < cmd; i++) {
pinMode(readData(), OUTPUT);
}
}
void loop() {
switch (readData()) {
case 0 :
//set digital low
digitalWrite(readData(), LOW); break;
case 1 :
//set digital high
digitalWrite(readData(), HIGH); break;
case 2 :
//get digital value
Serial.println(digitalRead(readData())); break;
case 3 :
// set analog value
analogWrite(readData(), readData()); break;
case 4 :
//read analog value
Serial.println(analogRead(readData())); break;
case 99:
//just dummy to cancel the current read, needed to prevent lock
//when the PC side dropped the "w" that we sent
break;
}
}
char readData() {
Serial.println("w");
while(1) {
if(Serial.available() > 0) {
return Serial.parseInt();
}
}
}