Elegoo smart robot car kit 4.0 with camera

Currently working on Elegoo smart robot car kit version 4.0 with camera. It is a bit different from the previous version 3.0. I am a beginner and I am facing a problem while understanding the line tracking code. The code that Elegoo has given is quite difficult to understand so I have designed my own code. But the problem is that the robot car keeps deviating from the track and goes over to the sides where it isn't supposed to move. Kindly if anyone knows this help me.

int STBY=3; //standby power system and it must be high for the motor control board to be enabled
int PWMA=5; //must be high for the right wheels to be enabled
int PWMB=6; //must be high for the lest wheels to be enabled 
int BIN1=8; //high for forward movement and low for backward movement 
int AIN1=7; // high for forward movement and low for backward movement 
int delayTime=2000;
int PIN_ITR20001xxxL=A2;
int PIN_ITR20001xxxM=A1;
int PIN_ITR20001xxxR=A0; 
#define MOTOR_SPEED 180 


void setup() {
  // put your setup code here, to run once:
pinMode(AIN1, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(STBY, OUTPUT);
pinMode(PWMA, OUTPUT);
pinMode(PWMB, OUTPUT);
digitalWrite(PWMA, HIGH);
digitalWrite(PWMB, HIGH);
pinMode(PIN_ITR20001xxxL, INPUT);
pinMode(PIN_ITR20001xxxM, INPUT);
pinMode(PIN_ITR20001xxxR, INPUT);

TCCR0B = TCCR0B & B11111000 | B00000010 ;


}

void loop() {
  // put your main code here, to run repeatedly:

  int PIN_ITR20001xxxL = digitalRead(A2);
  int PIN_ITR20001xxxR  = digitalRead(A0);
  int PIN_ITR20001xxxM = digitalRead(A1);

if(PIN_ITR20001xxxR==0 && PIN_ITR20001xxxL==0 && PIN_ITR20001xxxM==1) {
  Forward(); //FORWARD
}

  else if(PIN_ITR20001xxxR==0 && PIN_ITR20001xxxL==1 && PIN_ITR20001xxxM==1) {
    Right(); //Move Right
 }

  else if(PIN_ITR20001xxxR==1 && PIN_ITR20001xxxL==0 && PIN_ITR20001xxxM==1) {
    Left(); //Move Left

  }
  else if(PIN_ITR20001xxxR==1 && PIN_ITR20001xxxL==1 && PIN_ITR20001xxxM==0) {
    Backward(); //Move Backward
}
} 

void Forward(){
  digitalWrite(AIN1, HIGH);
  digitalWrite(BIN1, HIGH);
  // Move
  digitalWrite(STBY, HIGH);
  delay(delayTime);
  // Stationary
  digitalWrite(STBY, LOW);
  delay(delayTime);
}
void Backward(){
   digitalWrite(AIN1, LOW);
  digitalWrite(BIN1, LOW);
  // Move
  digitalWrite(STBY, HIGH);
  delay(delayTime);
  // Stationary
  digitalWrite(STBY, LOW);
  delay(delayTime);
}
void Left(){
  digitalWrite(AIN1, HIGH );
  digitalWrite(BIN1, LOW);
  // Move
  digitalWrite(STBY, HIGH);
  delay(delayTime);
  // Stationary
  digitalWrite(STBY, LOW);
  delay(delayTime);
}
void Right(){
  digitalWrite(AIN1, LOW );
  digitalWrite(BIN1, HIGH);
  // Move
  digitalWrite(STBY, HIGH);
  delay(delayTime);
  // Stationary
  digitalWrite(STBY, LOW);
  delay(delayTime);
  }

You're looking at pin numbers,not values read from the pins.

Edit: oops, no, you've rescoped them.

Your code is evidently making the wrong decisions on how to move, given the sensor information.

It would be a useful exercise to explain those decisions and why you think they are correct, when they obviously are not.

I am a beginner and trying to learn different commands and function. If you could tell what is wrong with the code , that'd be great.

I already explained that the code is making the wrong decisions based on the sensor information.

To see why, print out the sensor information, and the decisions while the robot is moving along the track.

I have deleted your other cross-post @ayesha_rafiq.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Use Serial.print and serial monitor for the steps between sensor readings and motor control output.

Tried this. If I place my hand on the line track module i.e cover it with something black the tires move as long as my hand is there which means that sensor is working all right. But when I place it on the black track the robot deviates. What do I do now?

Write the program to make the correct steering decisions, rather than incorrect ones.

You are the only person who knows what your robot doing wrong.

Reconsider reply #7.

Did you?

If the motors are the same, the vehicle is going to one side because the methods you use to slow, stop, and start the motors favors one motor over the other.

Maybe try the Elegoo sketch?

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