Serial.available() and Serial.read() problem

SQ
Im using Software Serial. Can you please tell me why is the highlighted part of the code not working?

Please help us to help you and post your code using < CODE/ > tags so that it can be copied for examination

What do you mean by "not working" ?
Does mode ever equal 0 ?
Are there ever 2 bytes available() to read ?


#include <SoftwareSerial.h>

SoftwareSerial BT(10, 11); // RX, TX

int bt_data; // variable to receive data from the serial port
int mode=0;
int xAxis,yAxis;

void setup(){
Serial.begin(38400);
BT.begin(38400); // Default communication rate of the Bluetooth module
delay(500);
}

void loop() {
  
if (BT.available() > 0){  
bt_data = BT.read();
if(bt_data == 5){Serial.println("MANUAL");mode=0;}    //Manual Android Joystick Application
else if(bt_data == 1){Serial.println("LINEFOLLOWER");mode=1; } //Auto Line Follower Command
else if(bt_data == 3){Serial.println("AUTO");mode=2;}//Auto Obstacle Avoiding Command
}
if (mode == 0){
  while (BT.available() >= 2){
   xAxis = BT.read();
   delay(10);
   yAxis = BT.read();
   Serial.print("Coordinates: ");
   Serial.print(xAxis);
   Serial.print(",");
   Serial.println(yAxis);}
   }


  1. Not working by not displaying anything on the Serial monitor
    2.Yes in the second condition and it is initially 0.
    3.Yes there is 2 Bytes available

Your Serial.begin() is using a very strange baud rate. Why is that and is the Serial monitor set to the same baud rate ?

Which Arduino board are you using and which pins are you using for the BT connection ?

Seeing you full sketch would help

because any available BT data is read in the preceding if statement

Yes thank you. Any advice on howto fix it?

I edited the sketch above to show it fully.
The baud rate is normal and the serial monitor has the same one.

describe in normal words by avoiding programming terms what the wanted behaviour of the code shall be

receive the mode then based on it. then if the manual mode is chosen it should start receiving two other variables.

Is bt data in binary or ASCII?

perhaps, make that 2nd part one of the conditions.

instead of

        else if (bt_data == 3) {        // Auto Obstacle Avoiding Command
            Serial.println ("AUTO");
            mode=2;
       }
    }
    if (mode == 0){
        while (BT.available () >= 2) {

this, but since that 2nd part provides numeric coordinates, use ASCII letters (e.g. 'M", 'L", 'A") instead of digits (i.e. 5, 1, 3)

        else if (bt_data == 3) {        // Auto Obstacle Avoiding Command
            Serial.println ("AUTO");
            mode=2;
       }
    
       else if (mode == 0){
           while (BT.available () >= 2) {
               ...
           }
       }

Thank you. I will try it out

Aha. OK a first description.

I try to describe it in more details

you are sending a first command that reprersents manual mode or "linefollowing"

you are sending the decimal-value 1 or 5

value 5 means set mode 0
value 1 means set mode 1

after the mode has been set you want your code to receive a single byte that represents
a value for x-axis
and then receive another single byte that represents
a value for y-axis

is this a correct description of the wanted behaviour?
Confirm or correct.

just realized this case needs to be first AND before doing the read(). if not in mode 0, do a read and check for the mode 'M', "L', 'A"

hope you understand

For the main part thats it.
just There are 3 modes autopilot also.
Also something i forgot to mention there are other buttons that have other functions that can be set by numeric values also and can be pressed simultaneously while the x and y are being sent so the data is interfering with eachother

This is a much to unprecise description of the functionality.
If you want to speed up finishing your project you should invest time into precise descriptions.

Otherwise you are slowing down finding the solutions because a lot of asking back has to be done

Where are these buttons?
What bluetooth device is sending the commands?

As a step for analysing how the bluetooth sender is sending the data
write a code that does only print the received data to the serial monitor byte for byte

another approach is to use Serial.readBytesUntil() which can be used to read a complete line containing multiple value which then need to be separater using sscanf(), strtok(), ...

the idea is a line contains a mode command, coordinates, ... A coordinate line might be prefixed with a 'C' to more easily distinguish from other commands

That´s correct.

Does the BT-module is able with this baudrate too?

Software serial:

Supported baud rates are: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200 bauds.

38400 is often cited as the highest reliable rate.

As for the BT module, if it responds to

 AT+BAUD6	

with

 OK38400	

that should mean it is willing and able.

If the rate is suspected at all, just dial it back and test again.

a7

The BT module works fine I have tried it many times