PID+Line Follower Code
PID+Line Follower Code
h>
#include <QTRSensors.h>
#include "BluetoothSerial.h"
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#define BIN1 25
#define AIN2 22
#define BIN2 33
#define PWMA 23
#define PWMB 32
#define STBY 19
// motors as you have memory for. If you are using functions like forward
// that take 2 motors as arguements you can either write new functions or
QTRSensors qtr;
BluetoothSerial SerialBT;
const uint8_t SensorCount = 5;
uint16_t sensorValues[SensorCount];
int threshold[SensorCount];
float Kp = 0;
float Ki = 0;
float Kd = 0;
uint8_t multiP = 1;
uint8_t multiI = 1;
uint8_t multiD = 1;
uint8_t Kpfinal;
uint8_t Kifinal;
uint8_t Kdfinal;
float Pvalue;
float Ivalue;
float Dvalue;
uint16_t position;
void setup()
qtr.setTypeAnalog();
qtr.setSensorPins((const uint8_t[]){26, 27, 14, 12, 13}, SensorCount);
delay(500);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
SerialBT.begin();
qtr.calibrate();
digitalWrite(LED_BUILTIN, LOW); // turn off Arduino's LED to indicate we are through with
calibration
Serial.print(threshold[i]);
Serial.print(" ");
Serial.println();
delay(1000);
}
void loop()
if (SerialBT.available()){
while(SerialBT.available() == 0);
valuesread();
processing();
if (onoff == true){
robot_control();
motor1.stop();
motor2.stop();
void robot_control(){
// read calibrated sensor values and obtain a measure of the line position
position = qtr.readLineBlack(sensorValues);
motor_drive(-230,230);
else{
position = qtr.readLineBlack(sensorValues);
}
PID_Linefollow(error);
//PID_Linefollow(error);
P = error;
I = I + error;
D = error - previousError;
Pvalue = (Kp/pow(10,multiP))*P;
Ivalue = (Ki/pow(10,multiI))*I;
Dvalue = (Kd/pow(10,multiD))*D;
previousError = error;
lsp = 255;
lsp = -255;
rsp = 255;
rsp = -255;
}
motor_drive(lsp,rsp);
//The Arduino knows that for each instruction it will receive 2 bytes.
void valuesread() {
val = SerialBT.read();
cnt++;
v[cnt] = val;
if (cnt == 2)
cnt = 0;
void processing() {
int a = v[1];
if (a == 1) {
Kp = v[2];
if (a == 2) {
multiP = v[2];
if (a == 3) {
Ki = v[2];
if (a == 4) {
multiI = v[2];
if (a == 5) {
Kd = v[2];
}
if (a == 6) {
multiD = v[2];
if (a == 7) {
onoff = v[2];
if(right>0)
motor2.setSpeed(right);
motor2.forward();
else
motor2.setSpeed(right);
motor2.backward();
if(left>0)
motor1.setSpeed(left);
motor1.forward();
else
motor1.setSpeed(left);
motor1.backward();
}
}