-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwo_relay_system.ino
More file actions
76 lines (66 loc) · 2.03 KB
/
Two_relay_system.ino
File metadata and controls
76 lines (66 loc) · 2.03 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
// Relay system
//
// It allows for relay interaction in the case whereby 2 or more relay sets are present.
// The relays have to be assigned in a systematic order to make full use of this code.
//
// By: Bryan Wong
// Date: 4/8/2015
String instruction = "";
// Number of relay slots in the first relay set
int firstRelaySlots = 8;
int numberOfRelays = 13;
void setup() {
Serial.begin(9600);
for (int i = numberOfRelays; i < numberOfRelays + 1; i++){
pinMode(i, OUTPUT);
}
}
void loop() {
char input = Serial.read();
instruction.concat(input);
// Assigns right pin in the String instructions
instruction = identify(instruction);
// ON or OFF the right relays or use the OFF command
if (instruction.length() == 5){
relaySetOne(instruction);
}
if (instruction == "OFF"){
for (int i = numberOfRelays; i < numberOfRelays + 1; i++){
digitalWrite(i, LOW);
}
}
}
String identify(String instruction){
// If the target is relay set 2, configure the pin number to the right one
if (instruction.substring(1,1) == "2"){
int relaySwitch = instruction.substring(1,1).toInt() + firstRelaySlots;
instruction = instruction.substring(0,3) + String(relaySwitch);
}
return instruction;
}
String relaySetOne(String instruction){
// The instructions should be in the form RX_XX
// Where by X is the number set for the relay and identity\
int relaySwitch;
String info = instruction.substring(3);
/////////////////////////////////////////////////////////////////////////////
// Finds the identity of the relay
if (info.substring(3,3) != "0" || info.substring(4,4)!= "0"){
relaySwitch = info.toInt();
}
else if (info.substring(4,4) != 0){
info = info.substring(1);
relaySwitch = info.toInt();
}
else{
return "ERROR";
}
/////////////////////////////////////////////////////////////////////////////
// On and off switch based on current status of relaySwitch
if (digitalRead(relaySwitch) == LOW){
digitalWrite(relaySwitch,HIGH);
}
else{
digitalWrite(relaySwitch,LOW);
}
}