For this project, I have two microcontroller embedded systems:
Transmitting > Teensy 3.1 processing three encoders and sending the data to system 2) using an XBee
Receiving > Arduino Due using an XBee to receive the data and process it further for the application.
I have an issue here and I am not sure what is causing it. During loop() on my receiving side (Arduino Due), I am outputting the data (serial.print) to the serial monitor continuously. The weird this that is happening is that after the Due has received the data from Teensy system, it then hangs the loop() between 0.5 and 1.5 seconds (it is not consistent). I know this because nothing is being displayed on the serial monitor, there is a pause.
I am assuming that this is a setup with the XBee and I must configure it appropriately - but I figure I would ask the community before crunching hours into fixing the issue.
This application is extensive so I will post the code below that is relative to the problem.
One is that you are misusing Serial.flush(). The flush() method blocks waiting for all pending data to be send. Why you are doing that on the end that only receives data is a mystery.
The second is that the XBee needs time to send the data, and get an acknowledgement, so that it knows that it doesn't need to send the data again. By not having any delay between the sending of one packet and the sending of the next, you are overwhelming the XBees. Try adding a delay(10) at the end of loop(), on the sending end, to give the XBees time to breathe.
One is that you are misusing Serial.flush(). The flush() method blocks waiting for all pending data to be send. Why you are doing that on the end that only receives data is a mystery.
I am not exactly sure, I was experimenting and I must have left it on the receiving end. It did not make a difference when I removed it.
PaulS:
The second is that the XBee needs time to send the data, and get an acknowledgement, so that it knows that it doesn't need to send the data again. By not having any delay between the sending of one packet and the sending of the next, you are overwhelming the XBees. Try adding a delay(10) at the end of loop(), on the sending end, to give the XBees time to breathe.
Yes, I added the delay on the sending end but it does not resolve the underlining issue. If XBee acknowledgement was the issue, shouldnt the loop be hanging after every time data is being sent? This is not what is happening. When encoder data is being sent over continuously, the data packets are received and displayed in the serial monitor. It is only after I stop sending continuous encoder data where it then hangs the loop. I hope this is more clear.
It is only after I stop sending continuous encoder data where it then hangs the loop. I hope this is more clear.
No. I'm sorry. It isn't.
Unless the encoders stop moving, the sender end sends data as fast as an encoder changes position.
On the receiving end, loop() does nothing but print the last received serial data. It receives new data when an encoder changes position, but receiving data is done in a blocking fashion.
You'd be better off sending data with start and end markers, and having the receiver simply store data until the complete packet is received, using code like this:
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Where it says "Process the packet", you'd use strtok() and atoi() to get the data from the packet.
The encoder position information should NOT be printed on every pass through loop(). It should only be printed when a packet of new data is received.