The communication between xbee is working great, i have no problem with this.
Great. That puts you one up on a lot of people here.
My only problem is the method to use for comunication between my two arduino. do you understand?
Yes. You are struggling to define a protocol.
A protocol is an agreed upon method for the exchange of information. Both sides (the sender and the receiver) need to understand how the data is going to be presented, and both sides need to present, or interpret, data the same way.
for exemple how can i send and read the value L0 or L1, i have try some code but don´t work, give me the last character ( 0 or 1) or working only from 0 to 9.
I would prefer to see the code that does not work for you.
Sending "L1" is quite simple. Having the receiver know that the packet is complete is hard. I prefer the use of start and end of packet markers for this very reason.
If the sender sends "" instead, this code:
#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';
}
}
can read the packet, and KNOW that the packet consists of the characters "L1".
Where it says "Process the packet", you could put:
char ltr = inData[0];
inData[0] = '0';
This would make inData "01". Then:
int val = atoi(inData);
would make val 1.
So, now you have the letter, in ltr, and the value, in val. You can easily do what you need to with this data.
If the sender sent , that code would result in ltr = 'X' and val = 908, which is perfect for whatever you need to do with the data.
So, now, you have a protocol. Commands consist of a singe letter followed by an integer value of any number of digits, enclosed in angle brackets.