Assignment 9.1

Post date: Jul 15, 2010 5:13:54 PM

I built a completely new circuit for this one. I got rid of the push buttons and added two potentiometers attached to analog pins 0 and 4 so that I could get two analog values to control the frequency(pitch) and amplitude(volume).

Arduino:

int potPin1 = 0; // set up variables

int potPin2 = 4;

int val1 = 0;

int val2 = 0;

void setup() {

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

}

void loop() {

val1 = analogRead(potPin1); // read potPin value and store in val1

val1 = map(val1, 0, 1023, 0, 4978); // adjust values

val2 = analogRead(potPin2);

val2 = map(val2, 0, 1023, 0, 4978);

Serial.print(val1, DEC); // print values and separate with a comma

Serial.print(',');

Serial.println(val2, DEC);

}

Processing:

import processing.serial.*; // import libraries

import ddf.minim.*;

import ddf.minim.signals.*;

Minim minim; // set up variables

AudioOutput out;

SineWave sine;

Serial myPort;

float freq;

float amp;

void setup() {

size(400, 220, P2D); // sets window size

minim = new Minim(this);

// get a line out from Minim with default bufferSize, sample rate, and bit depth

out = minim.getLineOut(Minim.STEREO);

// create a sine wave oscillator, set to value of freq, value of amp, sample rate from line out

sine = new SineWave(freq, amp, 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);

// declares myPort and begins serial communication at 9600 bps

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

// store values from myPort in a buffer until value is reached

myPort.bufferUntil('\n');

}

void draw() {

background(0); // sets background color

stroke(255); // sets stroke color

// 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 myPort) {

// read string and store it in a buffer until value is reached

String myString = myPort.readStringUntil('\n');

// if there is something in myString

if(myString != null) {

// split it at the comma

myString = trim(myString);

int sensors[] = int(split(myString, ','));

for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {

// print the sensor values

print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");

}

println();

if(sensors.length > 1) {

// adjust the value of sensors[0] and set as freq

float freq = map(sensors[0], 0, 4978, 1500, 60);

// set the frequency to the new values

sine.setFreq(freq);

// adjust the value of sensors[1] and set as amp

float amp = map(sensors[1], 0, 4978, -1, 1);

// set the amplitude to the new values

sine.setAmp(amp);

}

}

sine.setPan(0); // set speaker balance to even

}

void stop() {

out.close();

minim.stop();

super.stop();

}

A Fritzing picture of the circuit is attached.