Meaning of this code (obstacles avoidance robot)

I am beginner in arduino.

Could you tell me meaning of boolean in below codes?

I know boolean is a kind of variable.
True of false ?

However in the case of below codes, I am not sure what is "boolean" doing ?

"if(!goesForward)" will mean Not go forward.

What is "true " in these codes ?

I am sorry for my incorrect English. I am not good at English.

#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

//our L298N control pins
const int LeftMotorForward = 5;
const int LeftMotorBackward = 4;
const int RightMotorForward = 3;
const int RightMotorBackward = 2;

//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //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(){

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

  servo_motor.write(90);
  delay(2000);
  distance = readPing();//content of readping is distance
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop(){

  int distanceRight = 0;//number of pin?
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20){//front distance is less than20
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();//what is distance right?
    delay(300);
    distanceLeft = lookLeft();
    delay(300);

    if (distance >= distanceLeft){
      turnRight();
      moveStop();
    }
    else{
      turnLeft();//if distanceleff is more than front distance, turn left
      moveStop();
    }
  }
  else{
    moveForward(); 
  }
    distance = readPing();
}

int lookRight(){//Content of lookright  
  servo_motor.write(10);
  delay(500);
  int distance = readPing();
  delay(100);
  servo_motor.write(90);
  return distance;//what is return disance?
}

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

int readPing(){ // name is 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); 
  }
}

void moveBackward(){

  goesForward=false;

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

void turnRight(){

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

void turnLeft(){

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

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

Hello
Take a view in your sketch. Search for the variable "goesForward" and check the contents under the given conditions.

1 Like

As you said the operators inside curly braces

if (!goesForward) {operators ...}

are executed when
!goesForward == true.
So far as "!" means not,
goesForward == false
In human terms, this means: execute {operators ...} if the car is not going forward.

1 Like

Thank you very much for your reply.

I understand that if the car is not going forward, this code orders the car to move forward.
(if the car is moving forward, such order is not necessary. Because it is already doing so)

However I still can not why there is "true".

If there is a question, I will use true or not.
But I can not find such question.

goesForward=true;

Here?

1 Like

I am very sorry to say that I can not catch meaning of your answer.

If you can write full sentence, it is very kind of you.

Though, you are already very kind.

A boolean variable can have only 2 values. They are either "true" or "false".

int a = 1;
int b = 2;

a > b will yeald false.
b > a will yeald true.

1 Like

As you know, a boolean variable can have only two possible values, defined as 'true' or 'false'. The "if" statement is the question. So,

if(goesForward)  {

... in English means "Is the boolean goesForward set to true? If so, do what is written in the following curly brackets."

Does that make sense?

1 Like

The '!' reads the value of goesForward and inverts it, before asking the question about it.

if(!goesForward) {

... means "Is the opposite of the value in goesForward a true? If so do what is written in the following curly brackets."

As the opposite of a true is a false, then it's like asking "Is the value in goesForward a false? If so, do what is written in the following curly brackets."

I'm sorry if this is too trivial, but I couldn't think of another way to explain how the if statement is like a question.

1 Like

I understood this points.
thank you very much.

Thank you very much for your answers and kindness.

If my understanding is correct.

at first, I declare that goesForward is false.
It means NOt goesForward it true.

As for "void moveForward(){
if(!goesForward){
goesForward=true(???); "

Computer asks me whether "not goesforward" is true.

Then I answer it is true.

Then, computer will execute my order and robot goes forward.

However there is sentence of " goesForward=true".

I am not sure why this sentence is necessary.

Probably, I am misunderstanding basic points.

"if" is asking me whether it is true.
if it is true, order in curly brace should be executed.
Is my understanding correct?

This means "Does the Boolean variable called "goesForward" have the value "false"?".

If it does have the value 'false" (that is the whole condition was true), the next line sets goesForward to true

1 Like

goesForward is a boolean variable that represents whether the robot is going forwards. It is like a flag that says "Yes, the robot is moving forwards".

So, when you call the function moveForward(), the first thing it does is look at the goesForward flag to see if the robot is already moving forward. If goesForward is not true, then it knows the robot is not already moving forward, so it runs the code between the {} braces. That code starts the motors which move it forward, and sets the goesForward flag to true.

If, when you call moveForward(), the goesForward flag is true then it knows the robot is already moving forward so no action is necessary. The code between the braces is skipped and the function simply returns without changing anything.

Does that make sense?

1 Like

thank you very much for writing full sentences.
I have at last understood from bottom of my heart.

1 Like

thank you very much for teaching me.
I have understood at last.

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.