communication with Bluetooth terminal but not with switch

Hello,

I have copied a sketch, as I am a beginner and trying to operate it with Bluetooth switch from my phone but its not working but via Bluetooth terminal it is working well.Please kindly HELP!!

#include<SoftwareSerial.h>

#define IN1 12
#define IN2 11
#define IN3 10
#define IN4 9
//#define EN1 6
//#define EN2 5

SoftwareSerial mySerial(2, 3); // RX, TX

String data;
int btVal;

void setup() 
{ 
 //Serial.begin(115200);
 pinMode(IN1, OUTPUT);
 pinMode(IN2, OUTPUT);
 pinMode(IN3, OUTPUT);
 pinMode(IN4, OUTPUT);
 //pinMode(EN1, OUTPUT);
 //pinMode(EN2, OUTPUT);
 digitalWrite(IN1, LOW);
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
 //analogWrite(EN1,63);
 //analogWrite(EN2,63);
 mySerial.begin(9600);
}

void loop()
{
 while (mySerial.available())
 { 
 { 
 data = mySerial.readStringUntil('\n');
 //Serial.print(str); 
 } 
 
 btVal = (data.toInt());
 //Serial.print("BlueTooth Value ");
 //Serial.println(btVal); 



 switch (btVal) 
 {
 case 1: 
 //Serial.println("Forward");
 forward();
 break;

 case 2: 
 //Serial.println("Reverse");
 reverse();
 break;

 case 3: 
 //Serial.println("Left");
 left();
 break;
 
 case 4: 
 //Serial.println("Right");
 right();
 break;
 
 case 5: 
 //Serial.println("Stop");
 stoprobot();
 break; 

 }

 } 
 
 
 if (mySerial.available() < 0) 
 {
 //Serial.println("No Bluetooth Data "); 
 }
 
}

void forward()
{
 digitalWrite(IN1, HIGH);
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
}

void reverse()
{
 digitalWrite(IN1, LOW);
 digitalWrite(IN2, HIGH);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, HIGH);
}

void left()
{
 digitalWrite(IN1, LOW);
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
}

void right()
{
 digitalWrite(IN1, HIGH);
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
}

void stoprobot()
{
 digitalWrite(IN1, LOW);
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, LOW);
 digitalWrite(IN4, LOW);
}

Are you sure your phone is sending a ‘\n’ at the end like the terminal was?

No I don’t think so ...How can I do that .please help !!

I have no idea how bluetooth switch works on your phone. You could also alter your code so it simply reads in a single char and acts upon that rather than reading until ‘\n’ and then converting to an integer.

This will change your case statements to use char rather than an int

#include<SoftwareSerial.h>

#define IN1 12
#define IN2 11
#define IN3 10
#define IN4 9
//#define EN1 6
//#define EN2 5

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()
{
  //Serial.begin(115200);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  //pinMode(EN1, OUTPUT);
  //pinMode(EN2, OUTPUT);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  //analogWrite(EN1,63);
  //analogWrite(EN2,63);
  mySerial.begin(9600);
}

void loop()
{
  if (mySerial.available())
  {
    chat data = mySerial.read();

    switch (data)
    {
      case '1':
        //Serial.println("Forward");
        forward();
        break;

      case '2':
        //Serial.println("Reverse");
        reverse();
        break;

      case '3':
        //Serial.println("Left");
        left();
        break;

      case '4':
        //Serial.println("Right");
        right();
        break;

      case '5':
        //Serial.println("Stop");
        stoprobot();
        break;
    }
  }
}

void forward()
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void reverse()
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void left()
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void right()
{
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

void stoprobot()
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

Thanks blh64 for your concern and support.