#include <Servo.h>
Servo myservoL;
Servo myservoR;
int myservo = 2;
int myAngle;
int pulseWidth;
void setup()
{
pinMode(myservo, OUTPUT);
myservoL.attach(9);
myservoR.attach(10);
}
void servoPulse(int myservo, int myAngle)
{
pulseWidth = (myAngle *10) + 600;
digitalWrite(myservo, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(myservo, LOW);
}
void loop()
{
for (myAngle = 15; myAngle <= 165; myAngle++)
{
servoPulse(myservo, myAngle);
delay(20);
}
for (myAngle = 165; myAngle >= 15; myAngle--)
{
servoPulse(myservo, myAngle);
delay(20);
}
int distance = analogRead(0);
if (distance <= 451) // about 2 in.
{
myservoL.write(90); // stop
myservoR.write(87); // move back to turn right
delay(1000);
}
else
{
myservoL.write(0); // move forward
myservoR.write(180); // move forward
}
}
All this does is make my servo that attach to the sensor scan back and forth and left wheel will go back slow while right wheel goes forward slow. Then left wheel will stop after my servo scan for 15degree to 165degree and back to 15degree while my right wheel will start turning back.