robobuggy / arduino / RCbuggyMega / xbee.cpp @ 6331ce9f
History | View | Annotate | Download (1.54 KB)
1 | 0c873a67 | msebek | /**
|
---|---|---|---|
2 | 453e1532 | unknown | * Code relating to something I'm not sure yet
|
3 | * @author Audrey Yeoh (ayeoh)
|
||
4 | *
|
||
5 | */
|
||
6 | |||
7 | #include <Arduino.h> |
||
8 | #include "xbee.h" |
||
9 | |||
10 | #define STEERING_CENTER 133 |
||
11 | |||
12 | // holds the data read from the serial xbee
|
||
13 | static char data; |
||
14 | |||
15 | // holds the message itself
|
||
16 | static String message;
|
||
17 | |||
18 | static int pingPong = 1; |
||
19 | static int startFound = 0; |
||
20 | static int midFound = 0; |
||
21 | static int endFound = 1; |
||
22 | static char intbuf[32]; |
||
23 | |||
24 | static int steeringAngle = STEERING_CENTER; |
||
25 | static int brake = 0; |
||
26 | |||
27 | static int led; |
||
28 | |||
29 | void xbee_init(int ledPin){ |
||
30 | pinMode(ledPin, OUTPUT); |
||
31 | led = ledPin; |
||
32 | } |
||
33 | |||
34 | // parses the data from the xbee serial
|
||
35 | void parse(char data){ |
||
36 | switch(data){
|
||
37 | case 'A': |
||
38 | startFound = 1;
|
||
39 | midFound = 0;
|
||
40 | endFound = 0;
|
||
41 | message = "";
|
||
42 | break;
|
||
43 | |||
44 | case 'B': |
||
45 | startFound = 0;
|
||
46 | midFound = 1;
|
||
47 | endFound = 0;
|
||
48 | break;
|
||
49 | case 'C': |
||
50 | startFound = 0;
|
||
51 | midFound = 0;
|
||
52 | endFound =1;
|
||
53 | break;
|
||
54 | default: // all other cases |
||
55 | if (startFound){
|
||
56 | message = message + data; |
||
57 | |||
58 | }else if (midFound){ |
||
59 | if (data == '1'){ |
||
60 | brake = 1;
|
||
61 | }else{
|
||
62 | brake = 0;
|
||
63 | } |
||
64 | message.toCharArray(intbuf, sizeof(intbuf));
|
||
65 | steeringAngle = atoi(intbuf); |
||
66 | } |
||
67 | break;
|
||
68 | } |
||
69 | } |
||
70 | |||
71 | |||
72 | int getSteeringAngle(){
|
||
73 | return steeringAngle;
|
||
74 | } |
||
75 | |||
76 | int getBrake(){
|
||
77 | return brake;
|
||
78 | } |
||
79 | |||
80 | // flops an led everytime a message is received.
|
||
81 | // Helps to debug whether messages are received
|
||
82 | void debugMessage(){
|
||
83 | if (pingPong == 0){ |
||
84 | digitalWrite(led, LOW); |
||
85 | }else{
|
||
86 | digitalWrite(led, HIGH); |
||
87 | } |
||
88 | |||
89 | pingPong = 1 - pingPong;
|
||
90 | } |