line follower black line greensurface

hey everybody

we are in a line following project and we have the code below but i have the issue that the bot cant find the line as it is on a green surface… can anybody help me how to chance code so it sees the line?

#include <Wire.h>
#include <Zumo32U4.h>

// This is the maximum speed the motors will be allowed to turn.
// A maxSpeed of 400 lets the motors go at top speed. Decrease
// this value to impose a speed limit.
const uint16_t maxSpeed = 200;
Zumo32U4Buzzer buzzer;
Zumo32U4LineSensors lineSensors;
Zumo32U4Motors motors;
Zumo32U4ButtonA buttonA;
Zumo32U4LCD lcd;

int16_t lastError = 0;

#define NUM_SENSORS 5
unsigned int lineSensorValues[NUM_SENSORS];

// Sets up special characters in the LCD so that we can display
// bar graphs.
void loadCustomCharacters()
{
static const char levels PROGMEM = {
0, 0, 0, 0, 0, 0, 0, 63, 63, 63, 63, 63, 63, 63
};
lcd.loadCustomCharacter(levels + 0, 0); // 1 bar
lcd.loadCustomCharacter(levels + 1, 1); // 2 bars
lcd.loadCustomCharacter(levels + 2, 2); // 3 bars
lcd.loadCustomCharacter(levels + 3, 3); // 4 bars
lcd.loadCustomCharacter(levels + 4, 4); // 5 bars
lcd.loadCustomCharacter(levels + 5, 5); // 6 bars
lcd.loadCustomCharacter(levels + 6, 6); // 7 bars
}

void printBar(uint8_t height)
{
if (height > 8) { height = 8; }
const char barChars = {’ ', 0, 1, 2, 3, 4, 5, 6, 255};
lcd.print(barChars[height]);
}

void calibrateSensors()
{
lcd.clear();

// Wait 1 second and then begin automatic sensor calibration
// by rotating in place to sweep the sensors over the line
delay(1000);
for(uint16_t i = 0; i < 120; i++)
{
if (i > 30 && i <= 90)
{
motors.setSpeeds(-200, 200);
}
else
{
motors.setSpeeds(200, -200);
}

lineSensors.calibrate();
}
motors.setSpeeds(0, 0);
}

// Displays a bar graph of sensor readings on the LCD.
// Returns after the user presses A.
void showReadings()
{
lcd.clear();

while(!buttonA.getSingleDebouncedPress())
{
lineSensors.readCalibrated(lineSensorValues);

lcd.gotoXY(0, 0);
for (uint8_t i = 0; i < NUM_SENSORS; i++)
{
uint8_t barHeight = map(lineSensorValues*, 0, 1000, 0, 8);*

  • printBar(barHeight);*
  • }*
  • }*
    }
    void setup()
    {
  • // Uncomment if necessary to correct motor directions:*
  • //motors.flipLeftMotor(true);*
  • //motors.flipRightMotor(true);*
  • lineSensors.initFiveSensors();*
  • loadCustomCharacters();*
  • // Play a little welcome song*
  • buzzer.play(">g32>>c32");*
  • // Wait for button A to be pressed and released.*
  • lcd.clear();*
  • lcd.print(F(“Press A”));*
  • lcd.gotoXY(0, 1);*
  • lcd.print(F(“to calb”));*
  • buttonA.waitForButton();*
  • calibrateSensors();*
  • showReadings();*
  • // Play music and wait for it to finish before we start driving.*
  • lcd.clear();*
  • lcd.print(F(“Go!”));*
  • buzzer.play(“L16 cdegreg4”);*
  • while(buzzer.isPlaying());*
    }
    void loop()
    {
    _ // Get the position of the line. Note that we must provide_
  • // the “lineSensorValues” argument to readLine() here, even*
  • // though we are not interested in the individual sensor*
  • // readings.*
  • int16_t position = lineSensors.readLine(lineSensorValues);*
  • // Our “error” is how far we are away from the center of the*
  • // line, which corresponds to position 2000.*
  • int16_t error = position - 2000;*
  • // 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.*
    int16_t speedDifference = error / 1 + 2 * (error - lastError);
  • lastError = error;*
  • // Get individual motor speeds. The sign of speedDifference*
  • // determines if the robot turns left or right.*
  • int16_t leftSpeed = (int16_t)maxSpeed + speedDifference;*
  • int16_t rightSpeed = (int16_t)maxSpeed - speedDifference;*
  • // Constrain our motor speeds to be between 0 and maxSpeed.*
  • // One motor will always be turning at maxSpeed, and the other*
  • // will be at maxSpeed-|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.*
  • leftSpeed = constrain(leftSpeed, 0, (int16_t)maxSpeed);*
  • rightSpeed = constrain(rightSpeed, 0, (int16_t)maxSpeed);*
  • motors.setSpeeds(leftSpeed, rightSpeed);*

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

  • Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
  • Can you please tell us your electronics, programming, Arduino, hardware experience?
  • Did you write the code?
  • What sensors are you using?
  • Have you tested the sensors on their own?

Is this the same as this ?

Tom... :slight_smile:

From the documentation for the Zumo32U4 and the corresponding library, I get the impression that it is designed to work with a black line on a white background or the inverse.

Since the reflectance sensors used are dependent on IR emitting LEDs I should imagine that a green surface, which is green because it reflects green light and absorbs red, blue, etc light, will likely absorb most of the IR and appear to the sensor as almost black.

Hi,
The Op needs to write some code that just reads the sensors and checks their response to the green/black changes.

Then start to write their own code for the robot.

Tom.... :slight_smile:

TomGeorge:
Hi,
The Op needs to write some code that just reads the sensors and checks their response to the green/black changes.

Then start to write their own code for the robot.

Tom.... :slight_smile:

His code is just an example from the library.

There is also a LineSensorTest.ino which, as you say, may be useful for testing.