Detecting Range to make Sound on computer

Hey there, I just got my arduino and an ultrasonic PING sensor. So I was wondering if I could Make the arduino code, so whenever the Ping sensor detects an object in certain distance the Arduino which is connected to the computer, can make the computer play a song on the desktop forexample.

So when someone will get close to the sensor, the computer will play a song.. possible??? :-?

Check this out:

This lets you play the song from the Arduino. Triggering which song to play based on distance measured by the Ping sensor is easy.

Yeah okay, not totally what I was looking for but close. Cant I play the song through the computer? Istead of using the SD card and the speakers.

You can send serial data to the computer. That could take the form of "Pretty please, play this song: TheSongNameAndPathGoesHere.wav".

Then, you'd need to create an application on the computer that would monitor the serial port, and understand the string that was sent in. That program might be able to invoke a wav file compatible application, and trigger it's play method.

Gobetwino is just such a program.

Sounds nice. I have looked at it, and it Arduino and Gobetwino, does seem to communicate. In arduino I write: int value = 877; char buffer[5]; Serial.print("#S|LOGTEST|["); Serial.print(itoa((value), buffer, 10)); Serial.println("]#"); And in Gobetwino I do as told in the manual.

But Arduino gives: error: expected constructor, destructor, or type conversion before '.' token

What i the problem then?

Two suggestions. One, post all your code, not just a snippet. The problem may not be in the snippet you posted.

Two, when an error occurs, the IDE highlights the line that it believes is the cause of the error. Often it isn't, but it would be helpful to know what line is highlighted in the IDE.

It is the whole code, and Serial.print("#S|LOGTEST|["); is the highlighted line.

But I want to make Gobetwino to open a sound file everytime the sensor detects a certain range, so I want to make the right code in arduino. I think I have to use the SPRID, from gobetwino. any ideas?

An Arduino sketch needs a setup function and a loop function. There may be some variables defined outside of these two functions. They are global variables. There can be no code outside of a function.

You have no setup function, no loop function, and code outside of a function.

I think you have a lot to learn about the Arduino and programming. Start here:

Once you get the basics down. adding other types of sensors and sending serial data around will be a lot easier.

I put it in to a working code now. And the Gobetwino i responding with outputs. But it doesn't start the program I want i to.. What do I need in this code to get a program started properly. Code: const int pingPin = 7; void setup() { // initialize serial communication: Serial.begin(9600); } void loop() { int value = 877; char buffer[5]; Serial.print("#SPRID|INDU|["); Serial.print(itoa((value), buffer, 10)); Serial.println("]#"); // 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); cm = microsecondsToCentimeters(duration); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); 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: 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; }

Serial.print(itoa((value), buffer, 10));

itoa converts a value to a string, stored in the specified array (buffer, in your case). It may, or may not, return a value. If it does, it is most certainly not what you want to be printing. At least, I don't think it's what you want to print. I presume that it is buffer that you want to print.

But, you are doing all this based on a hard-coded value, and then reading the Ping sensor. Shouldn't you be reading the sensor first, then sending data based on the sensor value?

Yes, it is surpose to start a program every time the sensor return a certain value(for example when a person walks by). I know the code doesn't do that now. But I am surpose to start the program with the SPRID command to use gobetwino, and start a program(in my case a music file). And I can find out how to use the SPRID, and the program that I have stated in my gobetwino by the name INDU, does not start. So can you tell me how i do this?

I just wrote a library for using the Ping))) sensor ( It might simplify your code a bit, but I need testers either way.

Thanks but doesn’t seem to work, maybe because I am a rookie.
but the message from arduino is:

18: error: Ping.h: No such file or directory In function ‘void loop()’:
Bad error line: -3

And my code is:

#include <Ping.h>

Ping ping = Ping(13,74,29);

void setup(){

void loop(){;
Serial.print("Microseconds: “);
Serial.print(” | Inches “);
Serial.print(” | Centemeters: ");

And can anybody explain how to use the SPRID command right??(look back in the post)

The error you are receiving is because you have not installed the library. Download this file: Then copy the folder "Ping" into hardware/libraries/ in the Arduino program folder. Also try initializing with Ping(13,0,0) instead of Ping(13,74,29). And make sure you set the first number (ie. 13) to the pin your Ping))) is attached to.

To play a sound file with Gobetwino, install VLC media player ( Make a new command in Gobetwino and name it "SOUND". In the drop down, select SPRID. In the program path box, type "C:\Program Files\VideoLAN\VLC\vlc.exe", or whatever the path to vlc.exe is for you. In the command line arguments box, put "C:\Windows\Media\chimes.wav", or whatever the path to your sound file is. To run this from your Arduino, run Serial.println("#S|SOUND|[]#");

Thanks, just the information I need. but I put the ping in the right folder, and the codes can be upload, but the output from the serial monitor, is:


And it goes on..

That looks suspiciously like a mismatch between the Serial.begin baud rate and the Serial Monitor baud rate.

Try changing the baud in your Arduino code to 9600. That's the (slower) standard used by the Arduino serial monitor and Gobetwino.


I was working on a project similar to the one posted here. Just that I use an LDR to detect light intensity. I was successfully able to use serial comunication between my comp( a linux box) and arduino using processing. I used minim library and the serial communication library and successfully came up with a working solution :-)

If anyone is interested, I could post the code online!