im testing some code to do a connection test between the two arduinos through the xbees. im sending data just fine with my tx side and recieveing fine with my rx side but its when i try to send a connection verification from rx side to tx its not getting recieved. i posted in programming because i switched xbees and get the same result so it seems both xbees are good.
tx code
#include <SoftwareSerial.h>
SoftwareSerial xbee(8, 7); // TX, RX
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char messageFromRX[numChars] = {0};
#include <PS2X_lib.h> //for v1.6
#define PS2_DAT 9 //14
#define PS2_CMD 11 //15
#define PS2_SEL 10 //16
#define PS2_CLK 12 //17
//#define pressures true
#define pressures false
//#define rumble true
#define rumble false
boolean newData = false;
boolean Connection = false;
int LYVALUE;
int LXVALUE;
int oldLYvalue;
int oldLXvalue;
int RYVALUE;
int RXVALUE;
int oldRYvalue;
int oldRXvalue;
int oldCvalue;
int oldDvalue;
PS2X ps2x;
int error = 0;
byte type = 0;
byte vibrate = 0;
int Timeout = 0;
void setup() {
Serial.begin(9600);
xbee.begin(9600);
}
void loop() {
recvWithStartEndMarkers();
parseData();
showParsedData();
CheckConnection();
if (Connection == true) {
ReadGamePad();
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (xbee.available() > 0 && newData == false) {
rc = xbee.read();
Serial.print(rc);
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
strcpy(messageFromRX, strtokIndx); // copy it to messageFromPC
}
//============
void CheckConnection() {
//check for verification its still connected
if (Connection == false) {
Serial.println("press Start to connect");
ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
if (ps2x.ButtonPressed(PSB_START))
{
Serial.println("connected sent");
xbee.print("<");
xbee.print("connected");
xbee.print(",");
xbee.println(">");
}
}
if (strcmp(messageFromRX, "connected") == 0) // test to see if the two strings are equal
{
Connection = true;
Serial.println("Connected");
}
}
void ReadGamePad() {
ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
ps2x.read_gamepad(false, vibrate);
//Stick Values
LYVALUE = ps2x.Analog(PSS_LY);
LYVALUE = map(LYVALUE, 0, 255, 130, 45);
LXVALUE = ps2x.Analog(PSS_LX);
LXVALUE = map(LXVALUE, 0, 255, 39, 120);
RYVALUE = ps2x.Analog(PSS_RY);
RYVALUE = map(RYVALUE, 0, 255, 0, 180);
RXVALUE = ps2x.Analog(PSS_RX);
RXVALUE = map(RXVALUE, 0, 255, 0, 180);
delay(10);
if (LXVALUE < (oldLXvalue - 2) || LXVALUE > (oldLXvalue + 2) || LYVALUE < (oldLYvalue - 2) || LYVALUE > (oldLYvalue + 2) || RXVALUE < (oldRXvalue - 2) || RXVALUE > (oldRXvalue + 2) || RYVALUE < (oldRYvalue - 2) || RYVALUE > (oldRYvalue + 2)) {
Serial.print("<");
Serial.print("LXYValues");
Serial.print(",");
Serial.print(LYVALUE, DEC);
Serial.print(",");
Serial.print(LXVALUE, DEC);
Serial.print(",");
Serial.print(RYVALUE, DEC);
Serial.print(",");
Serial.print(RXVALUE, DEC);
Serial.println(">");
xbee.print("<");
xbee.print("XYValues");
xbee.print(",");
xbee.print(LYVALUE, DEC);
xbee.print(",");
xbee.print(LXVALUE, DEC);
xbee.print(",");
xbee.print(RYVALUE, DEC);
xbee.print(",");
xbee.print(RXVALUE, DEC);
xbee.println(">");
oldLXvalue = LXVALUE;
oldLYvalue = LYVALUE;
oldRXvalue = RXVALUE;
oldRYvalue = RYVALUE;
}
delay(50);
}
void showParsedData() {
Serial.println(messageFromRX);
}
rx side
#include <SoftwareSerial.h>
SoftwareSerial xbee(10, 11); // TX, RX
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char messageFromTX[numChars] = {0};
int Timeout = 0;
boolean newData = false;
boolean Connection = false;
int ServoA = 0;
int ServoB = 0;
int ServoC = 0;
int ServoD = 0;
//============
void setup() {
Serial.begin(9600);
xbee.begin(9600);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() replaces the commas with \0
parseData();
delay(10);
if(Connection == true){
showParsedData();
newData = false;
}
}
CheckConnection();
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (xbee.available() > 0 && newData == false) {
rc = xbee.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() {
// split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
strcpy(messageFromTX, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
ServoA = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
ServoB = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
ServoC = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
ServoD = atoi(strtokIndx);
}
//============
void showParsedData() {
Serial.println(messageFromTX);
Serial.print(ServoA);
Serial.print(", ");
Serial.print(ServoB);
Serial.print(", ");
Serial.print(ServoC);
Serial.print(", ");
Serial.println(ServoD);
}
void CheckConnection() {
if (Connection == false) {
Serial.println("not connected");
}
else if(Connection == true){
Serial.println("connected and doing things");
}
if (strcmp(messageFromTX, "connected") == 0) // test to see if the two strings are equal
{
Connection = true;
delay (50);
xbee.print("<");
xbee.print("connected");
xbee.print(",");
xbee.println(">");
Serial.println("Connected sent");
}
}