How do you send different bvalues?

Hello thee, I am designing an app that sends one byte and two bytes of data at different times. I am using a Bluetooth HC05 module and an Arduino Mega 2560. But the problem is that when I send two bytes of data from my phone, the Arduino receives it as two sets of one byte of data. Is there a possible way that I can send two bytes of data together? Here is my code below. When I send a two-byte number, it checks only the first if statement.

void loop() 
{
 
  if (Serial.available()==1){
    byte low = Serial.read();
    Serial.print("Received number1: ");
    Serial.print(low);
      
    }
    
 
  if (Serial.available()==2){
    int lowerByte = Serial.read();
    int higherByte = Serial.read()
    int pos1 = lowerByte | (higherByte << 8);
    Serial.print(pos1);
      }
}


here is my app

Did you check what you are really sending? Make an extra label (temporarily for debugging) on your screen, to see what you are sending.

1 Like

When I send a two-byte number, the Arduino receives it as two sets of one-byte numbers. For example, if I send one thousand, it will receive 232 and 3.

You did not understand what I wrote, I think. But also, I overlooked your label2. What is in it?

So, you have a communication problem:

  1. You assume your app is sending a two byte number
  2. Your Arduino seems to get two times a one byte number.

One of those two assumpions is wrong, but which one?
I suggested to catch what you are writing. You are doing that in label2. Is it what you expect?
Also, at the Arduino side, from those few lines it is impossible to see what is really happening. you could try tricks such as reversing the tests. Or maybe show a larger snippet.

Lable2 shows the value of the thumb position of the slider. And you are right, the Arduino gets two times a one-byte number when I send a two-byte number. I want to send a one-byte and a two-byte number at different times. But when I send a two-byte number, the Arduino gets two times a one-byte number.

I still do not trust your Arduino code. And, what is the timer interval of your timer?
Anyway, maybe you can combine those two one byte numbers in the Arduino into one two byte number?
Or, change your logic in a way that works.