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);
}