Hi,
I want to read byte #11 and byte#12, e.g. 0x02 and 0x7D and combine them with word() to make one number which is the voltage from the sending pot.
I can’t for the life of me figure out why I can’t isolate byte #11 and byte #12 from Serial.read when I know the data is there.
Setup:
Two xbee series 1
Transparente mode
Arduino Mega
Logic level converter - between the mega and receiving xbee
Using this program I can read the data being transmitted from the Transmitting xbee (connected to a pot) to the receiving xbee which is connected to a logic level converter then to the Mega Serial1 port.
int data = 0;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
if(Serial1.available() > 0) // checks for data
{
data = Serial1.read(); // reads data from Serial1
if(data <= 0x0F) // prints a leading zero on Serial.print if
// Hex is less than or equal to 15 as arduino
// wont print a leading 0. This took some time to figure out with help.
{
Serial.print("0");
}
Serial.println(data,HEX);
}
}
The HEX results are
7E 00 0A 83 12 34 1B 00 01 02 00 02 7D 9A then back to 7E…
I need the payload 02 7D bytes #11 and #12 after the delimiter.
Problem:
I can’t figure out why the folowing code does not work. I just want to see bytes 02 and 7D so I can combine them and using the arduino word() function put them together 027D (637 decimal) to drive a motor… I tried and tried to figure it out…
int hibyte = 0; //byte # 11
int lowbyte = 0; // byte # 12
int data = 0;
int Voltage = 0;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
if(Serial1.available() > 0) // checks for data
{
data = Serial1.read(); // reads data from Serial
if(data==0x7E) //start delimiter
{
for(int i = 1; i <=10; i++) //counter
{
Serial1.read(); //removes bytes 1 through 10 from the buffer
}
hibyte = Serial1.read();
lowbyte = Serial1.read();
Serial.print(hibyte);
Serial.print(lowbyte);
}
}
}
After running this code the result is -1-1-1-1-1- forever. I know there is data there as I can see it from the 1st program above. What am I doing wrong?
Thank you so much!!