Revision 0c873a67
Fixed up all the buggy code.
arduino/RCbuggyMega/RCbuggyMega.ino | ||
---|---|---|
1 | 1 |
/* |
2 | 2 |
Makes Arduino receive input from xbee and parse data. |
3 |
|
|
3 |
|
|
4 | 4 |
The circuit: |
5 | 5 |
* RX is digital pin 2 (connect to TX of XBee) |
6 | 6 |
* TX is digital pin 3 (connect to RX of XBee) |
7 |
|
|
7 |
|
|
8 | 8 |
*/ |
9 | 9 |
|
10 | 10 |
//#include <SoftwareSerial.h> |
11 |
#include <Servo.h> |
|
11 |
#include <Servo.h> |
|
12 |
|
|
13 |
#define BRAKE_PIN 8 |
|
14 |
#define BRAKE_INDICATOR_PIN 5 |
|
12 | 15 |
|
13 |
Servo myservo; // create servo object to control a servo |
|
16 |
#define SERVO_PIN 9 |
|
17 |
#define STEERING_CENTER 133 |
|
18 |
|
|
19 |
Servo myservo; // create servo object to control a servo |
|
14 | 20 |
|
15 | 21 |
//SoftwareSerial xbee(2, 3); // RX, TX |
16 | 22 |
unsigned long timer = 0L; |
... | ... | |
18 | 24 |
String message; |
19 | 25 |
char intbuf[32]; |
20 | 26 |
int brake = 0; |
21 |
int steeringAngle = 135;
|
|
27 |
int steeringAngle = STEERING_CENTER;
|
|
22 | 28 |
int pingPong = 1; |
23 | 29 |
int startfound = 0; |
24 | 30 |
int midfound = 0; |
25 | 31 |
int endfound = 1; |
26 | 32 |
|
27 |
int brakePin = 8; |
|
28 |
int brakeLedPin = 5; |
|
29 | 33 |
|
30 | 34 |
void setup() { |
31 | 35 |
Serial.begin(9600); |
32 |
//Serial.println( "Arduino started sending bytes via XBee" ); |
|
33 | 36 |
Serial1.begin(9600); |
34 |
|
|
37 |
|
|
35 | 38 |
pinMode(4, OUTPUT); |
36 |
pinMode(5, OUTPUT); |
|
37 |
pinMode(8, OUTPUT);
|
|
38 |
|
|
39 |
brake_init(brakePin, brakeLedPin); // initialize the brake with brake pin and led brake pin |
|
40 |
|
|
41 |
myservo.attach(9); // attaches the servo on pin 9 to the servo object
|
|
42 |
myservo.write(133);
|
|
39 |
|
|
40 |
// initialize the brake with brake pin and led brake pin
|
|
41 |
brake_init(brakePin, brakeLedPin); |
|
42 |
|
|
43 |
|
|
44 |
myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
|
|
45 |
myservo.write(STEEING_CENTER);
|
|
43 | 46 |
} |
44 | 47 |
|
45 | 48 |
void loop() { |
46 | 49 |
|
47 | 50 |
// receive and parse message from xbee |
48 |
if(Serial1.available() > 0) {
|
|
51 |
if(Serial1.available() > 0) { |
|
49 | 52 |
|
50 | 53 |
timer = millis(); |
51 | 54 |
|
... | ... | |
79 | 82 |
brake = 0; |
80 | 83 |
message.toCharArray(intbuf, sizeof(intbuf)); |
81 | 84 |
steeringAngle = atoi(intbuf); |
82 |
}
|
|
85 |
} |
|
83 | 86 |
|
84 | 87 |
// flop external LED everytime message is recieved |
85 | 88 |
if ( pingPong == 0 ) { |
86 | 89 |
digitalWrite(4, LOW); |
87 |
}
|
|
90 |
} |
|
88 | 91 |
else { |
89 | 92 |
digitalWrite(4, HIGH); |
90 | 93 |
} |
... | ... | |
110 | 113 |
// make brake LED light up if brakes should be down |
111 | 114 |
if( brake == 0 ) { |
112 | 115 |
digitalWrite(5, HIGH); |
113 |
}
|
|
116 |
} |
|
114 | 117 |
else { |
115 | 118 |
digitalWrite(5, LOW); |
116 | 119 |
} |
Also available in: Unified diff