If is not working(again)

This is my final code
everything works perfectly except the auto mode

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

#define TRIG_PIN A4 
#define ECHO_PIN A5 
#define MAX_DISTANCE 200 
#define MAX_SPEED 190 // sets speed of DC  motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor motor1(1,MOTOR12_1KHZ);
AF_DCMotor motor2(3, MOTOR12_1KHZ);
Servo myservo;

String readString;
int LDR = 0; //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0; //that’s a variable to store LDR values
int light_sensitivity = 500;//This is the approx value of light surrounding your LDR
int distance = 100;
int speedSet=0;
boolean goesForward=false;

void setup() 
{
    Serial.begin(9600);          //start the serial monitor with 9600 buad
    pinMode(13, OUTPUT);     //we mostly use 13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabled
    myservo.attach(9);  
    myservo.write(115); 
    delay(2000);
    distance = readPing();
    delay(100);
    distance = readPing();
    delay(100);
    distance = readPing();
    delay(100);
    distance = readPing();
    delay(100);
}

void loop() {
  LDRValue = analogRead(LDR);      //reads the ldr’s value through LDR
  delay(50);        //This is the speed by which LDR sends value to arduino
  if (LDRValue < light_sensitivity) 
      {
        digitalWrite(13, HIGH);
      }
    else
      {
        digitalWrite(13, LOW);
      }
  while (Serial.available()) {
    delay(3);
    char c = Serial.read();           
    readString += c;
  }
  //////////////////////Problem area////////////////////////
  if (readString == "auto mode")
  {
    int distanceR = 0;
  int distanceL = 0;
  delay(40);
  
  if (distance<=15){
    moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
  
  if(distanceR>=distanceL){
    turnRight();
    moveStop();
    }
  else
  {
    turnLeft();
    moveStop();
  }
 }
 else
 {
  moveForward();
 }
 distance = readPing();
  }
 ////////////////////////////end of problem area///////////////////////////////////////////////////////// 
  else if (readString == "go") 
  {
  moveForward();
  }
   if (readString == "left")
  {
    turnLeft();
  }
   else if (readString == "right")
  {
    turnRight();
  }
   else if (readString == "stop")
  {
    moveStop();
  }
   else if (readString == "reverse")
  {
    moveBackward();
  }
readString="";
}
////////////////////////////******************///////////////////////////////////////////
///////////////////////////group declarations///////////////////////////////////////////
int lookRight()
{
    myservo.write(50); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
}

int lookLeft()
{
    myservo.write(170); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
    delay(100);
}

int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}

void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  } 
  
void moveForward() {

 if(!goesForward)
  {
    goesForward=true;
    motor1.run(FORWARD);      
    motor2.run(FORWARD);      
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
   {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
    delay(5);
   }
  }
}

void moveBackward() {
    goesForward=false;
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
    delay(5);
  }
}  

void turnRight() {
  motor1.run(FORWARD);
  motor2.run(BACKWARD);     
  delay(300);
  motor1.run(FORWARD);      
  motor2.run(FORWARD);      
} 
 
void turnLeft() {
  motor1.run(BACKWARD);     
  motor2.run(FORWARD);     
  delay(300);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);
}
/////////////////**********************///////////////////////

What does the problem zone do or not do and what is your expectation what it should do?

You haven' learned from your other thread; "not working" does not mean anything. What do you expect it to do and what is it doing.

other thread

I guess you want to auto mode to keep running, but it probably runs only once, and then you have to type automode again right?
If that is the case try this.

boolean automode = false;

