Here is the code I used on the receive side (Arduino USB board with Adafruit XBee adapter) connected to the steering servo and speed control on an old RC-10T truck:
#include "Servo.h" #include "NewSoftSerial.h" int joyx,joyy = 135; int accx,accy = 125; int zbut,cbut = 0; int ledpin = 6; int wheelServo = 9; int motorServo = 10; int lightvalue = 255; char nunchuckData; int val = 0; int tempval = 0; int wheelPos; int motorPos; int buttonState = 0; int lightMode = 0; Servo steer; Servo move; NewSoftSerial xbee(2,3); //NewSoftSerial mySerial(int rx, int tx); void setup() { // Serial.begin(57600); // Serial.print("Nunchuck ready\n"); xbee.begin(57600); steer.attach(9); move.attach(10); pinMode(ledpin, OUTPUT); } void loop() { if (zbut) { if (joyy < 130) { motorPos = 92+((135-joyy)/5); if (motorPos > 135) motorPos = 135; move.write(motorPos); } if (joyy > 140) { motorPos = 92-((joyy-135)/5); if (motorPos < 45) motorPos = 45; move.write(motorPos); } } else { if ((joyy > 130) && (joyy < 140)) { move.write(92); } } if (accx > 125) { wheelPos = 90-(accx-125); if (wheelPos < 45) wheelPos = 45; steer.write(wheelPos); } if (accx < 125) { wheelPos = 90+(125-accx); if (wheelPos > 135) wheelPos = 135; steer.write(wheelPos); } if (cbut != buttonState) { // the button state has changed! if (cbut == 1) { // check if the button is pressed if (lightMode == 0) { // light is off lightMode = 1; // light is on! digitalWrite(ledpin, HIGH); } else { lightMode = 0; // light is on! digitalWrite(ledpin, LOW); } } } buttonState = cbut; // save the new state in our variable while(xbee.available()) { tempval = xbee.read(); if (tempval=='x') { nunchuckData='x'; val=0; } else if (tempval=='y') { nunchuckData='y'; val=0; } else if (tempval=='X') { nunchuckData='X'; val=0; } else if (tempval=='Y') { nunchuckData='Y'; val=0; } else if (tempval=='Z') { nunchuckData='Z'; val=0; } else if (tempval=='C') { nunchuckData='C'; val=0; } else if (tempval >='0' && tempval < = '9') { val=val * 10; val=val + (tempval - '0'); if (nunchuckData=='x'){ joyx=val; } else if (nunchuckData=='y'){ joyy=val; } else if (nunchuckData=='X'){ accx=val; } else if (nunchuckData=='Y'){ accy=val; } else if (nunchuckData=='Z'){ zbut=val; } else if (nunchuckData=='C'){ cbut=val; } } } //end of input in serial buffer }
The NewSoftSerial library is a great way to use any two Arduino pins as another serial port.