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

APPENDIX

The document contains code for controlling a robot using an L298N motor driver and PID control for line following. It defines digital pins for motor control, sets up input buttons, and implements a PID algorithm to adjust motor speeds based on sensor readings. The main functions include reading sensor values, calculating PID output, and controlling motor direction and speed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

APPENDIX

The document contains code for controlling a robot using an L298N motor driver and PID control for line following. It defines digital pins for motor control, sets up input buttons, and implements a PID algorithm to adjust motor speeds based on sensor readings. The main functions include reading sensor values, calculating PID output, and controlling motor direction and speed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

APPENDIX

Code

//designation of digital pins to L298N void setup() {


#define rmf 3 //IN1 //L298N pins
#define rmb 4 //IN2 pinMode(lmf, OUTPUT);
#define rms 5 //EnA pinMode(lmb, OUTPUT);
#define lms 6 //EnB pinMode(rmf, OUTPUT);
#define lmf 8 //IN3 pinMode(rmb, OUTPUT);
#define lmb 7 //IN4

//Input pull down button


//Variables to following line pinMode(button1, INPUT);
int s[5], sum; pinMode(button2, INPUT);
int position[5] = { 1, 2, 3, 4, 5};
int threshold = 500, sensor;
int lbase = 100, rbase = 100; //led pin as output
int lmotor, rmotor; pinMode(LED_BUILTIN, OUTPUT);
float avg; Serial.begin(9600);
int kp = 50, kd = 1000, ki=0.1; }
int PID;
float error, last_error, I;
char turn; void loop(){
int base[5] = { 1, 2, 4, 8, 16}; //pull down button to start the bot
int tsp = 130; //turn speed while (digitalRead(button1) == 0) {}
PID_LINE_FOLLOW(); //line follow
using pid
}
//push buttons for pull down
int button1 = 11;
int button2 = 12;
void PID_reading() {
bool button1_state, button2_state;
sensor = 0;
sum = 0; }
for (byte i = 0; i < 5; i++) { if(s[0]&&s[1]&&!s[2]&&!s[3]&&!s[4]){
s[i] = analogRead(i); motor(100,-100);
(s[i] < threshold) ? s[i] = 1 : s[i] = 0; while(!s[1]&&!s[2]&&!s[3])
//converts A0-A4 into digital PID_reading();
sensor += s[i] * position[i]; }
sum += s[i]; else{
} PID_reading();
if(sum) avg = sensor / sum; //average error = 2.1775 - avg;
val
PID = error * kp + kd * (error -
} last_error) + ki* (I + error);
last_error = error;

void reading() {
sensor = 0; rmotor = rbase - PID;
sum = 0; lmotor = lbase + PID;
for (byte i = 0; i < 5; i++) { motor(lmotor, rmotor);
s[i] = analogRead(i); }
(s[i] < threshold) ? s[i] = 1 : s[i] = 0; }
//converts A0-A4 into digital
}
sensor += s[i] * base[i];
sum += s[i];
}
//Motor turns orientation
}
void motor(int a, int b) {
if (a > 0) {
digitalWrite(lmf, 1);
void PID_LINE_FOLLOW() {
digitalWrite(lmb, 0);
while(1) {
}
PID_reading();
else {
if(!s[0]&&!s[1]&&!s[2]&&s[3]&&s[4]){
a = -(a);
motor(-100,100);
digitalWrite(lmf, 0);
while(!s[1]&&!s[2]&&!s[3])
PID_reading(); digitalWrite(lmb, 1);
}
if (b > 0) {
digitalWrite(rmf, 1);
digitalWrite(rmb, 0);
}
else {
b = -(b);
digitalWrite(rmf, 0);
digitalWrite(rmb, 1);
}

if (a > 150) a = 150;


if (b > 150) b = 150;

analogWrite(lms, a);
analogWrite(rms, b);
}

You might also like