Line Follower Robot Arduino
Line Follower Robot Arduino
Line Follower Robot Arduino
// Motor Variables
int ENA = 6;
int motorInput1 = 7;
int motorInput2 = 8;
int motorInput3 = 9;
int motorInput4 = 10;
int ENB = 11;
// PID Constants
float Kp = 25;
float Ki = 0;
float Kd = 15;
int flag = 0;
void setup()
{
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
pinMode(sensor3, INPUT);
pinMode(sensor4, INPUT);
pinMode(motorInput1, OUTPUT);
pinMode(motorInput2, OUTPUT);
pinMode(motorInput3, OUTPUT);
pinMode(motorInput4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
void read_sensor_values()
{
sensor[0] = !digitalRead(sensor1);
sensor[1] = !digitalRead(sensor2);
sensor[2] = !digitalRead(sensor3);
sensor[3] = !digitalRead(sensor4);
/*
Serial.print(sensor[0]);
Serial.print("\t");
Serial.print(sensor[1]);
Serial.print("\t");
Serial.print(sensor[2]);
Serial.print("\t");
Serial.println(sensor[3]);*/
void calculate_pid()
{
P = error;
I = I + previous_I;
D = error - previous_error;
previous_I = I;
previous_error = error;
}
void motor_control()
{
// Calculating the effective motor speed:
int left_motor_speed = initial_motor_speed - PID_value;
int right_motor_speed = initial_motor_speed + PID_value;
// The motor speed should not exceed the max PWM value
left_motor_speed = constrain(left_motor_speed, 0, 255);
right_motor_speed = constrain(right_motor_speed, 0, 255);
/*Serial.print(PID_value);
Serial.print("\t");
Serial.print(left_motor_speed);
Serial.print("\t");
Serial.println(right_motor_speed);*/
void forward()
{
/*The pin numbers and high, low values might be different depending on your
connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, HIGH);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, HIGH);
}
void reverse()
{
/*The pin numbers and high, low values might be different depending on your
connections */
digitalWrite(motorInput1, HIGH);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, HIGH);
digitalWrite(motorInput4, LOW);
}
void right()
{
/*The pin numbers and high, low values might be different depending on your
connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, HIGH);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, LOW);
}
void left()
{
/*The pin numbers and high, low values might be different depending on your
connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, HIGH);
}
void sharpRightTurn() {
/*The pin numbers and high, low values might be different depending on your
connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, HIGH);
digitalWrite(motorInput3, HIGH);
digitalWrite(motorInput4, LOW);
}
void sharpLeftTurn() {
/*The pin numbers and high, low values might be different depending on your
connections */
digitalWrite(motorInput1, HIGH);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, HIGH);
}
void stop_bot()
{
/*The pin numbers and high, low values might be different depending on your
connections */
digitalWrite(motorInput1, LOW);
digitalWrite(motorInput2, LOW);
digitalWrite(motorInput3, LOW);
digitalWrite(motorInput4, LOW);
}