Arduino / Ping / Sine Wave

Hello!

I’m totally new to arduino and processing. I’m not very proficient at reading code yet. What I’m looking to do is control this program for a sine wave signal (below) with the PING motion sensor.

I’m basically looking to make a theremin. In the sine program, when the mouse is at the bottom of the window, the wave is wide and the sound is low. At the top, the wave is tighter and the sound high. I want to program it so when the ping reads a certain distance, say 3 inches, the frequency is high. 4 inches, slightly lower, 5 inches, slightly lower, 6 inches slightly lower, ect.

This is for an art installation in which someone will be wearing a suit with several ping sensors on it, connected to an arduino, connected to a rotating speaker mounted on a helmet.

So instead of the sine wave program reading the position of a mouse, I’d like it to read the ping sensor.
Here is the code for the sine wave, which is currently controlled by a mouse -

import ddf.minim.*;
import ddf.minim.signals.*;

Minim minim;
AudioOutput out;
SineWave sine;

void setup()
{
 size(512, 200, P2D);
 
 minim = new Minim(this);
 // get a line out from Minim, default bufferSize is 1024, default sample rate is 44100, bit depth is 16
 out = minim.getLineOut(Minim.STEREO);
 // create a sine wave Oscillator, set to 440 Hz, at 0.5 amplitude, sample rate from line out
 sine = new SineWave(440, 0.5, out.sampleRate());
 // set the portamento speed on the oscillator to 200 milliseconds
 sine.portamento(200);
 // add the oscillator to the line out
 out.addSignal(sine);
}

void draw()
{
 background(0);
 stroke(255);
 // draw the waveforms
 for(int i = 0; i < out.bufferSize() - 1; i++)
 {
   float x1 = map(i, 0, out.bufferSize(), 0, width);
   float x2 = map(i+1, 0, out.bufferSize(), 0, width);
   line(x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
   line(x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
 }
}

void mouseMoved()
{
 // with portamento on the frequency will change smoothly
 float freq = map(mouseY, 0, height, 1500, 60);
 sine.setFreq(freq);
 // pan always changes smoothly to avoid crackles getting into the signal
 // note that we could call setPan on out, instead of on sine
 // this would sound the same, but the waveforms in out would not reflect the panning
 float pan = map(mouseX, 0, width, -1, 1);
 sine.setPan(pan);
}

void stop()
{
 out.close();
 minim.stop();
 
 super.stop();
}

Can anyone provide some help with this? Would greatly appreciate any guidance.

This is the Ping Code -

const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

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);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds)
{
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;
}

Thanks in advance for any advice or guidance.

Hi,

add a SineWave to your second sketch. In loop() after calculationg the distance add the following:

float freq = map(duration, 0, 1480, 2000, 400); sine.setFreq(freq);

duration increases by 74 for each inch, so 0..1480 is 0..20 inch. The frequency is 2000Hz at 0 inch and 400Hz at 20 inch. You can change these values as you need.

godd luck Mike

hey...

i tried this, but didn't get any results

wondering if anyone has any other suggestions?

i guess i'm not sure what you mean by duration?

"duration" from the sketch? The round-trip timing variable?

hey , am actually trying to make something very similar, did you have any luck yet with it? will keep ya posted if i manage to get it working :)