# Arduino Air Instrument with Ultrasonic Sensor and Processing

This is my first time using processing although I’ve been making Arduino projects for awhile. I’m trying to to create an instrument that changes pitch as the distance from an ultrasonic sensor varies. So far I am unable to get it to work. Here is my code so far which I based off an example from the Sound library.

``````/**
* Processing Sound Library, Example 1
*
* Five sine waves are layered to construct a cluster of frequencies.
* This method is called additive synthesis. Use the mouse position
* inside the display window to detune the cluster.
*/

import processing.sound.*;
import processing.serial.*;

Serial port;  // Create object from Serial class
float distance;      // Data received from the serial port

SinOsc[] sineWaves; // Array of sines
float[] sineFreq; // Array of frequencies
int numSines = 5; // Number of oscillators to use

void setup() {

size(640, 360);
background(255);

port = new Serial(this, "COM6", 9600);

sineWaves = new SinOsc[numSines]; // Initialize the oscillators
sineFreq = new float[numSines]; // Initialize array for Frequencies

for (int i = 0; i < numSines; i++) {
// Calculate the amplitude for each oscillator
float sineVolume = (1.0 / numSines) / (i + 1);
// Create the oscillators
sineWaves[i] = new SinOsc(this);
// Start Oscillators
sineWaves[i].play();
// Set the amplitudes for all oscillators
sineWaves[i].amp(sineVolume);
}
}

void draw() {

if (0 < port.available()) {  // If data is available to read,
println(distance);
//Map mouseY from 0 to 0 to 1
float yoffset = map(distance, 0, 360, 0, 1);
//Map mouseY logarithmically to 150 - 1150 to create a base frequency range
float frequency = pow(1000, yoffset) + 150;
//Use mouseX mapped from -0.5 to 0.5 as a detune argument
float detune = map(mouseX, 0, width, -0.5, 0.5);

for (int i = 0; i < numSines; i++) {
sineFreq[i] = frequency * (i + 1 * detune);
// Set the frequencies for all oscillators
sineWaves[i].freq(sineFreq[i]);
}
}

}
``````

Here also in my Arduino code.

``````// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
float distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
if (0 < distance < 200) {
Serial.println(distance);}
delay(100);
}
``````

I’m getting this error:
ERROR: /node/set: Control input index 2 out of range for synth 5. It doesn’t seem like my data is being sent properly from the Arduino.

Beginner here any help very much appreciated!

What is that extra } at the bottom of your processing code?

Nvm, but try reading until a new line in your processing code.

``````myPort.bufferUntil('\n');
``````

I was able to fix the serial communication issue by changing the variable I’m using - “distance” from float to integer. It now is communicating with processing and is working. I’m still getting the same ERROR: /node/set: Control input index 2 out of range for synth 5 so I’m still working on that part. The “instrument” part is now working also.

When do you get that error? Does the sketch stop working? Does it highlight a line in your code? If it does, which one?

Did you do that change in your ino or pde code? Notice in your ino you are working with decimal type, so you should be working with floats. But then you say everything is working there, so it is not clear.

You can run the serial monitor and check what values you get. You can even test if the sensor works by adjusting the testing distance. This is a very good check instead of trying to figuring this out in the pde code.

Kf