Using serial to Processing for sound, want to go stand alone..

Hello,

I am using an ultrasound sensor SRF 05 with Arduino Uno to control the amplitude of a sine wave. I am using Processing on a PC to create the sine wave using Minim and controlling the level by reading the serial port coming from Arduino.

Is there any way of having Arduino do all of this? i.e. make the oscillator and control the level and leave out the PC?

Many Thanks
Twogan

What frequency of sin wave do you want to produce?

See:-
http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/

Thanks for the response. Apologies for the delay in my response.

I need the the sin wave at 67hz (resonant freq of a steel sheet I am resonating with a contact transducer), if it was able to be altered between a range of 40hz to about 600hz that would be cool too, but not essential at this stage.

Thanks for the link. Any other hints and tips much appreciated!

Best Wishes

Twogan

Any other hints

No that link will do that fine over that frequency range.

Great thanks.

I just had a look at the link - looks perfect for the oscillator thanks so much Grumpy Mike.

So now I need to take my processing code and use it in arduino and include the code from the link above to make this all work without a PC..

I have to admit the code in the link is way over my head. Could you offer some advice on how I should best go about converting my processing patch code to an arduino patch and replace the minim oscillator with the on board arduino osc?

Any help again much appreciated!

Here's my processing code (apologies if its poorly formed. I tend to hack things together as and when I need things to work... I do learn along the way though, as I am doing today...) :slight_smile:

// This program takes ASCII-encoded strings
 // from the serial port at 9600 baud and graphs them. It expects values in the
 // range 0 to 1023, followed by a newline, or newline and carriage return
 
 // Created 20 Apr 2005
 // Updated 18 Jan 2008
 // by Tom Igoe
 // This example code is in the public domain.
 import ddf.minim.*;
import ddf.minim.signals.*;

Minim minim;
AudioOutput out;
SineWave sine;
 import processing.serial.*;
 float easing = 0.05;
 float easedVal;
 
 Serial myPort;        // The serial port
 int xPos = 1;         // horizontal position of the graph
 
 void setup () {
 // set the window size:
 size(400, 600);        
  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 67 Hz, at 0.5 amplitude, sample rate from line out
  sine = new SineWave(67, 1, 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);
 // List all the available serial ports
 println(Serial.list());
 // I know that the first port in the serial list on my mac
 // is always my  Arduino, so I open Serial.list()[0].
 // Open whatever port is the one you're using.
 myPort = new Serial(this, Serial.list()[1], 9600);
 // don't generate a serialEvent() unless you get a newline character:
 myPort.bufferUntil('\n');
 // set inital background:
 background(0);
 }
 void draw () {
 // everything happens in the serialEvent()
 }

 void serialEvent (Serial myPort) {
 // get the ASCII string:
 String inString = myPort.readStringUntil('\n');
 
 if (inString != null) {
 // trim off any whitespace:
 inString = trim(inString);
 // convert to an int and map to the screen height:
 float inByte = float(inString); 


//*** new bit atempt at Averaging
// inByte = map(inByte, 0, 1023, 0, height);
// float targetVal = inByte;
// easedVal += (targetVal - eased val) *easing;
 
 // draw the line:
 stroke(127,34,255);
 line(xPos, height, xPos, height - inByte*3);

 
 // at the edge of the screen, go back to the beginning:
 if (xPos >= width) {
 xPos = 0;
 background(0); 
 } 
 else {
 // increment the horizontal position:
 xPos++;

 float amp = map(inByte,20,300,1,0);
 sine.setAmp(amp); }
 }
 }

And here is what I have uploaded to the arduino:

int duration;                          // Stores duratiuon of pulse in
int distance;                          // Stores distance
int srfPin = 3;                        // Pin for SRF05

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

void loop(){
  pinMode(srfPin, OUTPUT);
  digitalWrite(srfPin, LOW);           // Make sure pin is low before sending a short high to trigger ranging
  delayMicroseconds(2);
  digitalWrite(srfPin, HIGH);          // Send a short 10 microsecond high burst on pin to start ranging
  delayMicroseconds(10);
  digitalWrite(srfPin, LOW);           // Send pin low again before waiting for pulse back in
  pinMode(srfPin, INPUT);
  duration = pulseIn(srfPin, HIGH);    // Reads echo pulse in from SRF05 in micro seconds
  distance = duration/58;              // Dividing this by 58 gives us a distance in cm
  Serial.println(distance);
  delay(50);                           // Wait before looping to do it again
}

Your processing code has graphics in it. How are you going to do that on an arduino?

very good point.

The graphics code is something I inserted so I had a visual reference on the sensor output. Only needed in the setting up stage.

All processing graphics code can be jettisoned from the arduino version.

The final piece is intended to be used in an art gallery as part of a sculpture so no extraneous screens or paraphernalia can be added..

I will strip that out now...

Forget what you have in Processing and just write the functions you want in the Arduino.

Thanks GM. I also just realised as I was trimming out the unneeded stuff from my processing sketch - I can loose basically everything apart from setting the amp level based on the sensor input...

OK so I am going to start again in Arduino.....

TBC....

Thanks
Twogan

Actually - 2 questions more at this stage:

  1. The link is based on using an Arduino Diecimila or Duemilenove. will this work on my Uno ?
  2. It mentions putting a pot across 5v / ground and analog 0 pin. Is this necessary or can I just set the fixed freq in the code?

I am really appreciating the guidance here.

Thanks
Twogan

ok freq setting looks simple, sorry

Ok I am going to restart this post in the programming questions section.....

  1. The link is based on using an Arduino Diecimila or Duemilenove. will this work on my Uno ?

Yes the two are just earlier versions but essentially they are the same.

Grumpy Mike, I started this thread:SRF-05 Proximity Sensor Controlling On Board Audio Oscillator - Amplitude? - Programming Questions - Arduino Forum

Perhaps you could have a look and let me know your thoughts?

Perhaps I should have just continued this here?

I cannot see any way of using a software method to alter audio amplitude with the Arduino as its +-5v PWM.

I could I suppose use the SRF distance figure to control the Pulse Width (duty cycle). Starting at 0 width for silence I suppose this could work. I will try....

Thanks
Twogan