Help with Code for Loop

Hello everyone.

I have the following code:

#include <AFMotor.h>     
#include <Servo.h>           
#include <NewPing.h>
#define TRIG_PIN A0 // Pin A0 
#define ECHO_PIN A1 // Pin A1 
#define MAX_DISTANCE 300 // sets maximum usable sensor measuring distance to 300cm
#define MAX_SPEED 74
#define MAX_SPEED_OFFSET 40
#define COLL_DIST 30 // sets distance at which robot stops and reverses to 30cm
#define TURN_DIST COLL_DIST+20 // sets distance at which robot veers away from object
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

AF_DCMotor leftMotor1(1, MOTOR12_1KHZ); 
AF_DCMotor leftMotor2(2, MOTOR12_1KHZ); 
AF_DCMotor rightMotor1(3, MOTOR34_1KHZ);
AF_DCMotor rightMotor2(4, MOTOR34_1KHZ);
Servo myservo;  

String voice;
int leftDistance, rightDistance; //distances on either side
int curDist = 0;
String motorSet = "";
int speedSet = 0;
int cobrelay = 9; //Connect the relay ~CONNECT 0(TX),1(RX)(From bluetooth) TO ARDUINO~
int beep = 5; //Connect the beeper
int flag = 0;
int state = -1;

void setup() {
  myservo.attach(10);  // attaches the servo on pin 10 (SERVO_1 on the Motor Drive Shield to the servo object 
  myservo.write(90); // tells the servo to position at 90-degrees ie. facing forward.
  pinMode(cobrelay, OUTPUT);
  pinMode(beep, OUTPUT);
  digitalWrite(cobrelay, LOW);
  analogWrite(beep, 0);
  Serial.begin(9600);
  delay(1000); // delay for one seconds
 }

void loop() {
  if(Serial.available()>0){
  state = Serial.read();
 if(state == '0'){
    moveNow();
    Serial.print("Robot: Moving");
  }
  else if(state == '1'){
    moveStop();
    Serial.print("Robot: Stopped");
  }
  else if(state == '2'){
    digitalWrite(cobrelay, HIGH);
    Serial.print("Lights: ON");
  }
  else if(state == '3'){
    digitalWrite(cobrelay, LOW);
    Serial.print("Lights: OFF");
  }
  else if(state == '4'){
    analogWrite(beep, 1000); // Send sound signal...
    delay(1000);        // ...for 1 sec
    analogWrite(beep, 0);
    Serial.print("BEEEP!");
  }
  }
}
//-------------------------------------------------------------------------------------------------------------------------------------
void moveNow() {
  while (state == 0 || state == "*move"){
  myservo.write(90);  // move eyes forward
  delay(90);
  curDist = readPing();   // read distance
  if (curDist < COLL_DIST) {changePath();}  // if forward is blocked change direction
  moveForward();  // move forward
  delay(500);
  }
}
void changePath() {
  moveStop();   // stop forward movement
  myservo.write(36);  // check distance to the right
    delay(500);
    rightDistance = readPing(); //set right distance
    delay(500);
    myservo.write(144);  // check distace to the left
    delay(700);
    leftDistance = readPing(); //set left distance
    delay(500);
    myservo.write(90); //return to center
    delay(100);
    compareDistance();
  }

  
void compareDistance()   // find the longest distance
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    turnLeft();
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    turnRight();
  }
   else //if they are equally obstructed
  {
    turnAround();
  }
}




int readPing() { // read the ultrasonic sensor distance
  delay(70);   
  unsigned int uS = sonar.ping();
  int cm = uS/US_ROUNDTRIP_CM;
  return cm;
}
//-------------------------------------------------------------------------------------------------------------------------------------
void moveStop() {leftMotor1.run(RELEASE); leftMotor2.run(RELEASE); rightMotor1.run(RELEASE); rightMotor2.run(RELEASE);}

