I have made a line follower using 5 IR sensors using analog pin. I am using pid algo for control of the bot.
Problem that i am facing is that while using L293D for motor control with 12v the motor is not working properly.
I have made the motor connections as per the image…
I have used the following code
long sensors_average;
int sensors_sum;
int position;
int proportional,integral,derivative;
int Kp=4;
int Ki=0.2;
int Kd=1;
int error_value,last_proportional;
int set_point=1998;
long sensors[] = {0, 0, 0, 0, 0}; // Array used to store 5 readings for 5 sensors
int motor_left[]={2,4};
int motor_right[]={7,8};
void setup()
{ Serial.begin(9600);
// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}
void loop()
{ sensors_average = 0;
sensors_sum = 0;
for (int i = 0; i < 5; i++)
{sensors[i] = analogRead(i);
sensors_average += sensors[i] * i * 1000; //Calculating the weighted mean
sensors_sum += int(sensors[i]);} //Calculating sum of sensor readings
pid_calc();
Serial.println();
turn_calc();
delay(500);
}
void pid_calc()
{ position = int(sensors_average / sensors_sum);
proportional=set_point-position;
integral = integral + proportional;
derivative = proportional - last_proportional;
last_proportional = proportional;
error_value = int(proportional * Kp + integral * Ki + derivative * Kd);
Serial.print(error_value);
Serial.print(' ');
}
// restrict error value to +-255.00
void turn_calc() {
if (error_value<-255) {
turn_right();
}
if (error_value>-50 and error_value<50) {
drive_forward();
}
if (error_value>255) {
turn_left();
}
}
// ————————————————————————— Drive
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}
void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
Serial.print('2');
Serial.print(' ');
}
void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
Serial.print('4');
Serial.print(' ');
}
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
Serial.print('1');
Serial.print(' ');
}
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
Serial.print('3');
Serial.print(' ');
}
Pls help ASAP!!!
Moderator edit: I swear, I’m going to start just deleting posts that should use CODE TAGS but don’t.