Stop DC motor inside a forward and backward loop

Hello, i am new with arduino, with my son we are trying to make a bluetooth controlled robot, was cutting and paste code from google/youtube/etc and below the result that work fine for forward,backward,left,right stop with sensors but i am not able to make manually stop with a bluetooth command like stop.
Thanks in advance to who can help us.

#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>


#define TRIGGER_PIN A1
#define ECHO_PIN A0
#define MAX_DISTANCE 300
#define IR A5


AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);


NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

Servo myservo;

String voice;

void setup() {
  Serial.begin(9600);
  myservo.attach(10);
  myservo.write(96);
  pinMode(IR, INPUT);
}

void loop() {
  //int distance = sonar.ping_cm();
  //int IR1 = digitalRead(IR);
  //Serial.println(distance);

  
  while(Serial.available()){
    voice="";
    delay(2);
    voice = Serial.readString();
    //delay(2);
    //Serial.println(voice);
    
    if (voice == "left") {
      left();
    }else if (voice == "left0") { 
      left();
    }else if(voice == "right") {
      right();
    }else if(voice == "right0") {
      right();
    }else if(voice == "dance") {
      dance();
    }else if(voice == "dance0") {
      dance();
    }else if(voice == "stop") {
      Stop();
      voice="";
    }
  }
  while(voice == "forward") {
    forward();
  }
  while(voice == "backward") {
    backward();
  }
  while(voice == "forward0") {
    forward();
  }
  while(voice == "backward0") {
    backward();
  }
}


void forward() {
  int distance = sonar.ping_cm();
  if(distance <= 15){
    Stop();
    voice="";
  }else {
    motor1.setSpeed(255); 
    motor1.run(BACKWARD); 
    motor2.setSpeed(255);
    motor2.run(FORWARD);
    motor3.setSpeed(255); 
    motor3.run(FORWARD); 
    motor4.setSpeed(255); 
    motor4.run(BACKWARD); 
  }
}
void backward() {
  int IR_Sensor = digitalRead(IR);
  if(IR_Sensor == 0) {
    Stop();
    voice="";
  }else {
    motor1.setSpeed(255); 
    motor1.run(FORWARD); 
    motor2.setSpeed(255); 
    motor2.run(BACKWARD); 
    motor3.setSpeed(255); 
    motor3.run(BACKWARD);
    motor4.setSpeed(255); 
    motor4.run(FORWARD);
  }
}
void left() {
  myservo.write(180);
  delay(500);
  myservo.write(96);
  delay(500);
  motor1.run(FORWARD);
  motor1.setSpeed(255);
  motor2.run(BACKWARD);
  motor2.setSpeed(255);
  motor3.run(FORWARD);
  motor3.setSpeed(255);
  motor4.run(BACKWARD);
  motor4.setSpeed(255);
  delay(700);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void right() {
  myservo.write(0);
  delay(500);
  myservo.write(96);
  delay(500);
  motor1.run(BACKWARD);
  motor1.setSpeed(255);
  motor2.run(FORWARD);
  motor2.setSpeed(255);
  motor3.run(BACKWARD);
  motor3.setSpeed(255);
  motor4.run(FORWARD);
  motor4.setSpeed(255);
  delay(700);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void Stop() {
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void dance() {
  myservo.write(40);
    delay(400);
    myservo.write(140);
    delay(400);
    myservo.write(40);
    delay(400);
    myservo.write(140);
    delay(400);
    myservo.write(96);
  for (int i = 0; i <= 12; i++){
    
    motor1.run(FORWARD);
    motor1.setSpeed(170);
    motor2.run(BACKWARD);
    motor2.setSpeed(170);
    motor3.run(FORWARD);
    motor3.setSpeed(170);
    motor4.run(BACKWARD);
    motor4.setSpeed(170);
    delay(100);
    motor1.run(BACKWARD);
    motor1.setSpeed(170);
    motor2.run(FORWARD);
    motor2.setSpeed(170);
    motor3.run(BACKWARD);
    motor3.setSpeed(170);
    motor4.run(FORWARD);
    motor4.setSpeed(170);
    delay(100);
    if (i == 12) {
      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
    }
  }
}

i am not able to make manually stop with a bluetooth command like stop.

Please post your best effort and describe the problem with it

abalest14:
but i am not able to make manually stop with a bluetooth command like stop.

Please explain in more detail what happens when you send the stop command.

My guess is that your WHILE loops may be the problem because they prevent the Arduino from checking for another command.

Try replacing the WHILEs with IF

...R

when i send stop command nothing happen, if i change while into if the motor will run only once and not continuosly. the only way it work is when i change the code like below but the sensor start work very bad and robot crash before stop.

void forward() {
  int distance = sonar.ping_cm();
  if(distance <= 15 || Serial.readString() == "stop"){
    Stop();
    voice="";
  }else {
    motor1.setSpeed(255); 
    motor1.run(BACKWARD); 
    motor2.setSpeed(255);
    motor2.run(FORWARD);
    motor3.setSpeed(255); 
    motor3.run(FORWARD); 
    motor4.setSpeed(255); 
    motor4.run(BACKWARD); 
  }
}

thanks to Robin2 i have solved changing while to if and moving the sensor control inside the loop like this:

if(distance <= 15 && action == "forward"){
    action= "";
    Stop();
  }
  if(IR_Sensor == 0 && action== "backward"){
    action= "";
    Stop();
  }

abalest14:
if i change while into if the motor will run only once and not continuosly.

Then you need a slightly different approach.

You need a variable (let's define it as char motorRun = 'S':wink:

Then your code should be something like this

if(Serial.available()){
    voice="";
    delay(2);
    voice = Serial.readString();
}

if (voice == "left") {
    motorRun = 'L';
}
else if (voice == "left0") { 
    motorRun = 'l'; // lower case L
}
else if(voice == "right") {
    motorRun = 'R';
}
else if(voice == "right0") {
    motorRun = 'r';
}
else if(voice == "dance") {
    motorRun = 'D';;
}
else if(voice == "dance0") {
    motorRun = 'd';;
}
else if(voice == "stop") {
    motorRun = 'S';
}
voice="";

if (motorRun == 'L') {
    left();
}
else if (motorRun == 'l') {
    left();
}
// etc
else if (motorRun == 'S') {
    Stop();
}

Note how the variable motorRun is used to remember which command was received

...R