-
Notifications
You must be signed in to change notification settings - Fork 0
/
RelayCtrl.ino
134 lines (128 loc) · 3.29 KB
/
RelayCtrl.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
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
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
const int R1 = 7;
const int R2 = 6;
const int R3 = 5;
const int R4 = 4;
const int D1 = 2;
const int D2 = 3;
void setup() {
// initialize serial:
Serial.begin(115200);
Serial.write("READY\n");
// reserve 200 bytes for the inputString:
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(R3, OUTPUT);
pinMode(R4, OUTPUT);
pinMode(D1, INPUT);
pinMode(D2, INPUT);
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
int pin = 0;
String state = "";
if (stringComplete) {
//Serial.write((char)inputString[2]);
if (inputString[0] == '?') {
switch (inputString[1]) {
case '1':
state = (String)digitalRead(D1);
break;
case '2':
state = (String)digitalRead(D2);
break;
case 'A':
switch (inputString[2]) {
case '0':
state = (String)(((float)analogRead(A0) * 5) / 1023);
break;
case '1':
state = (String)(((float)analogRead(A1) * 5) / 1023);
break;
case '2':
state = (String)(((float)analogRead(A2) * 5) / 1023);
break;
case '3':
state = (String)(((float)analogRead(A3) * 5) / 1023);
break;
case '4':
state = (String)(((float)analogRead(A4) * 5) / 1023);
break;
case '5':
state = (String)(((float)analogRead(A5) * 5) / 1023);
break;
default:
Serial.print("ERR\n");
break;
}
break;
default:
Serial.print("ERR\n");
break;
}
Serial.print(state);
Serial.print('\n');
}
else if (inputString[0] == 'C') {
Serial.print("READY\n");
}
else {
//int state_int = (int)inputString[1];
//Serial.println(inputString[1]);
switch (inputString[0]) {
case '1':
pin = R1;
break;
case '2':
pin = R2;
break;
case '3':
pin = R3;
break;
case '4':
pin = R4;
break;
default:
Serial.print("ERR\n");
break;
}
switch (inputString[1]) {
case '1':
digitalWrite(pin, HIGH);
Serial.print("ACK\n");
break;
case '0':
digitalWrite(pin, LOW);
Serial.print("ACK\n");
break;
default:
Serial.print("ERR\n");
break;
}
}
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}