Assignment 9.1

Post date: Jul 18, 2010 12:53:4 AM

Hooked up potentiometers to the arduino board, sent values through serial, read them in processing, same old stuff, the only addition being at the end the values are converted into frequencies / amplitudes and played using the minim library. Pretty simple - I did add an extra octave for a little extra complexity though.

ARDUINO CODE:

const int pot1 = 5;

const int pot2 = 4;

void setup () {

Serial.begin(9600); //begin serial communication at 9600 bps

}

void loop () {

//print to serial port

Serial.print(analogRead(pot1));

Serial.print(',');

Serial.println(analogRead(pot2));

delay(20);

}

PROCESSING CODE:

import processing.serial.*;

import ddf.minim.*;

import ddf.minim.signals.*;

Serial serPort;

Minim minim;

AudioOutput out;

SineWave sine;

SineWave octaveDown;

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());

// create second wave at half frequency (one octave down)

octaveDown = new SineWave(220, 0.5, out.sampleRate());

// set the portamento speed on the oscillator to 200 milliseconds

octaveDown.portamento(20);

sine.portamento(20);

// add the oscillator to the line out

out.addSignal(sine);

out.addSignal(octaveDown);

// set up serial buffer

serPort = new Serial(this, Serial.list()[0], 9600);

serPort.bufferUntil('\n');

}

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 serialEvent(Serial serPort) {

//store message from buffer in string

String message = serPort.readStringUntil('\n');

if(message != null) {

//remove whitespace and other nonsense

message = trim(message);

//set frequency to the pot value remapped to 60 - 1500 Hz

float freq = int(split(message, ','))[0];

sine.setFreq(map(freq, 0, 1023, 1500, 60));

//do the same to the octaveDown but cut the freq in half

octaveDown.setFreq(map(freq, 0, 1023, 1500, 60)/2);

//set amp to value of 2nd pot, remapped to 0 - 1

float amp = int(split(message, ','))[1];

sine.setAmp(map(amp, 0, 1023, 0, 1));

octaveDown.setAmp(map(amp, 0, 1023, 0, 1));

}

}

void stop()

{

//deallocate classes and whatnot

out.close();

minim.stop();

super.stop();

}

FRITZING DIAGRAM: