This repository was archived by the owner on Apr 20, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathSerialEvent.ino
More file actions
99 lines (82 loc) · 2.1 KB
/
Copy pathSerialEvent.ino
File metadata and controls
99 lines (82 loc) · 2.1 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
/*
Serial Relay Controller
Serial readings were extracted from
http://www.arduino.cc/en/Tutorial/SerialEvent
This code swiches pins according to a list of strings.
*/
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
//Relay channels
int ch1 = 12;
int ch2 = 11;
int ch3 = 10;
int ch4 = 9;
int array[] = {ch1, ch2, ch3, ch4};
void setup() {
// Initialize pins
for (int i = 0; i < 4; i++) {
pinMode(array[i], OUTPUT);
}
// initialize serial:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void switchPin(int pin) {
delay(200);
if (digitalRead(pin)) {
digitalWrite(pin, LOW);
}
else {
digitalWrite(pin, HIGH);
}
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
inputString.trim();
Serial.println(inputString);
if (inputString.equalsIgnoreCase("ventilador")) {
switchPin(ch1);
};
if (inputString.equalsIgnoreCase("carregador")) {
switchPin(ch2);
};
if (inputString.equalsIgnoreCase("luzes")) {
switchPin(ch3);
};
if (inputString.equalsIgnoreCase("pc")) {
switchPin(ch4);
};
if (inputString.equalsIgnoreCase("todos")) {
for (int i = 0; i < 4; i++) {
switchPin(array[i]);
}
};
// 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;
}
}
}