0% found this document useful (0 votes)
78 views2 pages

Line Follower Robot Arduino

This document defines code for controlling two DC motors connected to an Arduino board to follow a line using 5 infrared sensors. It includes definitions for proportional and derivative constants for the motor control, motor speed limits, and functions for reading the sensor values, calculating motor speeds based on PID control, and setting the motor speeds. The code initializes the motors and sensors, performs a manual calibration of the sensors, and enters a loop to continuously read the sensor values, calculate errors from a target position, determine motor speeds based on PID control, and set the motor speeds.

Uploaded by

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

Line Follower Robot Arduino

This document defines code for controlling two DC motors connected to an Arduino board to follow a line using 5 infrared sensors. It includes definitions for proportional and derivative constants for the motor control, motor speed limits, and functions for reading the sensor values, calculating motor speeds based on PID control, and setting the motor speeds. The code initializes the motors and sensors, performs a manual calibration of the sensors, and enters a loop to continuously read the sensor values, calculate errors from a target position, determine motor speeds based on PID control, and set the motor speeds.

Uploaded by

awais8991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <AFMotor.

h>
#include <QTRSensors.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ );
AF_DCMotor motor2(2, MOTOR12_8KHZ );
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define

KP .2
KD 5
M1_minumum_hiz 120
M2_minumum_hiz 120
M1_maksimum_hiz 210
M2_maksimum_hiz 210
MIDDLE_SENSOR 3
NUM_SENSORS 5
TIMEOUT 2500
EMITTER_PIN 2
DEBUG 0

QTRSensorsRC qtrrc((unsigned char[]) { 19,18,17,16,15} ,NUM_SENSORS, TIMEOUT, EM


ITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];
void setup()
{
delay(1500);
manual_calibration();
set_motors(0,0);
}
int lastError = 0;
int last_proportional = 0;
int integral = 0;
void loop()
{
unsigned int sensors[5];
int position = qtrrc.readLine(sensors);
int error = position 2000;
int motorSpeed = KP * error + KD * (error lastError);
lastError = error;
int leftMotorSpeed = M1_minumum_hiz + motorSpeed;
int rightMotorSpeed = M2_minumum_hiz motorSpeed;
// set motor speeds using the two motor speed variables above
set_motors(leftMotorSpeed, rightMotorSpeed);
}
void set_motors(int motor1speed, int motor2speed)
{
if (motor1speed > M1_maksimum_hiz ) motor1speed = M1_maksimum_hiz; //MAKSMUM MOTO
R 1 HIZ LMT
if (motor2speed > M2_maksimum_hiz ) motor2speed = M2_maksimum_hiz; // MAKSMUM MOT
OR 2 HIZ LMT
if (motor1speed < 0) motor1speed = 0; // MNIMUMMOTOER 1 HIZ LMT
if (motor2speed < 0) motor2speed = 0; // MNMUM MOTOR 2 HIZ LMT
motor1.setSpeed(motor1speed); //1.MOTOR HIZI
motor2.setSpeed(motor2speed);// 2.MOTOR HIZI
motor1.run(FORWARD); //LER

motor2.run(FORWARD); //LER
}
void manual_calibration() {
int i;
for (i = 0; i < 250; i++)
{
qtrrc.calibrate(QTR_EMITTERS_ON);
delay(20);
}
if (DEBUG) {
Serial.begin(9600);
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMinimumOn[i]);
Serial.print( );
}
Serial.println();
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMaximumOn[i]);
Serial.print( );
}
Serial.println();
Serial.println();
}
}
/////// Arduino Uno QTR-8RC Infrared Sensor L293D motor driver card robot Kits

You might also like