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,
distance = port.read(); // read it and store it in val
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!