I hope I'm posting in the right place! I have spent days trying to get two XBEEs series 1 connected to two arduinos to communicate analog data reliably but I can't seem to catch a break even after hours of googling and following jeremy blum's tutorial.
I'm trying to get one arduino to read data from a potentiometer and wirelessly transmit that data to another arduino with an xbee, but even when following Jeremy Blum's tutorial I get loads of erroneous data in the Serial output of the receiving arduino. This is even after using Serial.flush() at the end of my receiving arduino's code.
Here's where I'm very confused. When I have the receiving XBEE connected through an XBEE explorer, it displays the data PERFECTLY the way I want it! I set the transmitting arduino to read between 0 and 255, when I turn the knob, the other xbee picks it up perfectly so long as it is in the explorer. When I place the receiving XBEE into an arduino, it prints 255 out one byte at a time, i.e. instead of printing '255', it prints 255 as three separate integers, each integer gets printed on a new line. Then, most frustrating of all, I get two random negative numbers every time it prints out my value.
i.e. it prints like this:
2
5
5
-35
-38
instead of like this
255
255
255
Any idea of what I could be doing wrong? I thought it may have been an error that occurred because I was reading on the computer terminal the Serial data from the receiving arduino as the arduino was receiving the information, but setting up a pwm LED test yielded the same weird flicker I would have expected from getting a 2, a 5 and another 5 with the negative numbers in there. Thank you!
Here is my code on the sending side:
int pot = 5;
int potval = 0;
This is even after using Serial.flush() at the end of my receiving arduino's code.
I fail to see how waiting, on the receiving end, for all data that is has buffered for output to actually be sent is going to help anything. 99.9% of the people that call Serial.flush() shouldn't be using it. You are no exception.
Serial.println(potval);
This is going to send something like '1', '4', '5', , .
data = Serial.read()- '0';
Serial.println(data);
This is going to read the '1', and subtract '0' from it, and then print the integer result followed by a carriage return and line feed. Then, it will read the '4' and do the same. Then, it will read the '5' and do the same. Then, it will read the carriage return and do something stupid, like subtract '0' from it. Now, why would you do that?
I see, thank you for your response, like I said, I'm new to this, I'm just following tutorials I have found online.
That now makes sense as to why the values are being printed one at a time while the carriage return is responsible for the strange negative numbers. Jeremy Blum only sent one byte at a time between 0 and 9 in his tutorial. If I want to send three or two bytes at a time, how would I then do that? Through use of an array? My first thought is to take each element of the array, subtract the char '0' and then have them printed all together after that is done. Does that sound plausible?
This will send something like "<145>" to the XBee for broadcast.
Then, use code like this to read and store the data:
#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", inData will contain "145" (assuming that "<145>" was what was sent). The atoi() function will convert the string to an int:
THANK YOU SO SO SO MUCH. My heart skipped a beat seeing those beautiful three digit numbers, best of all, I understand all of your code! You've made it so easy! Thanks again.