left side wheels do not move in Obstacle Avoiding Robot

thank you very much for finding this post. I am not good at English language, though, I would like to find some help. I am making Obstacle Avoiding Robot with arduino UNO. 4 wheels.

I am very beginner in programming etc. I copied from other website and modified. But only left side wheels do not revolve.

Could you find any mistakes in this code?

● Even if I disconnect V of servo motor, left wheels do not revolve.
Even if I disconnect 2 dc motors of back side, left wheel does not revolve.
(I imagined there was a shortage in battery)

**************** mod edit - code Auto formatted and posted in code tags

#include <NewPing.h>

#include <Servo.h>

//our L298N control pins
const int LeftMotorForward = 8;
const int LeftMotorBackward = 9;
const int RightMotorForward = 6;
const int RightMotorBackward = 7;
int PIN_R_VREF = 5; // PWM
int PIN_L_VREF = 10; // PWM

//sensor pins
#define trig_pin A0 //analog input 1
#define echo_pin A1 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup()
{
  Serial.begin(9600);//
  // モーターの回転速度を中間にする
  analogWrite(PIN_R_VREF, 150);
  analogWrite(PIN_L_VREF, 250);
  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  servo_motor.attach(11); //our servo pin
  servo_motor.write(95);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop()
{
  Serial.print(distance);//
  Serial.println("cm");
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);
  if (distance <= 20)
  {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);
    if (distanceRight >= distanceLeft) //modifiyAdd right
    {
      turnRight();
      moveStop();
    }
    else
    {
      turnLeft();
      moveStop();
    }
  }
  else
  {
    moveForward();
  }
  distance = readPing();
}

int lookRight()
{
  servo_motor.write(40);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(95);
  return distance;
}

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

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

void moveStop()
{
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward()
{
  if (!goesForward)
  {
    goesForward = true;
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
    analogWrite(PIN_R_VREF, 150);
    analogWrite(PIN_L_VREF, 250);
  }
}

void moveBackward()
{
  goesForward = false;
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  analogWrite(PIN_R_VREF, 150);
  analogWrite(PIN_L_VREF, 250);
}

void turnRight()
{
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);
  analogWrite(PIN_R_VREF, 150);
  analogWrite(PIN_L_VREF, 250);
  delay(500);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  analogWrite(PIN_R_VREF, 150);
  analogWrite(PIN_L_VREF, 250);
}

void turnLeft()
{
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  analogWrite(PIN_R_VREF, 150);
  analogWrite(PIN_L_VREF, 250); //
  delay(500);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  analogWrite(PIN_R_VREF, 150);
  analogWrite(PIN_L_VREF, 250);
}

If you swap the left and right motor connections which motor runs ?

thank you very much for your prompt reply.

“If you swap the left and right motor connections which motor runs ?”

only Right side wheels do not revolve.

batteries are 3.7 v Li-ion Rechargeable, TF 14500, 900mAh * two

then in the case of code below, it works good.

but this does not include servo motor function beneath ultrasonic sensor . So ultrasonic sees only front side and this car bumps many time.

int PIN_R_IN1 = 6;
int PIN_R_IN2 = 7;
int PIN_R_VREF = 5; // PWM

int PIN_L_IN1 = 9;
int PIN_L_IN2 = 8;
int PIN_L_VREF = 10; // PWM

int TRIGGER_PIN = A0;
int ECHO_PIN = A1;

boolean OnFlg = false;

void setup(){
Serial.begin(9600);

pinMode(PIN_R_IN1,OUTPUT);
pinMode(PIN_R_IN2,OUTPUT);
pinMode(PIN_L_IN1,OUTPUT);
pinMode(PIN_L_IN2,OUTPUT);

pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(12,OUTPUT);//buzzer

digitalWrite(TRIGGER_PIN, HIGH);

// motor speed
analogWrite(PIN_R_VREF,100);
analogWrite(PIN_L_VREF,100);
}

