
LightFollowerArduino
By: a guest on
Feb 16th, 2011 | syntax:
C++ | size: 2.25 KB | hits: 368 | expires: Never
#include <Servo.h>
Servo myservo;
Servo myservo2;
int pos = 0; // Variable to store the servo position.
int pos2 = 0; // Variable to store the servo position.
int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
int inputPhotoRight = 0;
int inputPhotoUP = 3; // Easier to read, instead of just 1 or 0.
int inputPhotoDOWN = 2;
int Left = 0; // Store readings from the photoresistors.
int Right = 0; // Store readings from the photoresistors.
int UP = 0; // Store readings from the photoresistors.
int DOWN = 0; // Store readings from the photoresistors.
void setup()
{
myservo.attach(9); // Attach servo to pin 9.
myservo2.attach(10); // Attach servo to pin 10.
Serial.begin(9600); // Begin serial communcation
}
void loop()
{
Serial.println(analogRead(inputPhotoRight));
// Reads the values from the photoresistors to the Left and Right variables.
Left = analogRead(inputPhotoLeft);
Right = analogRead(inputPhotoRight);
// Checks if right is greater than left, if so move to right.
if (Left > (Right +10))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (pos < 139)
pos++;
myservo.write(pos);
}
// Checks if left is greater than right, if so move to left.
if (Right > (Left +10))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (pos > 61)
pos -= 1;
myservo.write(pos);
}
//#################
// Reads the values from the photoresistors to the Left and Right variables.
UP = analogRead(inputPhotoUP);
DOWN = analogRead(inputPhotoDOWN);
// Checks if right is greater than left, if so move to right.
if (UP > (DOWN +10))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (pos2 < 109)
pos2++;
myservo2.write(pos2);
}
// Checks if left is greater than right, if so move to left.
if (DOWN > (UP +10))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (pos2 > 51)
pos2 -= 1;
myservo2.write(pos2);
}
// Added some delay, increase or decrease if you want less or more speed.
delay(10);
}