I am using your code also with some modifications for 2 drive wheels and a ping)) with a piezo buzzer on detection. usina a uno r3 thruhole a r3 motorshield, an a magician chassis from sparkfun. Here is the modded code. please help me toggle a led on when the piezo sounds.
// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin = 6;
const int piezoPin = 5;
void setup() {
//establish motor direction toggle pins
pinMode(12, OUTPUT); //drive motor A -- HIGH = forwards and LOW = backwards
pinMode(13, OUTPUT); //drive motor B -- HIGH = forwards and LOW = backwards
//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) the drive A motor
pinMode(8, OUTPUT); //brake (disable) the drive B motor
//Turns brake off for drive A motor
digitalWrite(9, LOW);
//Turns brake off for drive B motor
digitalWrite(8, LOW);
//Sets initial speed of drive 1 motor
analogWrite(3, 255);
//Sets initial speed of drive 2 motor
analogWrite(11, 255);
//Sets initial direction of drive motor 1
digitalWrite(12, LOW);
//Sets initial direction of drive motor 2
digitalWrite(13, LOW);
}
void loop()
{
// 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:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// 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.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
//
//if objects are less than 12 inches away
//the robot reverses and turns to the right
//for 2 seconds
//
if (inches < 12){
//make a beep for indication of wall
tone(piezoPin,2000,500);
//brake drive motors 1 and 2 and pause 1/10 second
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
delay(100);
//
//setting turn routine
//
//turn off brake for drive 1 and 2 motor
digitalWrite(8, LOW);
digitalWrite(9, LOW);
//set drive 1 and 2 motor direction
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
//activate drive 1 and 2 motor
analogWrite(3, 200);
analogWrite(11, 200);
//backup for 2 seconds
delay(800);
//
//stopping
//
//brake both motors
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
}
//
//when nothing is within 12"
//the robot simply drives forwards
//
else{
//
//Setting drive motor
//
//set drive motor 1 forward direction
digitalWrite(12, LOW);
//set drive motor 2 forward direction
digitalWrite(13, LOW);
//turn off brake of drive motor1
digitalWrite(9, LOW);
//turn off brake to drive motor 2
digitalWrite(8, LOW);
//activate drive motor 1
analogWrite(3, 200);
//activate drive motor 2
analogWrite(11, 255);
}
delay(100);
}
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;
}
the mismatched pwm settins are there to make it drive straight forward.