robobuggy / arduino / RCBuggyBackup.ino @ 0c873a67
History | View | Annotate | Download (2.52 KB)
1 |
/* |
---|---|
2 |
Makes Arduino receive input from xbee and parse data. |
3 |
|
4 |
The circuit: |
5 |
* RX is digital pin 2 (connect to TX of XBee) |
6 |
* TX is digital pin 3 (connect to RX of XBee) |
7 |
|
8 |
*/ |
9 |
|
10 |
//#include <SoftwareSerial.h> |
11 |
#include <Servo.h> |
12 |
|
13 |
#define BRAKE_PIN 8 |
14 |
#define BRAKE_INDICATOR_PIN 5 |
15 |
|
16 |
#define SERVO_PIN 9 |
17 |
#define STEERING_CENTER 133 |
18 |
|
19 |
#define XBEE_MSG_REC 4 |
20 |
|
21 |
Servo myservo; // create servo object to control a servo |
22 |
|
23 |
unsigned long timer = 0L; |
24 |
char data; |
25 |
String message; |
26 |
char intbuf[32]; |
27 |
int brake = 0; |
28 |
int steeringAngle = STEERING_CENTER; |
29 |
int pingPong = 1; |
30 |
int startfound = 0; |
31 |
int midfound = 0; |
32 |
int endfound = 1; |
33 |
|
34 |
//code that keeps loop time constant each loop |
35 |
int hz = 40; |
36 |
int print_period = 1000 / hz; |
37 |
unsigned long last_time = 0; |
38 |
|
39 |
void setup() { |
40 |
Serial.begin(9600); |
41 |
Serial1.begin(9600); |
42 |
|
43 |
pinMode(XBEE_MSG_REC, OUTPUT); |
44 |
|
45 |
// initialize the brake with brake pin and led brake pin |
46 |
brake_init(brakePin, brakeLedPin); |
47 |
|
48 |
|
49 |
myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object |
50 |
myservo.write(STEEING_CENTER); |
51 |
} |
52 |
|
53 |
void loop() { |
54 |
|
55 |
// receive and parse message from xbee |
56 |
if(Serial1.available() > 0) { |
57 |
|
58 |
timer = millis(); |
59 |
|
60 |
// read message from xbee |
61 |
data = Serial1.read(); |
62 |
|
63 |
// parse message data |
64 |
if (data == 'A') { |
65 |
startfound = 1; |
66 |
midfound = 0; |
67 |
endfound = 0; |
68 |
message = ""; |
69 |
} |
70 |
else if (data == 'B') { |
71 |
startfound = 0; |
72 |
midfound = 1; |
73 |
endfound = 0; |
74 |
} |
75 |
else if (data == 'C') { |
76 |
startfound = 0; |
77 |
midfound = 0; |
78 |
endfound = 1; |
79 |
} |
80 |
else if (startfound) { |
81 |
message = message + data; |
82 |
} |
83 |
else if (midfound) { |
84 |
if(data == '1') |
85 |
brake = 1; |
86 |
else |
87 |
brake = 0; |
88 |
message.toCharArray(intbuf, sizeof(intbuf)); |
89 |
steeringAngle = atoi(intbuf); |
90 |
} |
91 |
|
92 |
// flop external LED everytime message is recieved |
93 |
if ( pingPong == 0 ) { |
94 |
digitalWrite(4, LOW); |
95 |
} |
96 |
else { |
97 |
digitalWrite(4, HIGH); |
98 |
} |
99 |
|
100 |
pingPong = 1 - pingPong; |
101 |
|
102 |
} // end receive message |
103 |
|
104 |
// brake if it has been greater than 3 seconds since we last got a message |
105 |
if( (millis() - timer) > 5000L ) { |
106 |
digitalWrite(8, 0); |
107 |
digitalWrite(5, HIGH); |
108 |
exit(1); |
109 |
} |
110 |
|
111 |
if((millis() - timer) > 2000L) { |
112 |
digitalWrite(12, HIGH); |
113 |
} |
114 |
else { |
115 |
digitalWrite(12, LOW); |
116 |
} |
117 |
|
118 |
// make brake LED light up if brakes should be down |
119 |
if( brake == 0 ) { |
120 |
digitalWrite(5, HIGH); |
121 |
} |
122 |
else { |
123 |
digitalWrite(5, LOW); |
124 |
} |
125 |
|
126 |
// send parsed signal to brakes and servo |
127 |
digitalWrite(8, brake); |
128 |
|
129 |
// sets the servo position |
130 |
myservo.write(steeringAngle); |
131 |
} |
132 |
|