// Define pins for IR sensors, motor drivers, and motor speed variables
const int sensorLeftPin = A0;
const int sensorCenterPin = A1;
const int sensorRightPin = A2;
const int motorLeftEnablePin = 9;
const int motorLeftSpeedPin = 10;
const int motorRightEnablePin = 11;
const int motorRightSpeedPin = 12;
int motorLeftSpeed = 0;
int motorRightSpeed = 0;
// Define PID gains
float Kp = 0.05;
float Ki = 0.01;
float Kd = 0.001;
// Create PID controller object
PID pid(Kp, Ki, Kd0);
void setup() {
// Initialize serial communication
[Link](9600);
// Set motor driver pins as outputs
pinMode(motorLeftEnablePin, OUTPUT);
pinMode(motorLeftSpeedPin, OUTPUT);
pinMode(motorRightEnablePin, OUTPUT);
pinMode(motorRightSpeedPin, OUTPUT);
}
void loop() {
// Read sensor values
int sensorLeftValue = analogRead(sensorLeftPin);
int sensorCenterValue = analogRead(sensorCenterPin);
int sensorRightValue = analogRead(sensorRightPin);
// Calculate error based on sensor readings
int error = sensorCenterValue - sensorLeftValue + sensorRightValue;
// Adjust motor speeds using PID controller
motorLeftSpeed = [Link](error);
motorRightSpeed = -motorLeftSpeed;
// Set motor speeds
analogWrite(motorLeftSpeedPin, motorLeftSpeed);
analogWrite(motorRightSpeedPin, motorRightSpeed);
// Print sensor values and motor speeds to serial monitor
[Link]("Left: ");
[Link](sensorLeftValue);
[Link](" | Center: ");
[Link](sensorCenterValue);
[Link](" | Right: ");
[Link](sensorRightValue);
[Link](" | Motor Left: ");
[Link](motorLeftSpeed);
[Link](" | Motor Right: ");
[Link](motorRightSpeed);
delay(20);
void setup() {
// put your setup code here, to run once:
void loop() {
// put your main code here, to run repeatedly: