The XBees are serial devices. You need to hook the XBee on each end to a serial port.
I would recommend using SoftwareSerial on the sending Arduino (on some pins other than 0 and 1,) so you don't get interference with programming the board.
You can use the hardware serial port (pins 0 and 1) on the receiving Arduino.
Then, build a simple protocol, and send commands over the serial wire. A simple protocol might be:
0xEE <command byte> <parameter byte>>
So, a command might be "Set forward speed." Call this 0x01. The parameter is the speed value (0 - 255.) Your sending Arduino would then do something like this:
enum {
CMD_FORWARD = 0x1,
CMD_TURN = 0x2
};
SoftwareSerial ss(...);
void sendForward(unsigned char val) {
unsigned char data[3] = { 0xEE, CMD_FORWARD, val };
ss.write(3, data);
}
On the receiving end, you have to receive and decode commands. You should be getting one byte at a time -- you can't assume that a full command will be coming in in a single read request. The algorithm looks something like:
unsigned char buffer[3];
unsigned char bufptr = 0;
void handle_command() {
switch (buffer[1]) {
case CMD_FORWARD:
... value is in buffer[2];
break;
....
}
}
void read_one() {
if (myserialport has data) {
buffer[bufptr] = read one byte;
if ((bufptr == 0 && buffer[0] == 0xEE) || (bufptr > 0)) {
++bufptr; // in a packet
}
if (bufptr == 3) { // packet finished
handle_command();
bufptr = 0;
}
}
}