Im having real trouble achieving consistent communication with two arduino and xbees. Essentially on my sending module I have some pots that I can alter integers with, these values are then sent over the xbees (which is essentially just serial commands), and they are recieved by the reciever module and processed. There is a 12ms window in which these values are processed and about 8ms to recieve them, when the reciever is ready it sends an 'H' to the sender to start the transfer of the next set of values.
The problem is that when I move the pots, I can only see the first value changing on the reciever COM port (the others just remain at the constant they initialise to at the start), and it often starts just printing out the first digit (although the right first digit as they are 3 figure integers and I can still see it changing), or freezing and printing out the same number over and over, or printing out random numbers completely etc etc
Im really stuck and I dont know why this is happening.
My sender code:
...... other code to get variables
if (Serial.available()) {
incomingByte = Serial.read();
if (incomingByte == 'H') {
Serial.print("<");
Serial.print(Throttledelay);
Serial.print(" ");
Serial.print(Aelerondelay);
Serial.print(" ");
Serial.print(Elevatordelay);
Serial.print(" ");
Serial.print(Yawdelay);
Serial.print(" ");
Serial.print(autoflag);
Serial.print(">");
collect = 1;
}
}
My receiver code:
// end of 12ms delay, print out integers used and send for next batch
Serial.print('H');
Serial.print(Throttledelay);
Serial.print(" ");
Serial.print(Aelerondelay);
Serial.print(" ");
Serial.print(Elevatordelay);
Serial.print(" ");
Serial.print(Yawdelay);
Serial.print(" ");
Serial.println(autoflag);
}
void loop() { // Main loop to recieve wireless data from pots
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '<')
{
started = true;
index = 0;
inData[index] = '\0';
}
else if(aChar == '>')
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
inData[index] = '\0';
}
}
if(started && ended)
{
// Convert the string to an integer
char *token;
if (token = strtok(inData, " "))
{
Throttledelay = atoi(token);
}
if (token = strtok(NULL, " "))
{
Aelerondelay = atoi(token);
}
if (token = strtok(NULL, " "))
{
Elevatordelay = atoi(token);
}
if (token = strtok(NULL, " "))
{
Yawdelay = atoi(token);
}
if (token = strtok(NULL, " "))
{
autoflag = atoi(token);
}
// Get ready for the next time
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Any help I greatly appreciate
thanks