black line following robot by arduino

Thread Starter


Joined Mar 15, 2018
iam having this university project creating a code in Arduino and using 6 sensores attached to it,
for a robot that follows a black line course , and at some point i have a "white line" and thats where the problem is.
my code workes for running the course but not the "white line" and with some adjusments goes through the "white line" but its not sensitive enough to complete the course . ille post my code here and maybe some one could suggest an idea.
thanks for all the helpers!

* PID-based algorithm. It works decently on courses with smooth, 6"
* radius curves and has been tested with Zumos using 30:1 HP and

#include <QTRSensors.h>
#include <ZumoReflectanceSensorArray.h>
#include <ZumoMotors.h>
#include <ZumoBuzzer.h>
#include <Pushbutton.h>

ZumoReflectanceSensorArray reflectanceSensors;
ZumoMotors motors;
Pushbutton button(ZUMO_BUTTON);
int lastError = 0;

// This is the maximum speed the motors will be allowed to turn.
// (400 lets the motors go at top speed; decrease to impose a speed limit)
const int MAX_SPEED = 250;

void setup()

// Initialize the reflectance sensors module

// Wait for the user button to be pressed and released

// Turn on LED to indicate we are in calibration mode
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

// Wait 1 second and then begin automatic sensor calibration
// by rotating in place to sweep the sensors over the line
int i;
for(i = 0; i < 80; i++)
if ((i > 10 && i <= 30) || (i > 50 && i <= 70))
motors.setSpeeds(-200, 200);
motors.setSpeeds(200, -200);

// Since our counter runs to 80, the total delay will be
// 80*20 = 1600 ms.

// Turn off LED to indicate we are through with calibration
digitalWrite(13, LOW);

// Wait for the user button to be pressed and released


void loop()
unsigned int sensors[6];

// Get the position of the line. Note that we *must* provide the "sensors"
// argument to readLine() here, even though we are not interested in the
// individual sensor readings
int position = reflectanceSensors.readLine(sensors);

// Our "error" is how far we are away from the center of the line, which
// corresponds to position 2500.
int error = position - 2500;
motors.setSpeeds(MAX_SPEED, MAX_SPEED);
// Get motor speed difference using proportional and derivative PID terms
// (the integral term is generally not very useful for line following).
// Here we are using a proportional constant of 1/4 and a derivative
// constant of 6, which should work decently for many Zumo motor choices.
// You probably want to use trial and error to tune these constants for
// your particular Zumo and line course.

int speedDifference = error / 4 + 6 * (error - lastError);

lastError = error;

// Get individual motor speeds. The sign of speedDifference
// determines if the robot turns left or right.
int m1Speed = MAX_SPEED + speedDifference;
int m2Speed = MAX_SPEED - speedDifference;

// Here we constrain our motor speeds to be between 0 and MAX_SPEED.
// Generally speaking, one motor will always be turning at MAX_SPEED
// and the other will be at MAX_SPEED-|speedDifference| if that is positive,
// else it will be stationary. For some applications, you might want to
// allow the motor speed to go negative so that it can spin in reverse.
if (m1Speed < 0)
m1Speed = -MAX_SPEED-20;
if (m2Speed < 0)
m2Speed = -MAX_SPEED-20;
if (m1Speed > MAX_SPEED)
m1Speed = MAX_SPEED;
if (m2Speed > MAX_SPEED)
m2Speed = MAX_SPEED;

motors.setSpeeds(m1Speed, m2Speed);