void loop() {
if (readString == "auto mode") {
    automode = true;
}
else {
automode = false;
}

if (automode) {
.... // put here the part that you want to run 
    int distanceR = 0;
  int distanceL = 0;
  delay(40);
  
  if (distance<=15){
    moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
  
  if(distanceR>=distanceL){
    turnRight();
    moveStop();
    }
  else
  {
    turnLeft();
    moveStop();
  }
 }
 else
 {
  moveForward();
 }
 distance = readPing();
  }

Let me know if it worked.

rpt007:
What does the problem zone do or not do and what is your expectation what it should do?

I expect to be an object avoiding robot but it doesn't do anything

Jashman:
I expect to be an object avoiding robot but it doesn't do anything

akatchi:
I guess you want to auto mode to keep running, but it probably runs only once, and then you have to type automode again right?
If that is the case try this.

boolean automode = false;

void loop() {
if (readString == "auto mode") {
    automode = true;
}
else {
automode = false;
}

if (automode) {
.... // put here the part that you want to run
    int distanceR = 0;
  int distanceL = 0;
  delay(40);
 
  if (distance<=15){
    moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
 
  if(distanceR>=distanceL){
    turnRight();
    moveStop();
    }
  else
  {
    turnLeft();
    moveStop();
  }
}
else
{
  moveForward();
}
distance = readPing();
  }




Let me know if it worked.

It didn't work

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

#define TRIG_PIN A4 
#define ECHO_PIN A5 
#define MAX_DISTANCE 200 
#define MAX_SPEED 190 // sets speed of DC  motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor motor1(1,MOTOR12_1KHZ);
AF_DCMotor motor2(3, MOTOR12_1KHZ);
Servo myservo;

String readString;
int LDR = 0; //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0; //that’s a variable to store LDR values
int light_sensitivity = 500;//This is the approx value of light surrounding your LDR
int distance = 100;
int speedSet=0;
boolean goesForward=false;
boolean automode = false;

void setup() 
{
    Serial.begin(9600);          //start the serial monitor with 9600 buad
    pinMode(13, OUTPUT);     //we mostly use 13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabled
    myservo.attach(9);  
    myservo.write(115); 
    delay(2000);
    distance = readPing();
    delay(100);
    distance = readPing();
    delay(100);
    distance = readPing();
    delay(100);
    distance = readPing();
    delay(100);
}

void loop() {
  LDRValue = analogRead(LDR);      //reads the ldr’s value through LDR
  delay(50);        //This is the speed by which LDR sends value to arduino
  if (LDRValue < light_sensitivity) 
      {
        digitalWrite(13, HIGH);
      }
    else
      {
        digitalWrite(13, LOW);
      }
  while (Serial.available()) {
    delay(3);
    char c = Serial.read();           
    readString += c;
  }
  //////////////////////Main area////////////////////////
  if (readString == "auto mode")
  {
    automode = true;
  }
 ////////////////////////////////////////////////////////////////////////////////// 
  else if (readString == "go") 
  {
  moveForward();
  }
   if (readString == "left")
  {
    turnLeft();
  }
   else if (readString == "right")
  {
    turnRight();
  }
   else if (readString == "stop")
  {
    moveStop();
  }
   else if (readString == "reverse")
  {
    moveBackward();
  }
  else {
    automode = false;
  }
  if (automode) {
int distanceR = 0;
  int distanceL = 0;
  delay(40);
  
  if (distance<=15){
    moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
  
  if(distanceR>=distanceL){
    turnRight();
    moveStop();
    }
  else
  {
    turnLeft();
    moveStop();
  }
 }
 else
 {
  moveForward();
 }
 distance = readPing();
}
readString="";
}
////////////////////////////******************///////////////////////////////////////////
///////////////////////////group declarations///////////////////////////////////////////
int lookRight()
{
    myservo.write(50); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
}

int lookLeft()
{
    myservo.write(170); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(115); 
    return distance;
    delay(100);
}

int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}

void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  } 
  
void moveForward() {

 if(!goesForward)
  {
    goesForward=true;
    motor1.run(FORWARD);      
    motor2.run(FORWARD);      
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
   {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
    delay(5);
   }
  }
}

void moveBackward() {
    goesForward=false;
    motor1.run(BACKWARD);      
    motor2.run(BACKWARD);  
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
    motor1.setSpeed(speedSet);
    motor2.setSpeed(speedSet+MAX_SPEED_OFFSET);
    delay(5);
  }
}  

void turnRight() {
  motor1.run(FORWARD);
  motor2.run(BACKWARD);     
  delay(300);
  motor1.run(FORWARD);      
  motor2.run(FORWARD);      
} 
 
void turnLeft() {
  motor1.run(BACKWARD);     
  motor2.run(FORWARD);     
  delay(300);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);
}
/////////////////**********************///////////////////////

Jashman:
It didn't work

Clearly the message isn't getting through to you.

Thread locked.

Imagine walking into a garage and saying "My car doesn't work. It's a white car. Please fix it."

How do you think that would be greeted by the staff?