Help me with my robot please

Hello to everyone.I have make a bleutooth robot which i can control it via android(tablet) and i have a problem with the code.I want from the robot to stop when connection lost or bluetooth disconnected.Now when the connection lost the robot go ahead and falls on the objects.How to add this line on the code?Can you help me please?Thank you for your time

The bluetooth that i use is HC-05

My code:

#include <Servo.h>

Servo SERVO_1; // Initialize Servo1

// Motor Control Variables
int PWM1 = 9;
int ENABLE1 = 8;
int PWM2 = 5;
int ENABLE2 = 7;
int PWM3 = 3;
int ENABLE3 = 4;
int PWM4 = 6;
int ENABLE4 = 2;

void setup() {
  SERVO_1.attach(10);
  Serial.begin(9600);
  pinMode(ENABLE1, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(ENABLE2, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(ENABLE3, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(ENABLE4, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος

}

void loop() {


  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
      case 'F':
        moveForward();
        Serial.println("Going forward");
        break;
      case 'L' : // Case 'L' is received,
        SERVO_1.write (180); // Στρίψε Αριστερά.
        SERVO_1.attach(10);
        break;
      case 'N':
        turnright();
        Serial.println("Turning right");
        break;
      case 'M':
        turnleft();
        Serial.println("Turning left");
        break;
      case 'O' : // Case 'L' is received,
        SERVO_1.write (0); // Στρίψε Αριστερά.
        SERVO_1.attach(10);
        break;
      case 'B':
        moveBackward();
        Serial.println("Going forward");
        break;
      case 'P':
        SERVO_1.write(90); // Στρίψε Αριστερά.
        SERVO_1.attach(10);
        break;
      case 'S':
        moveNone();
        Serial.println("Stopping");
        break;
      default:
        // if nothing matches, do nothing
        break;
    }
  }
}
void moveForward() {
  // turn the driving motor on to go forwards at set speed
  digitalWrite(ENABLE1, HIGH);
  digitalWrite(ENABLE2, HIGH);
  digitalWrite(ENABLE3, HIGH);
  digitalWrite(ENABLE4, HIGH);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);

}

void moveBackward() {
  // turn the driving motor on to go backwards at set speed
  digitalWrite(ENABLE1, LOW);
  digitalWrite(ENABLE2, LOW);
  digitalWrite(ENABLE3, LOW);
  digitalWrite(ENABLE4, LOW);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);
}
void turnright() {
  digitalWrite(ENABLE1, HIGH);
  digitalWrite(ENABLE2, HIGH);
  digitalWrite(ENABLE3, LOW);
  digitalWrite(ENABLE4, LOW);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);
}

void turnleft() {
  digitalWrite(ENABLE1, LOW);
  digitalWrite(ENABLE2, LOW);
  digitalWrite(ENABLE3, HIGH);
  digitalWrite(ENABLE4, HIGH);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);
}
void moveNone() {
  // turn the driving motor off
  digitalWrite(ENABLE1, 0);
  digitalWrite(ENABLE2, 0);
  digitalWrite(ENABLE3, 0);
  digitalWrite(ENABLE4, 0);
  analogWrite(PWM1, 0);
  analogWrite(PWM2, 0);
  analogWrite(PWM3, 0);
  analogWrite(PWM4, 0);
  SERVO_1.detach();
}

jack1992: I want from the robot to stop when connection lost or bluetooth disconnected.

Does your Bluetooth module provide a way to detect that the connection is lost?

So the problem comes down to can you detect when there is no bluetooth connection ?

Can you arrange for serial data (not one of your controls) to be sent periodically from the Android application ? If so, then you could revise the program to detect when no serial data is detected after a period of time.

UKHeliBob: So the problem comes down to can you detect when there is no bluetooth connection ?

Can you arrange for serial data (not one of your controls) to be sent periodically from the Android application ? If so, then you could revise the program to detect when no serial data is detected after a period of time.

Thank you all for your replies.Can you give an example on how to do this please?

Can you give an example on how to do this please?

Have you seen the stupid cell phone commercials "can you hear me now"? Your Arduino needs to ask that. Your Android needs to answer. Is your Android prepared to answer that? If not, it is pointless to discuss how to ask.

PaulS: Have you seen the stupid cell phone commercials "can you hear me now"? Your Arduino needs to ask that. Your Android needs to answer. Is your Android prepared to answer that? If not, it is pointless to discuss how to ask.

if you don't want don't answer.What do you think you are smart and everyone else is stupid?

and everyone else is stupid?

Nope. Just you.

PaulS: Nope. Just you.

ok mr clever.Remove the PaulS and write genius.I think fits you better.I don't waste my time for you.Goodnight GENIUS.

Can you give an example on how to do this please?

Did you write the Android program ?

