Bluetooth control - Inverse Byte Order

Hi,

I'm having an issue with the byte order changing after a while of use.
This works great for a while, then it shows some unexpected vlaues.
Output below a sequense from using the BT slide. It shows the
expected values, then the next set of bytes are all wrong ... seem as the byte order is reversed.
Using HC-06 on arduino UNO

Used MIT App Inventor:
image
maxvalue: 1180
minvalue: 1000
*note: bluetooth client has 'HighByteFirst" checked.

Here is the code to see the data:

#include <SoftwareSerial.h> 
int bluetoothTx = 4;
int bluetoothRx = 5;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  bluetooth.begin(9600);
}

void loop()
{
  //Read from bluetooth
  if(bluetooth.available()>= 2 )
  {
    unsigned int msbyte = bluetooth.read();
    unsigned int lsbyte = bluetooth.read();
    Serial.print("MSbyte: ");Serial.println(msbyte);
    delay(10);
    Serial.print("LSbyte: ");Serial.println(lsbyte);
    delay(10);
    unsigned int numsent = (msbyte *256) + lsbyte;
    Serial.print("Num-sent: ");Serial.println(numsent);  
   delay(10);
}
}

Output:
MSbyte: 4
LSbyte: 10
Num-sent: 1034 ... expected

MSbyte: 4
LSbyte: 8
Num-sent: 1032 ... expected

MSbyte: 4
LSbyte: 7
Num-sent: 1031 ... expected

MSbyte: 4
LSbyte: 3
Num-sent: 1027 ... expected

MSbyte: 254
LSbyte: 3
Num-sent: 65027 ... not expected

MSbyte: 245
LSbyte: 3
Num-sent: 62723 ... not expected

MSbyte: 248
LSbyte: 4
Num-sent: 63492 ... not expected

MSbyte: 14
LSbyte: 4
Num-sent: 3588 ... not expected

MSbyte: 35
LSbyte: 4
Num-sent: 8964 ... not expected

Any ideas why this is happening?

Thx,
Ron

Why not expected? 254*256+4
What did you expect?

Hi,

From the app, the slider is only sending the value 1000 to 1180.
If anything, it should have been 3*256 + 254 = 1022.

Most Significant byte should have been 3.
-Ron

you're slipping sync. byte pairs are overlapping and being sensed in reverse order.
add a sync char to the message or sanity check value range. Sync char fixes issue, sanity check masks issue. Sync fix needs to be at both ends. Sanity works if you have no control over rx format.

Hi,

Thanks, I'll give it a try.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.