void loop(){
// [超音波の発射]
// ※トリガーとなるHIGH時間は「10us ~ 60ms」まで
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(5);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(18);
digitalWrite(TRIGGER_PIN, LOW);

// [距離の取得]
// ※echoピンに入力されるパルスの長さを検出する(HIGH時間の取得/μ秒)
int duration = pulseIn(ECHO_PIN, HIGH);
if (duration > 0) {
// 距離(cm) = ECHOのHIGH時間 x 超音波速度(0.034cm/μ秒) / 2
float distance = (duration * 0.034) / 2;
Serial.print(distance);
Serial.println(“cm”);

if(distance >= 15){
if (!OnFlg){
// forward
Serial.println(“ok”);
digitalWrite(PIN_R_IN1,HIGH);
digitalWrite(PIN_R_IN2,LOW);
digitalWrite(PIN_L_IN1,LOW);
digitalWrite(PIN_L_IN2,HIGH);//aaa
OnFlg = true;
}
}else{
// motor stop
OnFlg = false;
digitalWrite(PIN_R_IN1,LOW);//modifiedByAki
digitalWrite(PIN_R_IN2,LOW);
digitalWrite(PIN_L_IN1,LOW);
digitalWrite(PIN_L_IN2,LOW);
digitalWrite(12,LOW);//buzzer
delay(500);//Aki Add

// go back
digitalWrite(PIN_R_IN1,LOW);
digitalWrite(PIN_R_IN2,HIGH);
digitalWrite(PIN_L_IN1,HIGH);
digitalWrite(PIN_L_IN2,LOW);//
delay(500);

// turn left
digitalWrite(PIN_R_IN1,HIGH);//hLLHaaa
digitalWrite(PIN_R_IN2,LOW);
digitalWrite(PIN_L_IN1,HIGH);
digitalWrite(PIN_L_IN2,LOW);
delay(1250);
}
}
delay(200);

picture of robot car.jpg

#include <Servo.h>
int PIN_L_VREF = 10; // PWM
analogWrite(PIN_L_VREF,100);

From https://www.arduino.cc/en/reference/servo

. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10,

Do you see the problem ?

“. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10”

thank you very much for this great information.

Without your help, I can not find this important information.

I still hope to moderate speed of motor. So I changed location of wires.

However only left motors do not turn.

Modified codes are as below?

Could you point out my mistakes?

#include <NewPing.h>//i hope to moderate motor speed

#include <Servo.h>

//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 8;
const int RightMotorForward = 4;
const int RightMotorBackward = 9;
int PIN_R_VREF = 5; // PWM
int PIN_L_VREF = 6; // PWM

//sensor pins
#define trig_pin A0 //analog input 1
#define echo_pin A1 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){
 Serial.begin(9600);//Aki

 // モーターの回転速度を中間にする
analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

 pinMode(RightMotorForward, OUTPUT);
 pinMode(LeftMotorForward, OUTPUT);
 pinMode(LeftMotorBackward, OUTPUT);
 pinMode(RightMotorBackward, OUTPUT);
 
 servo_motor.attach(11); //our servo pin

 servo_motor.write(95);
 delay(1000);//aki from 2000 to 1000
 distance = readPing();
 delay(100);
 distance = readPing();
 delay(100);
 distance = readPing();
 delay(100);
 distance = readPing();
 delay(100);
 
}

void loop(){

Serial.print(distance);//aki
   Serial.println("cm");
   
 int distanceRight = 0;
 int distanceLeft = 0;
 delay(50);

 if (distance <= 20){
   moveStop();
   delay(300);
   moveBackward();
   delay(400);
   moveStop();
   delay(300);
   distanceRight = lookRight();
   delay(300);
   distanceLeft = lookLeft();
   delay(300);

   if (distanceRight >= distanceLeft){//modifiyAdd right
     turnRight();
     moveStop();
   }
   else{
     turnLeft();
     moveStop();
   }
 }
 else{
   moveForward(); 
 }
   distance = readPing();
}

int lookRight(){  
 servo_motor.write(40);
 delay(200);//aki
 int distance = readPing();
 delay(100);
 servo_motor.write(95);
 return distance;
}

int lookLeft(){
 servo_motor.write(170);
 delay(200);//aki
 int distance = readPing();
 delay(100);
 servo_motor.write(95);
 return distance;
 delay(100);
}

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

void moveStop(){
 
 digitalWrite(RightMotorForward, LOW);
 digitalWrite(LeftMotorForward, LOW);
 digitalWrite(RightMotorBackward, LOW);
 digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

 if(!goesForward){

   goesForward=true;
   
   digitalWrite(LeftMotorForward, HIGH);
   digitalWrite(RightMotorForward, HIGH);
 
   digitalWrite(LeftMotorBackward, LOW);
   digitalWrite(RightMotorBackward, LOW); 

   analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);
 }
}

void moveBackward(){

 goesForward=false;

 digitalWrite(LeftMotorBackward, HIGH);
 digitalWrite(RightMotorBackward, HIGH);
 
 digitalWrite(LeftMotorForward, LOW);
 digitalWrite(RightMotorForward, LOW);

 analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);
 
}

