who wants to help with code?
September 21, 2009
this is the code for my new robot it uses two servos to drive forward and a ping mounted on a servo that sweeps back and forth when it senses something 9inches or closer it backs up and turns right. what i would like it to do is when is sees something it will take the angle of the servo and if its right it will turn left if its left it will turn right. any ideas? btw i cant claim the two main peices of code one was found here will give credit when i remember who posted it. 2nd is the ping sample
//servo bot with ping sweeper
byte currentPos = 0;
byte positioner = 15;
const int pingPin = 7; //ping on pin 7
if ( currentPos == 0 )
positioner = 20;
else if ( currentPos == 180 )
positioner = -20;
currentPos += positioner;
leftservo.attach(9); //left servo pin 9
rightservo.attach(10); //right servo pin 10
pingservo.attach(11); //ping servo pin 11
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
if( inches <= 9 )//if the robot sees something 9in or closer it backs up and turns
else if (inches >9 )//if not it goes forward
long microsecondsToInches(long microseconds)
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
long microsecondsToCentimeters(long microseconds)
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;