Arduino Forum

Using Arduino => Programming Questions => Topic started by: tx3ha on Jul 31, 2019, 04:04 am

Title: Line Follower Not Working
Post by: tx3ha on Jul 31, 2019, 04:04 am
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
Code: [Select]

#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);

}
Title: Re: Line Follower Not Working
Post by: PaulS on Jul 31, 2019, 01:01 pm
Code: [Select]
  int i;
  for (int i=0;i<p;i++)
  {if (i>=p/4 && i<3*p/4)

When the for loop is running, there are two variables named i in scope. One hides the other. Why? There is no excuse for that.

Code: [Select]
    {analogWrite(E1, x); // Run in half speed
    analogWrite(E2, x); // Run in full speed

x is a piss-poor name for the speed.

Code: [Select]
    {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);}

Stand on the throttle, then put the car in gear. I hope you don't drive your real car that way.

Quote
I am not able to troubleshoot the code.
Seriously? You are unable to put Serial.print() statements in the code, to find out values of the variables being used to determine the speed?

Code: [Select]
    analogWrite(E2,190 );//rightMotorSpeed
    analogWrite(E1,190 );//leftMotorSpeed

What was the point in locating the line and calculating data to keep the line in sight, when you then ignore the line and just mash the throttle?

EVERY { goes on a line BY ITSELF.
EVERY } goes on a line BY ITSELF.
Programmers that know what they are doing keep the code properly indented. Those that can't do that, but want to appear as though they know what they are doing use Tools + Auto Format. Those that don't even want to appear to know what they are doing post piss-poorly indented code.

Think about which category you want to be in.
Title: Re: Line Follower Not Working
Post by: Delta_G on Sep 18, 2019, 08:13 pm
https://forum.arduino.cc/index.php?topic=636664.0 (http://Cross posted here: [url=https://forum.arduino.cc/index.php?topic=636664.0)]Cross posted here: https://forum.arduino.cc/index.php?topic=636664.0 (https://forum.arduino.cc/index.php?topic=636664.0)[/url]
Title: Re: Line Follower Not Working
Post by: ballscrewbob on Sep 18, 2019, 08:21 pm
tx3ha

DO NOT CROSS POST especially 3 times  GRR.

Please read this before you make any more comments. (https://forum.arduino.cc/index.php?topic=148850.0)

Further transgressions may lead to your loss of forum privileges.

Bob.