Society of Robots - Robot Forum

Software => Software => Topic started by: Xyver on March 14, 2010, 02:38:18 PM

Title: Light threshold?
Post by: Xyver on March 14, 2010, 02:38:18 PM
My robot is so close to working, its almost ok but it does not travel in a straight line.  It kinda does, but only but going left and right really fast, it goes kind of forewards.  Its almost like its walking on wheels, shifting weight from one to the other.  So, in researching photovores and code for such, its says that the sensors must read ABOUT THE SAME light for it to go straight.  My code says, if it reads EXACTLY THE SAME light then it will go straight.  But light is never exactly the same, which is why it curves back and forth.  How can I make it almost the same instead of exactly the same?
Code: [Select]
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <>

#include <Servo.h>

Servo leftservo;  // create servo object to control a servo
Servo rightservo;

int potpin0 = 0;  // analog pin used to connect the potentiometer
int left;    // variable to read the value from the analog pin
int potpin1 = 1;
int right;
void setup()

void loop()
  left = analogRead(potpin0); 
  right = analogRead(potpin1);
  if(left < right)
  { leftservo.write(75);
  else if (left > right)
  { leftservo.write(90);
Title: Re: Light threshold?
Post by: Razor Concepts on March 14, 2010, 02:58:56 PM
if(left + 10 < right)

if(left > right + 10)

10 can be whatever the threshold is (since its 0-1023 maybe try a threshold of around 100 )
Title: Re: Light threshold?
Post by: Xyver on March 14, 2010, 03:29:40 PM
PERFECT!  It now follows a flashlight very nicely.  My first robot is done!! Thanks guys.  How much harder is it to make a line follower?
Title: Re: Light threshold?
Post by: little-c on March 14, 2010, 04:21:35 PM
not much. read up on it first. make sure you have the sensors, and its straightfoward.

there is a few tutorials in the members tutorials section