void turnRight(){

 digitalWrite(LeftMotorForward, HIGH);
 digitalWrite(RightMotorBackward, HIGH);
 
 digitalWrite(LeftMotorBackward, LOW);
 digitalWrite(RightMotorForward, LOW);

 analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

 delay(500);
 
 digitalWrite(LeftMotorForward, HIGH);
 digitalWrite(RightMotorForward, HIGH);
 
 digitalWrite(LeftMotorBackward, LOW);
 digitalWrite(RightMotorBackward, LOW);

 analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

  
}

void turnLeft(){

 digitalWrite(LeftMotorBackward, HIGH);
 digitalWrite(RightMotorForward, HIGH);
 
 digitalWrite(LeftMotorForward, LOW);
 digitalWrite(RightMotorBackward, LOW);

 analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);//

 delay(500);
 
 digitalWrite(LeftMotorForward, HIGH);
 digitalWrite(RightMotorForward, HIGH);
 
 digitalWrite(LeftMotorBackward, LOW);
 digitalWrite(RightMotorBackward, LOW);

 analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);
}

Please edit your posts to add code tags ( “</>” button on editor), as described in “How to use the forum”.

However only left motors do not turn.

Do the same test as before and swap the wires (or the pins) between the left and right motors. Which motor runs now ?

". On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10,"

According to your advice, I modified from analogWrite into digitalWrite.

In that case, both side wheels turn correctly and it can avoid obstacles to some extent.

Thank you very much for your help.

It has passed one year after I purchased this car and now progressed greatly thanks to you.

According to your advice, I modified from analogWrite into digitalWrite.

I am glad that you got it working but using digitalWrite() you will not be able to control the speed of the motors if that is important to you

controlling of speed is important for me. So I need to change pin number and use analogWrite().

akitomo: controlling of speed is important for me. So I need to change pin number and use analogWrite().

Yes

The fact that both motors work using digitalWrite() shows that the wiring and power supply is OK and that the program can control the motors

As a matter of interest, how are the motors powered ?

batteries are 3.7 v Li-ion Rechargeable, TF 14500, 900mAh * two

this is current code.

#include <NewPing.h>// モーターの回転速度を中間にするのはやめた

#include <Servo.h>

//our L298N control pins

const int LeftMotorForward = 8;
const int LeftMotorBackward = 9;
const int RightMotorForward = 6;
const int RightMotorBackward = 7;
int PIN_R_VREF = 5; // PWM
int PIN_L_VREF = 10; // PWM

//sensor pins
#define trig_pin A0 //analog input 1
#define echo_pin A1 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){
  Serial.begin(9600);//Aki

  

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  
  pinMode(PIN_R_VREF, OUTPUT);//modified
  pinMode(PIN_L_VREF, OUTPUT);//modified
  
  servo_motor.attach(11); //our servo pin

  servo_motor.write(95);
  delay(3000);//aki from 2000 to 3000
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  
}

void loop(){

Serial.print(distance);//aki
    Serial.println("cm");
    
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 30){ //20 wo 30 he
    moveStop();
    delay(1000);//300 wo
    moveBackward();
    delay(200);//400wo
    moveStop();
    delay(1000);//300
    distanceRight = lookRight();
    delay(1000);//300
    distanceLeft = lookLeft();
    delay(1000);//300wo

    if (distanceRight >= distanceLeft){//modifiyAdd right
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(40);
  delay(2000);//aki
  int distance = readPing();
  delay(100);
  servo_motor.write(95);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(2000);//aki 200 wo
  int distance = readPing();
  delay(100);
  servo_motor.write(95);
  return distance;
  delay(100);
}

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

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
  
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 

    digitalWrite(PIN_R_VREF, HIGH);
    digitalWrite(PIN_L_VREF, HIGH);    
 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);

  digitalWrite(PIN_R_VREF, HIGH);
  digitalWrite(PIN_L_VREF, HIGH);

  delay(200);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);

  digitalWrite(PIN_R_VREF, HIGH);
  digitalWrite(PIN_L_VREF, HIGH);

  delay(200);//500wo
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  digitalWrite(PIN_R_VREF, HIGH);
  digitalWrite(PIN_L_VREF, HIGH);
 
   
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  digitalWrite(PIN_R_VREF, HIGH);
  digitalWrite(PIN_L_VREF, HIGH);

  delay(200);//500wo
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  digitalWrite(PIN_R_VREF, HIGH);
  digitalWrite(PIN_L_VREF, HIGH);
}

Motors not moving Obsticles avoided Harmony abounds

Now, I have modified codes and this obstacles avoidance car is available.