void moveForward() {
    motorSet = "FORWARD";
    leftMotor1.run(FORWARD);      // turn it on going forward
    leftMotor2.run(FORWARD);      // turn it on going forward
    rightMotor1.run(FORWARD);     // turn it on going forward
    rightMotor2.run(FORWARD);     // turn it on going forward
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    leftMotor1.setSpeed(speedSet);
    leftMotor2.setSpeed(speedSet);
    rightMotor1.setSpeed(speedSet); 
    rightMotor2.setSpeed(speedSet);
    delay(5);
  }
}
//-------------------------------------------------------------------------------------------------------------------------------------
void moveBackward() {
    motorSet = "BACKWARD";
    leftMotor1.run(BACKWARD);     // turn it on going backward
    leftMotor2.run(BACKWARD);     // turn it on going backward
    rightMotor1.run(BACKWARD);    // turn it on going backward
    rightMotor2.run(BACKWARD);    // turn it on going backward
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    leftMotor1.setSpeed(speedSet);
    leftMotor2.setSpeed(speedSet);
    rightMotor1.setSpeed(speedSet); 
    rightMotor2.setSpeed(speedSet); 
    delay(5);
  }
}  
//-------------------------------------------------------------------------------------------------------------------------------------
void turnRight() {
  motorSet = "RIGHT";
  leftMotor1.run(FORWARD);      // turn motor 1 forward
  leftMotor2.run(FORWARD);      // turn motor 2 forward
  rightMotor1.run(BACKWARD);    // turn motor 3 backward
  rightMotor2.run(BACKWARD);    // turn motor 4 backward
  rightMotor1.setSpeed(speedSet+MAX_SPEED_OFFSET);      
  rightMotor2.setSpeed(speedSet+MAX_SPEED_OFFSET);     
  delay(750); // run motors this way for 750        
  motorSet = "FORWARD";
  leftMotor1.run(FORWARD);      // set both motors back to forward
  leftMotor2.run(FORWARD);
  rightMotor1.run(FORWARD);
  rightMotor2.run(FORWARD);      
}  
//-------------------------------------------------------------------------------------------------------------------------------------
void turnLeft() {
  motorSet = "LEFT";
  leftMotor1.run(BACKWARD);      // turn motor 1 backward
  leftMotor2.run(BACKWARD);      // turn motor 2 backward
  leftMotor1.setSpeed(speedSet+MAX_SPEED_OFFSET);     
  leftMotor2.setSpeed(speedSet+MAX_SPEED_OFFSET);    
  rightMotor1.run(FORWARD);     // turn motor 3 forward
  rightMotor2.run(FORWARD);     // turn motor 4 forward
  delay(750); // run motors this way for 750  
  motorSet = "FORWARD";
  leftMotor1.run(FORWARD);      // turn it on going forward
  leftMotor2.run(FORWARD);      // turn it on going forward
  rightMotor1.run(FORWARD);     // turn it on going forward
  rightMotor2.run(FORWARD);     // turn it on going forward
}  
//-------------------------------------------------------------------------------------------------------------------------------------
void turnAround() {
  motorSet = "RIGHT";
  leftMotor1.run(FORWARD);      // turn motor 1 forward
  leftMotor2.run(FORWARD);      // turn motor 2 forward
  rightMotor1.run(BACKWARD);    // turn motor 3 backward
  rightMotor2.run(BACKWARD);    // turn motor 4 backward
  rightMotor1.setSpeed(speedSet+MAX_SPEED_OFFSET);      
  rightMotor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
  delay(1700); // run motors this way for 1700
  motorSet = "FORWARD";
  leftMotor1.run(FORWARD);      // set both motors back to forward
  leftMotor2.run(FORWARD);
  rightMotor1.run(FORWARD);
  rightMotor2.run(FORWARD);      
}

This project is a “car” robot that is being controlled by bluetooth.

What actually does: I can send the command “move” (presented by ‘0’), and it starts moving around and avoiding objects.
I can Open and Close Lights.
And i can activate the buzzer to hear a “beep”.

I have 2 questions.

1st: How to add voice control (via bluetooth) function without breaking any piece of the code?
2nd: When the robot takes the command ‘0’ that means it should start running and avoiding objects.
If i send ‘0’ and then ‘2’ to open up the lights, it should stop running then it will open the lights.(Never)
How i can make it “multitasking” so it will be able to light up even if it’s moving? (I tried to make this as clear as possible)

How i can make it "multitasking"

Rewrite the entire thing so it doesn't use delay for timing. Nothing with blocking code can be responsive.

I think there isn’t anything in your design that says you should have to not be moving in order to turn the lights on or off. Your main loop doesn’t have any delays in it, and should work fine. However, this code concerns me:

void moveNow() {
  while (state == 0 || state == "*move"){
  myservo.write(90);  // move eyes forward
  delay(90);
  curDist = readPing();   // read distance
  if (curDist < COLL_DIST) {changePath();}  // if forward is blocked change direction
  moveForward();  // move forward
  delay(500);
  }
}

First of all, state is declared as an int, yet you are using it to hold characters as they are read in from the serial port. In order for moveNow() to actually be called from the loop() function, state has to be equal to ‘0’, right? Note that 0 isn’t the same as ‘0’. state isn’t equal to 0, nor is it equal to “*move”. You can’t compare integers to character arrays in that manner. How is this code even being executed?

Second, there is nothing in this loop that changes the state variable. This while loop, if it ever gets executed, will loop forever. No other code will ever execute.

I suggest you change the while to an if, and change the conditional to if (state == ‘0’)

Although I still can’t see how that code is being executed, it should solve your dilemma of not being able to turn on the lights while the car is moving.

Jimmus, thank you very much.
You are right about that is declared as an integer, althought that works.