Jim's
Tutorials

Fall 2011
course
navigation

nov 4

This morning I got the output of a bend sensor (previously graphed) to pair with the frequency of a sine wave (courtesy of the Minim library). This was surprisingly simple.
For the actual hardware attached to the arduino, I looked at this page: http://www.ecs.umass.edu/ece/m5/tutorials/CdS_Flex_Sensor.html ...specifically this image: http://www.ecs.umass.edu/ece/m5/images/Flex_Sensor_Breadboard_S.jpg
I'm still using an inexact resistor value for the bend sensor. Its output is quite noisy though the input can be easily smoothed by using a piece of code similar to what I previously wrote for Gadgets.
For the Arduino side, I used something like this (he doesn't declare A0): void setup() { int analog_pin1 = 1; // initialize the serial communication: Serial.begin(9600); } void loop() { // send the value of analog input 0: Serial.println(analogRead(analog_pin1)); // wait a bit for the analog-to-digital converter // to stabilize after the last reading: delay(10); }
For the processing end of things, I ended up splicing two pieces of code together. I figured out how to set up a sine wave using the polyphony example sketch (from the Minim library) I didn't end up saving this version of things but it wasn't too different from the dual sine audio out which I just got working tonight (11-04-11 ~8:30PM). : // [Dual] Graphing sketch (modified to graph two inputs and output two sine waves (using Minim) // 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 [in a comma separated list (example: "10, 20, 30")], 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.*; //import controlP5.*; import processing.serial.*; Minim minim; AudioOutput out; SineWave sine1; SineWave sine2; //TriangleWave tri; //ControlP5 gui; //public float triAmp = 0.5; //public float triPan = 0; //public float triFreq = 880; //public float triFreq2 = 1; float sin1Amp = 0.5; //these floats were public. I'm not sure why that was necessary. It was probably for the controlP5 GUI. float sin1Pan = 0.5; float sin1Freq = 440; float sin2Amp = 0.5; float sin2Pan = 0.5; float sin2Freq = 330; int sin1Glide = 50; //ms int sin2Glide = 50; //ms Serial myPort; // The serial port int xPos = 1; // horizontal position of the graph int x_size = 1000; int y_size = 500; int switcher = 0; void setup () { // set the window size: size (x_size, y_size); // this line also had ", P3D" as a parameter of size. We're just using 2 dimensions right now. minim = new Minim(this); // get a stereo line out from Minim with a 2048 sample buffer, default sample rate is 44100, bit depth is 16 out = minim.getLineOut(Minim.STEREO, 8000); // create a sine wave Oscillator, set to 440 Hz, at 0.5 amplitude, sample rate 44100 to match the line out sine1 = new SineWave(440, 0.5, out.sampleRate()); sine2 = new SineWave(330, 0.5, out.sampleRate()); // set the portamento speed on the oscillator to 200 milliseconds sine1.portamento(sin1Glide); sine2.portamento(sin2Glide); // add the oscillator to the line out out.addSignal(sine1); out.addSignal(sine2); // create a triangle wave Oscillator //tri = new TriangleWave(880, 0.5, out.sampleRate()); //tri.portamento(200); //out.addSignal(tri); // 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()[0], 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); int incomingValues[] = int(split(inString, ",")); // convert to an array of ints ***taken from serialEvent.pde line 8 in http://web.media.mit.edu/~plusea/downloads/code/p_rSkin_11x11.zip *** //float inByte = float(inString); sine1.setFreq(incomingValues[0]); //This updates the pitch of the sine wave every time the graph is drawn. sine2.setFreq(incomingValues[1]); //scale the output into the bounds of the box. float graphline1 = map(incomingValues[0], 0, 1023, 0, height); float graphline2 = map(incomingValues[1], 0, 1023, 0, height); // draw the line: switcher += 1; if(switcher % 2 == 1){ stroke(0,0,255); line(xPos, height, xPos, (height - graphline1)); } else{ stroke(0, 255, 0); line(xPos, height, xPos, (height - graphline2)); } // at the edge of the screen, go back to the beginning: if (xPos >= width) { xPos = 0; background(0); } else { // increment the horizontal position: xPos++; } } }
This code is quite buggy. It works some of the time if I fidget with the sensors a bit (though I'm not entirely sure this is causing it).
As I don't know how to do transparency with processing (probably more trouble than it's worth) I interleaved the two inputs which made a pretty graph (file attached).
http://cs.marlboro.edu/ courses/ fall2011/jims_tutorials/ aaron/ nov_4
last modified Monday November 7 2011 10:36 am EST

attachments [paper clip]

     name last modified size
[IMG]Screenshot-simple_analog_read_sound.png Nov 4 2011 10:19 pm 4.91kB