Do I need to use separate function in PID algorithm to make the bot go LEFT or RIGHT?
/**************************
Author-
Project- PID Code V-2
**************************/
float Kp = 0, Ki = 0, Kd = 0;
float error = 0, P = 0, I = 0, D = 0, PID_value = 0;
float previous_error = 0, previous_I = 0;
int sensor[5] = {0, 0, 0, 0, 0};
int initial_motor_speed = 100;
void read_sensor_values(void);
void calculate_pid(void);
void motor_control(void);
void motor_left(void);
void motor_right(void);
void setup()
{
pinMode(3, OUTPUT); //PWM Pin 1;9;EN PIN
pinMode(5, OUTPUT); //PWM Pin 2;10;EN PIN
pinMode(12, OUTPUT); //Left Motor Pin 1;4
pinMode(13, OUTPUT); //Left Motor Pin 2;5
pinMode(7, OUTPUT); //Right Motor Pin 1;6
pinMode(8, OUTPUT); //Right Motor Pin 2;7
Serial.begin(9600); //Enable Serial Communications
}
void loop()
{
read_sensor_values();
delay(15);
calculate_pid();
delay(15);
motor_control();
}
void read_sensor_values()
{
sensor[0] = digitalRead(A0);
Serial.print("Sensor[0]:");
Serial.println(sensor[0]);
sensor[1] = digitalRead(A1);
Serial.print("Sensor[1]:");
Serial.println(sensor[1]);
sensor[2] = digitalRead(A2);
Serial.print("Sensor[2]:");
Serial.println(sensor[2]);
sensor[3] = digitalRead(A3);
Serial.print("Sensor[3]:");
Serial.println(sensor[3]);
sensor[4] = digitalRead(A4);
Serial.print("Sensor[4]:");
Serial.println(sensor[4]);
delay(3);
// for(int i=0; i<5; i++)
// {
// sensor[i] = digitalRead(i);
// Serial.print("Sensor[i]:");
// Serial.println(sensor[i]);
// }
if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[4] == 0) && (sensor[4] == 1))
error = 4;
else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[4] == 1) && (sensor[4] == 1))
error = 3;
else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[4] == 1) && (sensor[4] == 0))
error = 2;
else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 1) && (sensor[4] == 1) && (sensor[4] == 0))
error = 1;
else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 1) && (sensor[4] == 0) && (sensor[4] == 0))
error = 0;
else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 1) && (sensor[4] == 0) && (sensor[4] == 0))
error = -1;
else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 0) && (sensor[4] == 0) && (sensor[4] == 0))
error = -2;
else if ((sensor[0] == 1) && (sensor[1] == 1) && (sensor[2] == 0) && (sensor[4] == 0) && (sensor[4] == 0))
error = -3;
else if ((sensor[0] == 1) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[4] == 0) && (sensor[4] == 0))
error = -4;
else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[4] == 0) && (sensor[4] == 0))
if (error == -4) error = -5;
else error = 5;
}
void calculate_pid()
{
P = error;
I = I + previous_I;
D = error - previous_error;
PID_value = (Kp * P) + (Ki * I) + (Kd * D);
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;
// Constraining the motor speed:
constrain(left_motor_speed, 0, 255);
constrain(right_motor_speed, 0, 255);
analogWrite(3, initial_motor_speed - PID_value); //Left Motor Speed
analogWrite(5, initial_motor_speed + PID_value); //Right Motor Speed
//goForward:
/*The pin numbers and high, low values are configurable depending on connections */
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
}