analogWrite() is also available and it can control speed of 4 wheels.

But it sometimes bumps obstacles (edge of door etc).

And I hope to light up LED only at the time of backward.
However it always lights up unfortunately.
I added code . You can find sentence of “it does not work correctly” in this code.

Could you help me?

Thank you very much for your great help.

#include <NewPing.h>//i hope to moderate motor speed

#include <Servo.h>

//our L298N control pins
const int LeftMotorForward = 8;
const int LeftMotorBackward = 9;
const int RightMotorForward = 10;
const int RightMotorBackward = 7;
int PIN_R_VREF = 5; // PWM
int PIN_L_VREF = 6; // PWM

//sensor pins
#define trig_pin A0 //analog input 1
#define echo_pin A1 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){
  Serial.begin(9600);//Aki

  // control motor speed
analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(3, OUTPUT);
  
  servo_motor.attach(11); //our servo pin

  servo_motor.write(95);
  delay(2000);//aki from 2000 to 1000
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  
}

void loop(){

Serial.print(distance);//aki
    Serial.println("cm");
    
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distanceRight >= distanceLeft){//modifiyAdd right
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(40);
  delay(2000);//aki
  int distance = readPing();
  delay(100);
  servo_motor.write(95);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(2000);//aki
  int distance = readPing();
  delay(100);
  servo_motor.write(95);
  return distance;
  delay(100);
}

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

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 

    analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);

  analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

digitalWrite(3, HIGH);//it does not work correctly.
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);

  analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);
 
   
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);//

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);
}

"it does not work correctly"

What does that mean ? Does it light and not turn off, not light at all, something else ?

Thank you very much for your reply. It always light up. And it does not light off.

It always light up. And it does not light off.

That is because you don't ever turn it off

When should it turn off ?

thank you very much for your prompt reply.

I added digitalWrite(3,HIGH) in section of moveBackward.

So, I thought after moveBackward, Led will off automatically.

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);

  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);

  analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

digitalWrite(3, HIGH);//it does not work correctly.

}

I thought after moveBackward, Led will off automatically.

Why would you think that as you don’t tell it to ?

What you need to do is to turn off the LED when the motors are moving in any direction other than backwards, such as in moveForward(), for instance

Can I also suggest that you give the LED pin a name so that the code is easier to read and understand

As your advice, I have modified it.
Thank you very much for your kind advice.

#include <NewPing.h>//moderate motor speed

#include <Servo.h>

//our L298N control pins
const int LeftMotorForward = 8;
const int LeftMotorBackward = 9;
const int RightMotorForward = 10;
const int RightMotorBackward = 7;
int PIN_R_VREF = 5; // PWM
int PIN_L_VREF = 6; // PWM

const int BackwardLed = 3;

//sensor pins
#define trig_pin A0 //analog input 1
#define echo_pin A1 //analog input 2

#define maximum_distance 200
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name


void setup(){
  Serial.begin(9600);//Aki

  // モーターの回転速度を中間にする
analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(BackwardLed, OUTPUT);
  
  servo_motor.attach(11); //our servo pin

  servo_motor.write(95);
  delay(2000);//aki from 2000 to 1000
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  
}

void loop(){

Serial.print(distance);//aki
    Serial.println("cm");
    
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distanceRight >= distanceLeft){//modifiyAdd right
      turnRight();
      moveStop();
    }
    else{
      turnLeft();
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){  
  servo_motor.write(40);
  delay(2000);//aki
  int distance = readPing();
  delay(100);
  servo_motor.write(95);
  return distance;
}

int lookLeft(){
  servo_motor.write(170);
  delay(2000);//aki
  int distance = readPing();
  delay(100);
  servo_motor.write(95);
  return distance;
  delay(100);
}

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

void moveStop(){
  
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(BackwardLed, LOW);
  
}

void moveForward(){

  if(!goesForward){

    goesForward=true;
    
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
  
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 

    analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

digitalWrite(BackwardLed, LOW);
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);

  analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

digitalWrite(BackwardLed, HIGH);
  
}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);

  analogWrite(PIN_R_VREF,250);
analogWrite(PIN_L_VREF,250);

digitalWrite(BackwardLed, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

digitalWrite(BackwardLed, LOW);
 
   
}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  analogWrite(PIN_R_VREF,250);
analogWrite(PIN_L_VREF,250);//

digitalWrite(BackwardLed, LOW);

  delay(500);
  
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);
  
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  analogWrite(PIN_R_VREF,111);
analogWrite(PIN_L_VREF,111);

digitalWrite(BackwardLed, LOW);
}