PaulS:
Have you seen the stupid cell phone commercials “can you hear me now”? Your Arduino needs to ask that. Your Android needs to answer. Is your Android prepared to answer that? If not, it is pointless to discuss how to ask.

You seem to have been angered at this post, but it describes exactly what you need to do. Pity you let your feelings get in the way of fixing your robot.

I searched on the internet and i found that when the STATE pin is low, it carries on doing what it did last(that’t why falls on the objects).I have to put on the code the ELSE statement and write MOVENONE.I modify the code and now i have this, but it doesn’t stop.Actually it doesn’t move.So what am i doing wrong?

#include <Servo.h>

Servo SERVO_1; // Initialize Servo1

// Motor Control Variables
int PWM1 = 9;
int ENABLE1 = 8;
int PWM2 = 5;
int ENABLE2 = 7;
int PWM3 = 3;
int ENABLE3 = 4;
int PWM4 = 6;
int ENABLE4 = 12;
int BTState = 2;

void setup() {
  SERVO_1.attach(10);
  Serial.begin(9600);
  pinMode(ENABLE1, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(ENABLE2, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(ENABLE3, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(ENABLE4, OUTPUT);  //Δήλωση όλων των μεταβλητών ως έξοδος
  pinMode(BTState, INPUT);
}

void loop() {

  if (digitalRead(BTState) == LOW)
  {
    //remain empty
  }
  else
  {
    moveNone;
  }
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();
    // action depending on the instruction
    // as well as sending a confirmation back to the app
    switch (incomingByte) {
      case 'F':
        moveForward();
        Serial.println("Going forward");
        break;
      case 'L' : // Case 'L' is received,
        SERVO_1.write (180); // Στρίψε Αριστερά.
        SERVO_1.attach(10);
        break;
      case 'N':
        turnright();
        Serial.println("Turning right");
        break;
      case 'M':
        turnleft();
        Serial.println("Turning left");
        break;
      case 'O' : // Case 'L' is received,
        SERVO_1.write (0); // Στρίψε Αριστερά.
        SERVO_1.attach(10);
        break;
      case 'B':
        moveBackward();
        Serial.println("Going forward");
        break;
      case 'P':
        SERVO_1.write(90); // Στρίψε Αριστερά.
        SERVO_1.attach(10);
        break;
      case 'S':
        moveNone();
        Serial.println("Stopping");
        break;
      default:
        // if nothing matches, do nothing
        break;
    }
  }
}
void moveForward() {
  // turn the driving motor on to go forwards at set speed
  digitalWrite(ENABLE1, HIGH);
  digitalWrite(ENABLE2, HIGH);
  digitalWrite(ENABLE3, HIGH);
  digitalWrite(ENABLE4, HIGH);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);

}

void moveBackward() {
  // turn the driving motor on to go backwards at set speed
  digitalWrite(ENABLE1, LOW);
  digitalWrite(ENABLE2, LOW);
  digitalWrite(ENABLE3, LOW);
  digitalWrite(ENABLE4, LOW);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);
}
void turnright() {
  digitalWrite(ENABLE1, HIGH);
  digitalWrite(ENABLE2, HIGH);
  digitalWrite(ENABLE3, LOW);
  digitalWrite(ENABLE4, LOW);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);
}

void turnleft() {
  digitalWrite(ENABLE1, LOW);
  digitalWrite(ENABLE2, LOW);
  digitalWrite(ENABLE3, HIGH);
  digitalWrite(ENABLE4, HIGH);
  analogWrite(PWM1, 255);
  analogWrite(PWM2, 255);
  analogWrite(PWM3, 255);
  analogWrite(PWM4, 255);
}
void moveNone() {
  // turn the driving motor off
  digitalWrite(ENABLE1, 0);
  digitalWrite(ENABLE2, 0);
  digitalWrite(ENABLE3, 0);
  digitalWrite(ENABLE4, 0);
  analogWrite(PWM1, 0);
  analogWrite(PWM2, 0);
  analogWrite(PWM3, 0);
  analogWrite(PWM4, 0);
  SERVO_1.detach();
}

moveNone; is not a function call. moveNone(); is a function call.

johnwasser: moveNone; is not a function call. moveNone(); is a function call.

Hello sir and thank you for your reply.I try to put moveNone(); but it doesn't move at all.

Your code seems to be taking commands from Serial interface, not bluetooth, but I guess that’s just for testing?

If your program does nothing, it may mean that reading pin BTState returns HIGH. So next step is to research why. And so on.

Rupert909:
So next step is to research why. And so on.

How to do that?via serial monitor and AT commands?Please answer

jack1992: How to do that?via serial monitor and AT commands?Please answer

Write a test program that reports its value continously via Serial.println() for example, to decide why it has or hasn't expected value?