I am currently building a rover that is controlled with a PS2 controller. The controller part works fine, and I am able to control the rover no problem with a direct wired connection. I am adding the XBee link in, and I've hit a snag. The problem is that my Xbee on the rover is not receiving any data, or that the controller is not transmitting it.
I've done the following things to isolate the issue:
- As stated before, the controller interface portion works just fine.
- The rover motor drivers are downstream and thus not a concern, but they work fine too.
- I have attached the two XBee Series 1 modules to the arduino through the Adafruit Xbee Adapter (http://www.adafruit.com/products/126), and verified that they can talk to X-CTU just fine through the adapter.
- Both XBee modules are receiving power when connected to their respective Arduinos.
- Both XBee modules have the same PAN ID (default) and baud rate (9600).
- Both XBee modules are programmed with the correct source and destination addresses via X-CTU
I suspect a code issue. This is my first time working with the SoftwareSerial Library. I am aware of the differences between the current one in Arduino 1.0 (based on NewSoftSerial) and the previous implementation. My code seems simple enough, but I'll be damned if I can figure out what is wrong. See the controller and rover code below.
//Wireless Robot Controller/Transmitter Code
//Input from a standard Playstation2 controller – Analog sticks used for motor speed
//Output to xBee
#include <PS2X_lib.h>
#include <SoftwareSerial.h>
//PS2 controller pins
#define ClkPin 10
#define CmdPin 9
#define AttPin 11
#define DatPin 8
//xBee pins
#define RxPin 2
#define TxPin 3
//create PS2X controller class
PS2X ps2x;
//create xBee serial port interface via software
SoftwareSerial xBee(RxPin, TxPin);
//create controller data array. Element [0] is x-axis, element [1] is y-axis.
byte controllerData[2] = {0, 0};
void setup() {
//opens serial port
Serial.begin(9600);
//Opens SoftwareSerial Port to XBee
xBee.begin(9600);
//configs PS2 controller with previously-defined pins
ps2x.config_gamepad(ClkPin, CmdPin, AttPin, DatPin);
delay(1000);
}//end setup
void loop() {
//read controller
ps2x.read_gamepad();
//gets position from the y-axis of each analog controller stick
controllerData[0] = ps2x.Analog(PSS_LY);
controllerData[1] = ps2x.Analog(PSS_RY);
//Print the stick position
if(controllerData[0] < 100 || controllerData[0] > 155) {
Serial.print(controllerData[0]);
xBee.print(controllerData[0]);
}
else{
Serial.print("DEAD");
}
Serial.print(", ");
if(controllerData[1] < 100 || controllerData[1] > 155) {
Serial.println(controllerData[1]);
xBee.print(controllerData[1]);
}
else{
Serial.println("DEAD");
}
delay(50);
}//end loop
//Wireless Rover Code
//Input from xBee
//Output to Motor Controller
#include <SoftwareSerial.h>
//xBee pins
#define RxPin 2
#define TxPin 3
//create xBee serial port interface via software
SoftwareSerial xBee(RxPin, TxPin);
void setup() {
//opens serial port
Serial.begin(9600);
//Opens SoftwareSerial Port to XBee
xBee.begin(9600);
}//end setup
void loop() {
if (xBee.available() > 1) {
int leftMotor = xBee.read();
int rightMotor = xBee.read();
Serial.print(leftMotor);
Serial.print(", ");
Serial.println(rightMotor);
}
}//end loop
Appreciate any insight that you can give.