-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPID_Controlled_Line_Follower_Code.ino
149 lines (125 loc) · 4.05 KB
/
PID_Controlled_Line_Follower_Code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Motor control pins for the left side
#define LEFT_MOTOR1 2
#define LEFT_MOTOR2 3
// Motor control pins for the right side
#define RIGHT_MOTOR1 4
#define RIGHT_MOTOR2 5
#define ENA 10
#define ENB 9
// IR Sensor Pins
#define LEFT_SENSOR A3
#define MIDDLE_SENSOR A4
#define RIGHT_SENSOR A5
float Kp = 4.35;
float Ki = 0.0;
float Kd = 0.0;
float PIDvalue = 0;
float P = 0, I = 0, D = 0;
float previousError = 0;
double leftProportional, rightProportional;
int error=0;
double setPoint = 1023;
void setup() {
// Configure motor control pins as outputs
pinMode(LEFT_MOTOR1, OUTPUT);
pinMode(LEFT_MOTOR2, OUTPUT);
pinMode(RIGHT_MOTOR1, OUTPUT);
pinMode(RIGHT_MOTOR2, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(LEFT_SENSOR, INPUT);
pinMode(MIDDLE_SENSOR, INPUT);
pinMode(RIGHT_SENSOR, INPUT);
// Initialize serial communication
Serial.begin(9600);
}
void loop() {
int leftSensorValue = analogRead(LEFT_SENSOR);
int middleSensorValue = analogRead(MIDDLE_SENSOR);
int rightSensorValue = analogRead(RIGHT_SENSOR);
leftProportional = Kp * (setPoint - leftSensorValue);
rightProportional = Kp * (setPoint - rightSensorValue);
// Display sensor values on the serial monitor
Serial.print("Left Sensor: ");
Serial.print(leftSensorValue);
Serial.print(" | Middle Sensor: ");
Serial.print(middleSensorValue);
Serial.print(" | Right Sensor: ");
Serial.println(rightSensorValue);
// Implement line-following logic based on sensor readings
if ((leftSensorValue < 165 && middleSensorValue >= 165 && rightSensorValue < 165) || (leftSensorValue >= 165 && middleSensorValue >= 165 && rightSensorValue >= 165)) {
error=0;
calculatePID();
motorPIDcontrol();
moveForward(leftProportional, rightProportional);
} else if ((leftSensorValue >= 165 && middleSensorValue < 165 && rightSensorValue < 165) || (leftSensorValue >= 165 && middleSensorValue >= 165 && rightSensorValue < 165)) {
error=-1;
calculatePID();
turnRight();
} else if ((leftSensorValue < 165 && middleSensorValue < 165 && rightSensorValue >= 165) || (leftSensorValue < 165 && middleSensorValue >= 165 && rightSensorValue >= 165)) {
error=-1;
calculatePID();
turnLeft();
} else {
stopMotors();
}
}
// Rest of your motor control functions remain the same
// Function to move the car forward
void moveForward(double leftSpeed, double rightSpeed) {
analogWrite(ENA, 60); // Set the enable (speed) to 55
analogWrite(ENB, 55); // Set the enable (speed) to 55
analogWrite(LEFT_MOTOR1, leftSpeed);
digitalWrite(LEFT_MOTOR2, LOW);
analogWrite(RIGHT_MOTOR1, rightSpeed);
digitalWrite(RIGHT_MOTOR2, LOW);
}
// Function to move the car backward
void moveBackward() {
analogWrite(ENA, 60); // Set the enable (speed) to 55
analogWrite(ENB, 55); // Set the enable (speed) to 55
digitalWrite(LEFT_MOTOR1, LOW);
digitalWrite(LEFT_MOTOR2, HIGH);
digitalWrite(RIGHT_MOTOR1, LOW);
digitalWrite(RIGHT_MOTOR2, HIGH);
}
// Function to turn the car left
void turnLeft() {
analogWrite(ENA, 0);
analogWrite(ENB, 85); // Set the enable (speed) to 55
digitalWrite(LEFT_MOTOR1, LOW);
digitalWrite(LEFT_MOTOR2, LOW);
digitalWrite(RIGHT_MOTOR1, HIGH);
digitalWrite(RIGHT_MOTOR2, LOW);
}
// Function to turn the car right
void turnRight() {
analogWrite(ENA, 85); // Set the enable (speed) to 55
analogWrite(ENB, 0);
digitalWrite(LEFT_MOTOR1, HIGH);
digitalWrite(LEFT_MOTOR2, LOW);
digitalWrite(RIGHT_MOTOR1, LOW);
digitalWrite(RIGHT_MOTOR2, LOW);
}
// Function to stop the motors
void stopMotors() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(LEFT_MOTOR1, LOW);
digitalWrite(LEFT_MOTOR2, LOW);
digitalWrite(RIGHT_MOTOR1, LOW);
digitalWrite(RIGHT_MOTOR2, LOW);
}
void motorPIDcontrol() {
int leftMotorSpeed = leftMotorSpeed + PIDvalue;
int rightMotorSpeed = rightMotorSpeed - PIDvalue;
constrain(leftMotorSpeed, 55, 90);
constrain(rightMotorSpeed, 55, 90);
}
void calculatePID() {
P = error;
I = I + error;
D = error - previousError;
PIDvalue = (Kp * P) + (Ki * I) + (Kd * D);
previousError = error;
}