# Ping Sensor to MIDI

Dear Arduino community!

I'm currently working on my examination-project at an art-college in the north of Norway. I am very (!) new to Arduino and mostly base my codes on others work.

As for now, I'm trying to combine two codes:

int pingPin = 7;

void setup() { Serial.begin(9600); }

void loop() { long duration, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // We 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(" ");// space in ascii Serial.print(cm); Serial.println();

delay(10); }

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; }

AND

void loop() { // subtract the last reading: total= total - readings[index]; // read from the sensor: readings[index] = ping(); // add the reading to the total: total= total + readings[index]; // advance to the next position in the array: index = index + 1; // if we're at the end of the array... if (index >= numReadings) // ...wrap around to the beginning: index = 0; // calculate the average: average = total / numReadings; // send it to the computer (as ASCII digits) Serial.println(average, DEC); delay(250); //delay 1/4 seconds. }

As I lack all knowledge on how to combine the two of these, I am asking you for help.

Oh, and my project has due on Monday. As late as ever, huh?

Cheers, and thank you for all your help!

All the best, Fridtjof

Oh, I should probably add that I need to smooth the signal in order to get away from the misreadings from the ping sensor.

As I lack all knowledge on how to combine the two of these, I am asking you for help.

However I don't see any MIDI code in there.

I'm converting the signal to MIDI in Max/MSP, so thats not included in the code.

wesseltoften: As I lack all knowledge on how to combine the two of these, I am asking you for help.

Hi, what you need to do is provide two functions for your sketch.

In this line from your second loop() code:

``````readings[index] = ping();
``````

you are requesting a value from a function named 'ping()'.

This is a function that you must provide.

So in your first code you'd have to rewrite/rename your loop() function to the name 'ping()' instead of 'loop()' and provide a 'return value' from the function (instead of 'void', which means 'no return value'). You then can copy that function into the second code and use it.

But about the timing you have to think of: - how often do you want to measure the distance? - how often do you want to print the distance average?

As your sensor is based on sound/echo, there must be a waiting time: Before you trigger a new sound, all echos in the room must have gone. Otherwise echos from a previous triggering might conflict with the next distance measuring.

Do you have the first sketch (distance measuring) up and working with the ultrasonic sensor on pin-7? Is it really a sensor where the 'trigger' and 'echo' pin is the same? So you trigger the sensor on pin-7 and get also the echo on pin-7?

What is the expected distance range you want to measure? Is it the full range (3m) of the sensor? Or can you limit the range so some smaller range of values that are of interest in your program?