Society of Robots - Robot Forum
Software => Software => Topic started by: Elijah5647 on January 12, 2011, 02:20:06 PM
-
Hey this code is for my differential drive robot with photoresistor sensors. It seems to work ok but does anyboby have any tips to make it better??
#include <Servo.h>
Servo servo_left; // create servo object to control a servo
// a maximum of eight servo objects can be created
Servo servo_right; // create servo object to control a servo
int ledPin = 13; // LED connected to digital pin 13
void setup() // run once, when the sketch starts
{
servo_left.attach(9); // attaches the servo on pin 9 to the servo object
servo_right.attach(10); // attaches the servo on pin 10 to the servo object
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
int sensor_left=0;//left photoresistor
int sensor_right=1;//right photoresistor
int threshold=8;//the larger this number, the more likely your robot will drive straight
void loop()
{
digitalWrite(ledPin, HIGH); // sets the LED on
//store sensor data
sensor_left = analogRead(0); // reads the value of the left photoresistor (value between 0 and 1023)
sensor_left = map(sensor_left, 0, 1023, 0, 255); // scale it to use it with the 8bits (value between 0 and 255)
sensor_right = analogRead(1); // reads the value of the right photoresistor (value between 0 and 1023)
sensor_right = map(sensor_right, 0, 1023, 0, 255); // scale it to use it with the 8bits (value between 0 and 255)
//detects more light on left side of robot
if(sensor_left > sensor_right && (sensor_left - sensor_right) > threshold)
{//go left
servo_left.write(0);
servo_right.write(150);
}
//detects more light on right side of robot
else if(sensor_right > sensor_left && (sensor_right - sensor_left) > threshold)
{//go right
servo_left.write(150);
servo_right.write(0);
}
//light is about equal on both sides
else
{//go straight
servo_left.write(150);
servo_right.write(150);
}
delay(25); // waits 25ms for a small delay to prevent crazy oscillations
digitalWrite(ledPin, LOW); // sets the LED off
}
Thanks
-
I don't have the code with me right now, but I can give you some ideas:
1) Have the robot calibrate itself based on the ambient light in the room when the robot first turns on and base its light detection off that. This way it'll work in a bright room or a dark room and not have problems with a static difference threshold.
2) Try to implement different kinds of movement functions. For example, have the robot start out with a wide turn as it first detects a shadow and make the turn sharper the longer it stays in the shadow. I just thought of this now, actually, and I'll probably try it when I get home ;D In theory, I think it should work around the problem of hitting a shadow edge dead-on (where the same amount of darkness is detected by both sensors, so the robot continues forward), and maybe smooth out the motion a bit.
3) Get the IR rangefinder sensor. It makes the robot a-whole-nother beast and is really interesting to play around with. I implemented mine without the servos (stationary on the front of the chassis). I used this so that my robot can avoid obstacles as it's searching for light. If you program the robot to move forward in a subtle 'S' pattern, you can create a field of detection that is wide enough to keep the sides of your robot from hitting anything (accounting for the narrow width of the IR beam). Works pretty well on mine!