Hey,
I am making a line follower for a competition and i am using following components
micro gear motor(3000rpm)
arduino nano
pololu qtr 18rc
l298
The robot is doing callibration(Motor Working) but its not working after it(motor is not rotating).I have tried almost everything but no luck.I am not able to troubleshoot the code.
Thanks in advance
#include <QTRSensors.h>
#define E1 10 // Enable Pin for motor 1
#define E2 11 // Enable Pin for motor 2
#define I1 A1 // Control pin 1 for motor 1
#define I2 A2 // Control pin 2 for motor 1
#define I3 A3 // Control pin 1 for motor 2
#define I4 A4 // Control pin 2 for motor 2
#define Kp 55
#define Kd 0
#define rightMaxSpeed 250
#define leftMaxSpeed 250
#define rightBaseSpeed 175
#define leftBaseSpeed 175
#define NUM_SENSORS 8
#define TIMEOUT 2500
#define p 12
#define x 140
QTRSensorsRC qtrrc((unsigned char[]) {2, 3, 4, 5, 6, 7, 8, 9} ,NUM_SENSORS, TIMEOUT);
unsigned int sensorValues[NUM_SENSORS];
void setup()
{
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
pinMode(I3, OUTPUT);
pinMode(I4, OUTPUT);
delay(4000);
int i;
for (int i=0;i<p;i++)
{if (i>=p/4 && i<3*p/4)
{analogWrite(E1, x); // Run in half speed
analogWrite(E2, x); // Run in full speed
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);}
else
{analogWrite(E1, x); // Run in half speed
analogWrite(E2, x); // Run in full speed
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);}
qtrrc.calibrate();
delay(60);
digitalWrite(I1, LOW);
digitalWrite(I2, LOW);
digitalWrite(I3, LOW);
digitalWrite(I4, LOW);
delay(300);}
analogWrite(E1, 255); // Run in half speed
analogWrite(E2, 255); // Run in full speed
digitalWrite(I1, LOW);
digitalWrite(I2, LOW);
digitalWrite(I3, LOW);
digitalWrite(I4, LOW);
delay(2000);
Serial.begin(9600);
}
int lastError = 0;
void loop()
{
unsigned int sensors[8];
int position = qtrrc.readLine(sensors);
int error = position - 2500;
int motorSpeed = Kp*error+Kd*(error-lastError);
lastError = error;
int rightMotorSpeed = rightBaseSpeed+motorSpeed;
int leftMotorSpeed = leftBaseSpeed-motorSpeed;
if(rightMotorSpeed>rightMaxSpeed) rightMotorSpeed = rightMaxSpeed;
if(leftMotorSpeed>leftMaxSpeed) leftMotorSpeed = leftMaxSpeed;
if(rightMotorSpeed < 0) rightMotorSpeed = 0;
if(leftMotorSpeed < 0) leftMotorSpeed = 0;
analogWrite(E2,190 );//rightMotorSpeed
analogWrite(E1,190 );//leftMotorSpeed
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}