Project

General

Profile

Statistics
| Branch: | Revision:

robobuggy / arduino / xbee.ino @ 0c873a67

History | View | Annotate | Download (1.2 KB)

1
/**
2
 * Code relating to xbee
3
 *
4
 */
5

    
6
static int xbee_brake;
7
static int xbee_angle;
8
static int xbee_connected;
9

    
10
String message;
11
char intbuf[32];
12

    
13
int pingPong = 1;
14
int startfound = 0;
15
int midfound = 0;
16
int endfound = 1;
17

    
18
void xbee_init() {
19

    
20

    
21
}
22

    
23
void xbee_loop() {
24
  // receive and parse message from xbee
25
  if(Serial1.available() > 0) {
26

    
27
    timer = millis();
28

    
29
    // read message from xbee
30
    data = Serial1.read();
31

    
32
    // parse message data
33
    if (data == 'A') {
34
      startfound = 1;
35
      midfound = 0;
36
      endfound = 0;
37
      message = "";
38
    }
39
    else if (data == 'B') {
40
      startfound = 0;
41
      midfound = 1;
42
      endfound = 0;
43
    }
44
    else if (data == 'C') {
45
      startfound = 0;
46
      midfound = 0;
47
      endfound = 1;
48
    }
49
    else if (startfound) {
50
      message = message + data;
51
    }
52
    else if (midfound) {
53
      if(data == '1')
54
        brake = 1;
55
      else
56
        brake = 0;
57
      message.toCharArray(intbuf, sizeof(intbuf));
58
      steeringAngle = atoi(intbuf);
59
    }
60

    
61
    // flop external LED everytime message is recieved
62
    if ( pingPong == 0 ) {
63
      digitalWrite(4, LOW);
64
    }
65
    else {
66
      digitalWrite(4, HIGH);
67
    }
68

    
69
    pingPong = 1 - pingPong;
70

    
71
  } // end receive message
72
}