Line Following Code that works

Hello all,

I was a bit dismayed at the line following example…because it didn’t work.

Feel free to update this example with this.

I added in the line reflectance example so that you can debug it for your particular line (you may have to adjust the threshold). Mine works awesome with the pdf’s that are available here: http://robotsquare.com/2012/11/28/line-following/

Code follows…but as the sparki is currently doing laps I’m going to say that it works fine.

#include <Sparki.h> // include the sparki library

void setup() 
{
}

void loop() {
  //Threshold is the value that helps you determine what's black and white.  
  //Sparki's library indicates white as close to 900, black is around 200.
  //I'm using a threshold of 400 for the example, but if you have a narrow line, or perhaps a lighter black, you may 
  //Need to adjust.
  int threshold = 400;
  
  int edgeLeft   = sparki.edgeLeft();   // measure the left edge IR sensor
  int lineLeft   = sparki.lineLeft();   // measure the left IR sensor
  int lineCenter = sparki.lineCenter(); // measure the center IR sensor
  int lineRight  = sparki.lineRight();  // measure the right IR sensor
  int edgeRight  = sparki.edgeRight();  // measure the right edge IR sensor

  if ( lineLeft < threshold ) // if line is below left line sensor
  {  
    sparki.moveLeft(); // turn right
  }

  if ( lineRight < threshold ) // if line is below right line sensor
  {  
    sparki.moveRight(); // turn left
  }

  // if the center line sensor is the only one reading a line
  if ( (lineCenter < threshold) && (lineLeft > threshold) && (lineRight > threshold) )
  {
    sparki.moveForward(); // move forward
  }  
  sparki.clearLCD(); // wipe the screen
  


  // write the measurements to the screen
  sparki.print("Edge Left:   ");
  sparki.println(edgeLeft);
  
  sparki.println();

  sparki.print("Line Left:   ");
  sparki.println(lineLeft);
  
  sparki.print("Line Center: ");
  sparki.println(lineCenter);
  
  sparki.print("Line Right:  ");
  sparki.println(lineRight);
  
  sparki.println();
  
  sparki.print("Edge Right:  ");
  sparki.println(edgeRight);
  
  sparki.updateLCD(); // display all of the information written to the screen
  delay(100); // wait 0.1 seconds
}

Excellent. Thanks.

We included this code in the latest update. Thanks!

I can get this to work. I have uploaded the code and Sparki the wonder robot, goes around in circles, I guess looking for the line… but to no avail.

what surface would this work on best?? I have tried butchers paper and a white plastic tray. Both with permanent marker…

Help!

Pookie

Print the sheets.

[quote=“pookie”]
what surface would this work on best??[/quote]
This is a link to an earlier forum post of mine to print a simple line-following sheet that works: Updated line following

Good luck!