by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
// twelve servo objects can be created on most boardsint pos = 90; // variable to store the servo position
int sensorPin0 = A0; // input from first photosensor
int sensorPin1 = A1; // input from second photosensor
const int buttonPin = 2; // the number of the pushbutton pin
int valPin0 = 0; // variable to store the value coming from the sensor Pin0
int valPin1 = 0; // variable to store the value coming from the sensor Pin1
int offset = 100;
int buttonState = 1;
int hysteresis = 5;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
Serial.println(“start loop”);
buttonState = digitalRead(buttonPin);
while(buttonState == LOW)
{
valPin0 = analogRead(sensorPin0);
valPin1 = analogRead(sensorPin1);
if(valPin1 > valPin0)
{
offset = valPin1 – valPin0; // want to subtract offset from Pin1
}
else if(valPin1 < valPin0)
{
offset = valPin1 – valPin0; // want to add offset to Pin1
}
Serial.print(“Calibrate: “);
Serial.print(offset);
Serial.println(“buttonState”);
buttonState = digitalRead(buttonPin);
delay(1000);
}
if (analogRead(sensorPin0) < analogRead(sensorPin1)-offset-hysteresis)
{
Serial.println(analogRead(sensorPin0));
Serial.println(analogRead(sensorPin1));
pos +=1;
myservo.write(pos); // tell servo to go to position in variable ‘pos’
if(pos > 160)
{
pos = 160;
}
delay(50);
}
else if (analogRead(sensorPin0) > analogRead(sensorPin1)-offset+hysteresis)
{
pos-=1;;
myservo.write(pos); // tell servo to go to position in variable ‘-pos’
if(pos < 10)
{
pos = 10;
}
delay(50);
}
}