i have a rc car im trying to periodicley send a connection verification. the goal is when the timer hits a certain point to send the verification from the remote and says not connected if it doesnt get one back it. and if the timer on the car reaches a point without receiving a verification it stops untill it receives one so not to go speeding of into the world if out of range. the intitial connection works but after that the timeout never goes obove 1. therefor at the 50 mark the connection doesnt get sent but yet it continually says connected and timeout 1
tx
#include <SoftwareSerial.h>
SoftwareSerial xbee(5,4); // TX, RX
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char messageFromRX[numChars] = {0};
boolean newData = false;
boolean connection = false;
int tick = 0;
int timeout = 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);
newData = false;
}
if (!connection && tick == 0) {
Serial.println("Not connected");
tick = 1;
}
if (!connection || timeout >= 50) {
xbee.print("<");
xbee.print("connected");
xbee.print(",");
xbee.println(">");
}
delay(10);
if (strcmp(messageFromRX, "connected") == 0) // test to see if the two strings are equal
{
connection = true;
timeout = 0;
}
if (connection && tick == 1) {
Serial.println("Connected!");
tick = 0;
}
timeout += 1;
if (timeout >= 100){
connection = false;
tick = 0;
timeout = 0;
}
}
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(messageFromRX, strtokIndx);
}
//============
void showParsedData() {
Serial.print(messageFromRX);
}
rx
#include <SoftwareSerial.h>
SoftwareSerial xbee(10,11); // TX, RX
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char messageFromTX[numChars] = {0};
boolean newData = false;
boolean connection = false;
int tick = 0;
int timeout = 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);
newData = false;
}
if (!connection && tick == 0) {
Serial.println("Not connected");
tick = 1;
}
if (strcmp(messageFromTX, "connected") == 0) // test to see if the two strings are equal
{
timeout = 0;
connection = true;
xbee.print("<");
xbee.print("connected");
xbee.print(",");
xbee.println(">");
}
delay(10);
if (connection && tick == 1) {
Serial.println("Connected!");
tick = 0;
}
timeout += 1;
if(timeout >=100){
connection = false;
tick = 0;
}
}
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
}
//============
void showParsedData() {
Serial.print(messageFromTX);
}