0% found this document useful (0 votes)
5 views3 pages

Google Code

The document outlines an Arduino program for controlling motors using PID control based on readings from infrared sensors. It defines pins for sensors and motors, initializes the PID controller with specific gains, and continuously reads sensor values to adjust motor speeds accordingly. The program also prints sensor readings and motor speeds to the serial monitor for debugging purposes.

Uploaded by

vaibhavnikunj35
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Google Code

The document outlines an Arduino program for controlling motors using PID control based on readings from infrared sensors. It defines pins for sensors and motors, initializes the PID controller with specific gains, and continuously reads sensor values to adjust motor speeds accordingly. The program also prints sensor readings and motor speeds to the serial monitor for debugging purposes.

Uploaded by

vaibhavnikunj35
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// 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:

You might also like