Go Down

### Topic: Making Robot Beep When Object Detected? (Read 2733 times)previous topic - next topic

#### cherryguy

##### Jul 24, 2012, 10:47 pm
So, I have a working autonomous robot that works with a ping))) sensor and a rear motor and a directional forward motor. I have a piezo speaker laying around, not many specs on it but it works with the example sketch called 'toneMelody'. I'm not much of a code guy but ANY help is appreciated. I have an IF function and I should be able to just add something to it but im not sure what method/function to add. Heres my code.

Code: [Select]
`/*RC Car to Robot Conversionby Nathan BrownUsed to convert an RC car into a robot that uses a PING sensor to avoid obstacles,and an Arduino motor shield for motor control.For more information see:http://www.instructables.com/id/RC-Car-to-Robot/Built atop sketch by Randy Sarafan which wasBuilt atop Ping example code by Tom Igoe*/// this constant won't change.  It's the pin number// of the sensor's output:const int pingPin = 7;void setup() {    //establish motor direction toggle pins  pinMode(12, OUTPUT); //drive motor -- HIGH = forwards and LOW = backwards  pinMode(13, OUTPUT); //turn motor -- HIGH = left and LOW = right    //establish motor brake pins  pinMode(9, OUTPUT); //brake (disable) the drive motor  pinMode(8, OUTPUT); //brake (disable) the turn motor  //Turns brake off for drive motor  digitalWrite(9, LOW);   //Turns brake on for turn motor  digitalWrite(8, HIGH);   //Sets initial speed of drive motor  analogWrite(3, 200);    //Sets initial direction of drive motor  digitalWrite(12, HIGH);}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 < 24){        //brake drive motor and pause 1/10 second    digitalWrite(9, HIGH);    delay(100);    //    //setting turn motor    //        //turn off brake for turn motor     digitalWrite(8, LOW);    //set turn motor direction    digitalWrite(13, HIGH);    //activate turn motor    analogWrite(11, 255);        //    //setting drive motor    //        //turn off brake of drive motor    digitalWrite(9, LOW);         //set drive motor backwards direction    digitalWrite(12, LOW);        //activate the drive motor    analogWrite(3, 200);        //backup for 2 seconds    delay(2000);        //    //stopping    //        //brake both motors    digitalWrite(8, HIGH);    digitalWrite(9, HIGH);          }    //  //when nothing is within 12"   //the robot simply drives forwards  //    else{        //    //Setting drive motor    //        //set drive motor forward direction    digitalWrite(12, HIGH);        //turn off brake of drive motor    digitalWrite(9, LOW);            //activate drive motor    analogWrite(3, 200);      }    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;}`

#### cherryguy

#1
##### Jul 24, 2012, 10:48 pm
Maybe even add an LED flash to it?

#### warunasanjaya11

#2
##### Jul 25, 2012, 12:23 amLast Edit: Oct 19, 2012, 07:19 am by warunasanjaya11 Reason: 1
There are two things I do not understand. The ping))) sensor assesses space using sonar. Not for detecting objects. How do you modify that kind of device for your robot? Also Piezoelectric speakers are often used as beepers in watches and other electronic gadgets. For what purpose are you going to use that kind of device?
I am sorry that I could not help with your code.

Dashquid
fatlossprofessional.co.uk
fatlossprofessional
mobilehelper
securetrip
whichpetcover
abc
craigslist

#### arduinopi

#3
##### Jul 25, 2012, 03:17 amLast Edit: Jul 25, 2012, 03:34 am by arduinopi Reason: 1
if the piezo speaker uses less than 30 mA (almost all do unless it's extremely large) then you can attach the red wire to a arduino pin and the black to ground. to code it you would make the pin a output as usual. to play noise through it you would put this for example.
tone(piezoPin,2000,500);
this would play a 2000 hertz square wave on the  piezo pin for 500 milliseconds

All of my projects --> http://www.frankmagazu.com/

#### cherryguy

#4
##### Jul 25, 2012, 04:41 am

if the piezo speaker uses less than 30 mA (almost all do unless it's extremely large) then you can attach the red wire to a arduino pin and the black to ground. to code it you would make the pin a output as usual. to play noise through it you would put this for example.
tone(piezoPin,2000,500);
this would play a 2000 hertz square wave on the  piezo pin for 500 milliseconds

Thank you so much. I used this code and it worked perfectly, it plays a bit of a two tone beep when it realizes hey, theres 'something' 2 feet ahead of me, let me put my breaks on, play this for 200 milliseconds, wait 300 milliseconds, and play this tone for 300 milliseconds. I'm showing it off at work tomorrow

#### chrisandcarleen

#5
##### Dec 10, 2012, 03:54 am
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.
Code: [Select]
`// 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.

Go Up