Skip to content

Instantly share code, notes, and snippets.

@clarksonrichard
Created June 13, 2013 07:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarksonrichard/5771785 to your computer and use it in GitHub Desktop.
Save clarksonrichard/5771785 to your computer and use it in GitHub Desktop.
code for mr. Indecision
/*
Simple Pan Tracking with 2 IR Sensors
Requires Arduino IDE version 0017
or later (0019 or later preferred)
*/
#include
Servo servo; // Define the servo
const int lineLSense = A4; //Define the Left IR Sensor
const int lineRSense = A2; //Define the Right IR Sensor
int degree = 90; //Starting point of the Servo
int irReflectR = 0;
int irReflectL = 0;
int thresh = 200; //You can adjust this threshold to manipulate the range of your IR sensor readings.
void setup() {
servo.attach(9); // servo pin D9
Serial.begin(9200);
}
void loop() {
// Read reflective sensors
int trav = 2;
irReflectL = analogRead(lineLSense);
irReflectR = analogRead(lineRSense);
if (degree >= 180){
degree = 179;
}
if (degree <= 0){
degree = 1;
}
if (irReflectL >= thresh && irReflectR >= thresh) {
Serial.println("on line");
}
if (irReflectL >= thresh && irReflectR <= thresh) {
servo.write(degree + trav); // veering off right
degree=(degree + trav);
delay(4);
Serial.println("veering off right");
}
if (irReflectL <= thresh && irReflectR >= thresh) {
servo.write(degree - trav); // veering off left
degree=(degree - trav);
delay(4);
Serial.println("veering off left");
}
// If line is lost try to reacquire
if (irReflectL < thresh && irReflectR < thresh) {
Serial.println("lost");
delay(20);
}
}
// Motion routines for line following
void line_forward() {
servo.write(0);
}
void line_slipRight() {
servo.write(degree + 5);
}
void line_slipLeft() {
servo.write(0);
}
void line_spinRight() {
servo.write(180);
}
void line_spinLeft() {
servo.write(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment