-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_Senior_Project.ino
More file actions
299 lines (145 loc) · 4.06 KB
/
Arduino_Senior_Project.ino
File metadata and controls
299 lines (145 loc) · 4.06 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <SPI.h>
#include <Ethernet.h>
// MAC address and IP address for your controller
// MAC=90 a2 da 0d 14 dc
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x14, 0xDC };
//IPAddress ip(192,168,1, 3);
// Initialize the Ethernet server library with port 80
EthernetServer server(80);
//set the pins in use
int pins[]={49,47,45,43,41,39,37,35};
int const NUM_OF_PINS=8;
//delay between setting the bit sequence returning to normal state, 0.
int const TRANSITION_DELAY=125; // delay between the shorts
int const TRANSITION_DELAY_INSTRUCTIONS=125; // Delay Between instructions
int const DELAY_START=1000; // Delay before executing the start button
//Index at which the bit sequence in the http request start
int const BITSEQ_INDEX=6; // send the request you have to start at #6, 6 characters that you have to start parsing from there
void setup() {
//Open serial communications
Serial.begin(9600);
//setup the pins as output
for(int i=0;i<NUM_OF_PINS;i++){
pinMode(pins[i], OUTPUT);
}
//Initializes the ethernet library and network settings
Serial.print("Configuring IP...");
if(Ethernet.begin(mac) == 0){
Serial.println("Failed to configure");
while(true);
}
else
Serial.println("complete");
//start the server at port 80
server.begin();
}
void loop(){
//get a client connected to Arduino server. If there isnt any, then client==false
EthernetClient client = server.available();
//Serial.println("Checking for clients...");
boolean currentLineIsBlank = true;
if (client) {
char c;
String s;
while (client.connected()) {
if (client.available()) { //determine if there is >0 bytes to process from this client
c = client.read();
s+=String(c);
if (c == '\n' && currentLineIsBlank) {
//reply(client,s); //to see the Reply in the web page
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else
if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
client.stop();
Serial.println("Executing Instructions");
parse_exec(s,BITSEQ_INDEX);
Serial.println("Done");
}
}
void reply(EthernetClient client,String s){
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<html><head></head><body><div>");
client.println(s);
client.println("</div></body></html>");
}
//parse and execute the sequence of instructions
void parse_exec(String ar,int cnt){
int size=CharToInt(ar.charAt(cnt++));
String ins;
for(int i=0;i<size;i++){
ins=ar.substring(cnt,cnt+2);
if(ins=="18"){
delay(DELAY_START);
}
exec(ins);
delay(TRANSITION_DELAY_INSTRUCTIONS);
cnt=cnt+2;
}
}
void exec(String in){
int pin1=CharToInt(in.charAt(0));
int pin2=CharToInt(in.charAt(1));
if (pin1>8 or pin1<1 or pin2>8 or pin2<1){
Serial.println('Error: pin number out of range');
}
else{
pin1-=1;
pin2-=1;
digitalWrite(pins[pin1], HIGH);
digitalWrite(pins[pin2], HIGH);
delay(TRANSITION_DELAY);
digitalWrite(pins[pin1], LOW);
digitalWrite(pins[pin2], LOW);
}
}
int CharToInt(const char c){
switch (c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
Serial.println("Error: Its not a number. Reason:the number of instructions is wrong or instructions contain characteres.");return -1;
}
}
void test(){
Serial.println("Testing Pins in use...");
for(int i=0;i<8;i++){
digitalWrite(pins[i], HIGH);
}
delay(1000);
for(int i=0;i<8;i++){
digitalWrite(pins[i], LOW);
}
delay(1000);
}