-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain
More file actions
567 lines (472 loc) · 13.8 KB
/
Main
File metadata and controls
567 lines (472 loc) · 13.8 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
Robotics Via UDP over Wifi
Author: Adam Thomas
*/
//Dependencies
#include <SPI.h>
#include <Servo.h>
#include <WiFi.h>
#include <WiFiUdp.h>
//Set up WiFi
//String ssidString; // Default Wifi SSID <----------------------------------------------------------------------------------------
//String passString; // Default Wifi password <----------------------------------------------------------------------------------------
//char ssid[] = "guest7"; // your network SSID (name)
//char pass[] = "intel4life"; // your network password
char ssid[] = "SSIDNAMEHERE"; // your network SSID (name)
char pass[] = "WIFIPASSWORDHERE"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
byte mac[] = {
0x08, 0x11, 0x96, 0xCF, 0x31, 0xFC };
IPAddress ip(192, 168,1,148);
unsigned int localPort = 8888; // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
//Instantiate Objects
Servo HeadLeftRight;
Servo HeadUpDown;
Servo EyesLeftRight;
Servo EyesUpDown;
Servo MouthOpenShut;
WiFiUDP Udp;
//Assign Pin Numbers
int led = 13;
int StatusBlue = 0;
int StatusGreen = 2;
int StatusRed = 1;
//Assign Variables
int Deadzone = 3;
boolean CenterServos = false;
int RXRate = 0;
void setup() {
Serial.begin(9600);
// while (!Serial) {
// ; // wait for serial port to connect.
// }
//Instantiate Pins
pinMode(led, OUTPUT);
pinMode(StatusBlue, OUTPUT);
pinMode(StatusGreen, OUTPUT);
pinMode(StatusRed, OUTPUT);
StatusBlink("BLUE", 50, 10);
HeadLeftRight.attach(6);
HeadUpDown.attach(9);
EyesLeftRight.attach(3);
EyesUpDown.attach(5);
MouthOpenShut.attach(10);
Status("RED");
//--------------------------------------------------SET UP Wifi-----------------
ConnectToWLAN();
system("ifconfig eth0 192.168.1.148 netmask 255.255.255.0 up");
Udp.begin(localPort);
StatusBlink("BLUE", 50, 10);
//debug setup
Status("BLUE");
}
void loop() {
// if there's data available, read a packet+
delay(RXRate);
//printCurrentNet();
int packetSize = Udp.parsePacket();
if(packetSize)
{
digitalWrite(led, HIGH);
Udp.read(packetBuffer,255);
//Serial.println("Contents:");
//Serial.println(packetBuffer); //<print recieved data for debug
// Serial.println(String(packetBuffer).indexOf("Buttons5:ON"));
//RX rate
//digitalWrite(led, HIGH);
//delay(10);
//digitalWrite(led, LOW);
//interpret commands
//---------------------------------------------------------------RB
if(String(packetBuffer).indexOf("Buttons5:ON") > -1) //RB
{
digitalWrite(led, HIGH);
}
if(String(packetBuffer).indexOf("Buttons5:OFF") > -1)
{
//Serial.println("Turning LED off.");
digitalWrite(led, LOW);
}
//---------------------------------------------------------------A
if(String(packetBuffer).indexOf("Buttons0:ON") > -1)
{
Status("GREEN");
}
if(String(packetBuffer).indexOf("Buttons0:OFF") > -1)
{
Status("BLUE");
}
//---------------------------------------------------------------B
if(String(packetBuffer).indexOf("Buttons1:ON") > -1)
{
Status("RED");
}
if(String(packetBuffer).indexOf("Buttons1:OFF") > -1)
{
Status("BLUE");
}
//---------------------------------------------------------------Y
if(String(packetBuffer).indexOf("Buttons3:ON") > -1)
{
Status("YELLOW");
}
if(String(packetBuffer).indexOf("Buttons3:OFF") > -1)
{
Status("BLUE");
}
//---------------------------------------------------------------X
if(String(packetBuffer).indexOf("Buttons2:ON") > -1)
{
Status("VIOLET");
}
if(String(packetBuffer).indexOf("Buttons2:OFF") > -1)
{
Status("BLUE");
}
//---------------------------------------------------------------Start (Speaker)
if(String(packetBuffer).indexOf("Buttons7:ON") > -1)
{
}
if(String(packetBuffer).indexOf("Buttons7:OFF") > -1)
{
}
//---------------------------------------------------------------Back (Mic)
if(String(packetBuffer).indexOf("Buttons6:ON") > -1)
{
}
if(String(packetBuffer).indexOf("Buttons6:OFF") > -1)
{
}
//---------------------------------------------------------------LB
if(String(packetBuffer).indexOf("Buttons4:ON") > -1)
{
CenterServos = true;
}
if(String(packetBuffer).indexOf("Buttons4:OFF") > -1)
{
CenterServos = false;
}
//---------------------------------------------------------------Left Joystick, Right/Left (X Axis)
if(String(packetBuffer).indexOf("X:") == 0)
{
//Serial.println("Turning LED off.");
int SD = ServoDegree(packetBuffer, true);
EyesLeftRight.write(SD);
}
//---------------------------------------------------------------Left Joystick, Up/Down (Y Axis)
if(String(packetBuffer).indexOf("Y:") == 0)
{
//Serial.println("Turning LED off.");
int SD = ServoDegree(packetBuffer, true);
EyesUpDown.write(SD);
}
//---------------------------------------------------------------Right Joystick, Right/Left (RotationX Axis)
if(String(packetBuffer).indexOf("RotationX:") == 0)
{
//Serial.println("Turning LED off.");
int SD = ServoDegree(packetBuffer, true);
HeadLeftRight.write(SD);
}
//---------------------------------------------------------------Right Joystick, Up/Down (RotationY Axis)
if(String(packetBuffer).indexOf("RotationY:") == 0)
{
//Serial.println("Turning LED off.");
int SD = ServoDegree(packetBuffer, true);
HeadUpDown.write(SD);
}
//---------------------------------------------------------------Left Trigger
if(String(packetBuffer).indexOf("Z:") == 0)
{
//Serial.println("Turning LED off.");
int SD = ServoDegree(packetBuffer, false);
MouthOpenShut.write(SD);
}
//---------------------------------------------------------------Right Trigger
// if(String(packetBuffer).indexOf("Sliders1:") == 0)
// {
// //Serial.println("Turning LED off.");
// int SD = ServoDegree(packetBuffer, false);
// analogWrite(Bled,SD);
// }
//---------------------------------------------------------------Audio
if(String(packetBuffer).indexOf("Audio:") == 0)
{
//Serial.println("Turning LED off.");
int SD = ServoDegree(packetBuffer, false);
MouthOpenShut.write(SD);
}
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
digitalWrite(led, LOW);
}
}
void StatusBlink(char* Color, int Frequency, int Qty)
{
for (int i = 0; i < Qty; i++)
{
Status(Color);
delay(Frequency);
Status("OFF");
delay(Frequency);
}
}
void Status(char* Color)
{
if(Color == "RED")
{
digitalWrite(StatusRed, LOW);
digitalWrite(StatusGreen, HIGH);
digitalWrite(StatusBlue, HIGH);
Serial.println("RED:");
}
if(Color == "GREEN")
{
digitalWrite(StatusRed, HIGH);
digitalWrite(StatusGreen, LOW);
digitalWrite(StatusBlue, HIGH);
Serial.println("GREEN");
}
if(Color == "BLUE")
{
digitalWrite(StatusRed, HIGH);
digitalWrite(StatusGreen, HIGH);
digitalWrite(StatusBlue, LOW);
Serial.println("BLUE");
}
if(Color == "VIOLET")
{
digitalWrite(StatusRed, LOW);
digitalWrite(StatusGreen, HIGH);
digitalWrite(StatusBlue, LOW);
}
if(Color == "YELLOW")
{
digitalWrite(StatusRed, LOW);
digitalWrite(StatusGreen, LOW);
digitalWrite(StatusBlue, HIGH);
}
if(Color == "BLUEGREEN")
{
digitalWrite(StatusRed, HIGH);
digitalWrite(StatusGreen, LOW);
digitalWrite(StatusBlue, LOW);
}
if(Color == "OFF")
{
digitalWrite(StatusRed, HIGH);
digitalWrite(StatusGreen, HIGH);
digitalWrite(StatusBlue, HIGH);
Serial.println("OFF");
}
if(Color == "ALL")
{
digitalWrite(StatusRed, LOW);
digitalWrite(StatusGreen, LOW);
digitalWrite(StatusBlue, LOW);
}
if(Color == "WHITE")
{
digitalWrite(StatusRed, LOW);
digitalWrite(StatusGreen, LOW);
digitalWrite(StatusBlue, LOW);
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
int ServoDegree(String Command, boolean Invert)
{
//Get first command in string
String btnval = getValue(Command, '~', 0);
//get number off of btnval
String inChar = getValue(btnval,':',1);
int deg = inChar.toInt();
//Serial.println("was" + String(deg));
if(Invert){
int newdeg = (deg * -1) + 180;
deg = newdeg;
//Serial.println("now" + String(deg));
}
if(CenterServos)
{
deg = 90;
}
return deg;
}
/*
Networking Finctions:
//*/
void ConnectToWLAN(){
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
String fv = WiFi.firmwareVersion();
if( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
}
//void PickNetwork() {
// // scan for nearby networks:
// Status("YELLOW");
// Serial.println("** Scan Networks **");
// int numSsid = WiFi.scanNetworks();
// if (numSsid == -1)
// {
// Serial.println("Couldn't get a wifi connection");
// while(true)
// {
// Status("RED");
// delay(1000);
// Status("OFF");
// delay(1000);
// }
// }
//
// // print the list of networks seen:
// Serial.print("number of available networks:");
// Serial.println(numSsid);
// boolean foundssid = false;
//
// // print the network number and name for each network found:
// for (int thisNet = 0; thisNet<numSsid; thisNet++) {
// Serial.print(thisNet);
// Serial.print(") ");
// Serial.print(WiFi.SSID(thisNet));
// Serial.print("\tSignal: ");
// Serial.print(WiFi.RSSI(thisNet));
// Serial.print(" dBm");
// Serial.print("\tEncryption: ");
// //printEncryptionType(WiFi.encryptionType(thisNet));
//
//
// if(String(WiFi.SSID(thisNet)) == "guest5") //Office
// {
// ssidString = "";
// passString = "";
// StatusBlink("GREEN", 200, 10);
// foundssid = true;
// break;
// }
// else if(String(WiFi.SSID(thisNet)) == "RAWifi") //Home
// {
// ssidString = "";
// passString = "";
// StatusBlink("VIOLET", 100, 20);
// foundssid = true;
// break;
// }
// else if(String(WiFi.SSID(thisNet)) == "thomasair") //Moms
// {
// ssidString = "";
// passString = "";
// StatusBlink("GREEN", 200, 10);
// foundssid = true;
// break;
// }
// else if(String(WiFi.SSID(thisNet)) == "idunno") //Perrys
// {
// ssidString = "";
// passString = "";
// StatusBlink("VIOLET", 100, 20);
// foundssid = true;
// break;
// }
// else //Not on list
// {
// char ssid[] = "";
// char pass[] = "";
// }
// }
//
// if(!foundssid)
// {
// while(true)
// {
// Status("YELLOW");
// delay(1000);
// Status("OFF");
// delay(1000);
// }
// }
//}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5],HEX);
Serial.print(":");
Serial.print(bssid[4],HEX);
Serial.print(":");
Serial.print(bssid[3],HEX);
Serial.print(":");
Serial.print(bssid[2],HEX);
Serial.print(":");
Serial.print(bssid[1],HEX);
Serial.print(":");
Serial.println(bssid[0],HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption,HEX);
Serial.println();
}
void printWifiData() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print(mac[5],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
}