so i bought a HS 311 to use for the swivel mount for my ping sensor but for some odd reason the servo only turns left. I have been googling this problem since last night and really havent found an easy solution besides people saying buy another servo.
/*MAEP 2.0 Navigation
by Noah Moroze, aka GeneralGeek
This code has been released under a Attribution-NonCommercial-ShareAlike license, more info at http://creativecommons.org/licenses/
PING))) code by David A. Mellis and Tom Igoe http://www.arduino.cc/en/Tutorial/Ping
Edited by: Jason Miller for Project KITT
*/
#include <Servo.h> //include Servo library
#include <LiquidCrystal.h> //include lcd library
LiquidCrystal lcd(2, 3, 6, 7, 8, 9);
const int RForward = 0;
const int RBackward = 180;
const int LForward = RBackward;
const int LBackward = RForward;
const int RNeutral = 90;
const int LNeutral = 90; //constants for motor speed
const int pingPin = 4;
const int dangerThresh = 5; //threshold for obstacles (in inches)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print("Hello Jason");
delay(1000);
lcd.clear();
lcd.print("Activating KITT");
delay(1000);
lcd.clear();
lcd.print("KITT Online");
delay(1000);
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(5); //attach motors to proper pins
panMotor.write(90); //set PING))) pan to center
}
void loop()
{
int distanceFwd = ping();
if (distanceFwd>dangerThresh) //if path is clear
{
leftMotor.write(LForward);
rightMotor.write(RForward); //move forward
lcd.print("Forward");
}
else //if path is blocked
{
leftMotor.write(LNeutral);
rightMotor.write(RNeutral);
panMotor.write(0);
delay(500);
rightDistance = ping(); //scan to the right
lcd.print("Scan Right");
delay(500);
panMotor.write(180);
delay(700);
leftDistance = ping(); //scan to the left
lcd.print("Scan Left");
delay(500);
panMotor.write(90); //return to center
delay(100);
compareDistance();
}
}
void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
leftMotor.write(LBackward);
rightMotor.write(RForward); //turn left
lcd.print("Turn Left");
delay(500);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn right
lcd.print("Turn Right");
delay(500);
}
else //if they are equally obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn 180 degrees
lcd.print("Turn 180");
delay(500);
}
}
long ping()
{
// Send out PING))) signal pulse
long duration, inches;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//Get duration it takes to receive echo
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
lcd.print(inches);
lcd.print("in to obstacle");
delay(100);
lcd.clear();
delay(100);
}
long microsecondsToInches(long microseconds)
{
return duration / 29 / 2;
}