-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubmasterController.ino
190 lines (165 loc) · 4.48 KB
/
SubmasterController.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Testing the XBee
// Jack Shaver
// 2/20/2024
/*
This program is written for the Sub-Master Controller V1
It takes XBee communication on Pins 10 and 11 (RX and TX)
The wiring for these pins is difficult, from left to right with the board oriented upwards:
TX, unconnected, RX, unconnected, ...
on the 6 headers to the right of the arduino nano
Expected Commands:
coilOne
coilTwo
collectData
help
TODO:
Add the ability to check the state of the data collection.
Change how the Nano speaks to the teensy for data collection.
*/
#include <SoftwareSerial.h>
SoftwareSerial XBee(10, 11);
#define Error_Comm_LED A0
#define Relay1 2
#define Relay2 3
#define Relay3 4
#define Relay4 5
#define ParallelBit0 9
#define ParallelBit1 8
#define ParallelBit2 7
#define ParallelBit3 6
void setup() {
pinMode(Error_Comm_LED, OUTPUT);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(ParallelBit0, OUTPUT);
pinMode(ParallelBit1, OUTPUT);
pinMode(ParallelBit2, OUTPUT);
pinMode(ParallelBit3, OUTPUT);
digitalWrite(ParallelBit0, LOW);
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, LOW);
XBee.begin(9600);
Serial.begin(9600);
Serial.println("Testing the XBee");
Serial.println("Expected Messages: coilOne, coilTwo, collectData, help, ping");
}
String message = "";
char character;
int commandRecieved = 0;
long coilOneTimer;
bool coilOneState = LOW;
long coilTwoTimer;
bool coilTwoState = LOW;
long dataTimer;
bool dataPulse = LOW;
long commTimer;
bool commPulse = LOW;
void loop()
{
// This statement reads the incoming characters from the XBee, and formats a string
while(XBee.available())
{
digitalWrite(Error_Comm_LED, HIGH);
commTimer = millis();
commPulse = HIGH;
character = XBee.read();
message.concat(character);
if(message.lastIndexOf(0x0D) > 0)
{
commandRecieved = decodeMessage(message);
message = "";
}
}
// This statement evaluates what to do when a valid command is recieved
if(commandRecieved != 0)
{
switch(commandRecieved){
case 1:
digitalWrite(Relay1, HIGH);
coilOneState = HIGH;
coilOneTimer = millis();
break;
case 2:
digitalWrite(Relay2, HIGH);
coilTwoState = HIGH;
coilTwoTimer = millis();
break;
case 3:
digitalWrite(ParallelBit0, HIGH);
dataPulse = HIGH;
dataTimer = millis();
break;
case 4:
XBee.println("XBee Serial Interface with Sensor Monitor");
XBee.println("Commands: coilOne, coilTwo, collectData, help, ping");
break;
case 5:
XBee.println("XBee_B Successfully Pinged");
break;
}
commandRecieved = 0;
}
// These if cases handle returning the IO back to their resting after a time period
if((coilOneState == HIGH) && ((millis() - coilOneTimer) > 1000)){
coilOneState = LOW;
digitalWrite(Relay1, LOW);
}
if((coilTwoState == HIGH) && ((millis() - coilTwoTimer) > 1000)){
coilTwoState = LOW;
digitalWrite(Relay2, LOW);
}
if((dataPulse == HIGH) && ((millis() - dataTimer) > 500)){
dataPulse = LOW;
digitalWrite(ParallelBit0, LOW);
}
if((commPulse == HIGH) && ((millis() - commTimer) > 50)){
commPulse = LOW;
digitalWrite(Error_Comm_LED, LOW);
}
}
// This function takes a compiled string, and checks if a valid command exists within it.
// It also transmits the validity of the command back to the user.
int decodeMessage(String message){
int returnCode = 0;
String confirmationString = "";
confirmationString = "Message Recieved: ";
message.toLowerCase();
if(strstr(message.c_str(), "coilone") != NULL)
{
confirmationString.concat("coilone");
returnCode = 1;
}
else if(strstr(message.c_str(), "coiltwo") != NULL)
{
returnCode = 2;
confirmationString.concat("coiltwo");
}
else if(strstr(message.c_str(), "collectdata") != NULL)
{
returnCode = 3;
confirmationString.concat("collectdata");
}
else if(strstr(message.c_str(), "help") != NULL)
{
returnCode = 4;
confirmationString.concat("help");
}
else if(strstr(message.c_str(), "ping") != NULL)
{
returnCode = 5;
confirmationString.concat("ping");
}
else
{
returnCode = 0;
confirmationString.concat("INVALID COMMAND");
}
XBee.println(confirmationString);
Serial.print("Message Recieved: ");
Serial.print(message);
Serial.print(" -> Response: ");
Serial.println(returnCode);
return returnCode;
}