Assignment 9.2

Post date: Jul 17, 2010 2:29:6 AM

This circuit uses four push buttons connected to pins 4-7 on the Arduino to control sound in Processing. For each button pressed, a different drum sound is played in Processing. For example: when the button connected to pin 4 is pressed, a cowbell sound is played. The sounds can also be played by pressing keys k, j, h, and g.

Arduino code:

int switchPin1 = 4; // declares variables

int switchPin2 = 5;

int switchPin3 = 6;

int switchPin4 = 7;

void setup() {

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

pinMode(switchPin1, INPUT); // set switchPins to input

pinMode(switchPin2, INPUT);

pinMode(switchPin3, INPUT);

pinMode(switchPin4, INPUT);

}

void loop() {

if(digitalRead(switchPin1) == HIGH) { // if button is pressed

Serial.print(1, DEC); // print this number

delay(100); // and delay for 100 milliseconds

}

else if(digitalRead(switchPin2) == HIGH) {

Serial.print(2, DEC);

delay(100);

}

else if(digitalRead(switchPin3) == HIGH) {

Serial.print(3, DEC);

delay(100);

}

else if(digitalRead(switchPin4) == HIGH) {

Serial.print(4, DEC);

delay(100);

}

}

Processing code:

import processing.serial.*; // import libraries

import ddf.minim.*;

Minim minim; // declare variables

AudioSample cowbell;

AudioSample bass;

AudioSample kick;

AudioSample snare;

Serial myPort;

void setup()

{

// sets screen size

size(512, 200, P2D);

// always start Minim before you do anything with it

minim = new Minim(this);

// load file from the data folder, with a 512 sample buffer

cowbell = minim.loadSample("Cowbell.mp3");

bass = minim.loadSample("Bass.wav");

kick = minim.loadSample("Kick.wav");

snare = minim.loadSample("Snare.wav");

// sets port to read from

String portName = Serial.list()[0];

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

}

void draw()

{

int val = 0; // resets value every time draw is run

background(0); // sets background color

stroke(255); // sets stroke color

// use the mix buffer to draw the waveforms.

// because these are MONO files, we could have used the left or right buffers and got the same data

for (int i = 0; i < cowbell.bufferSize() - 1; i++)

{

line(i, 100 - cowbell.left.get(i)*50, i+1, 100 - cowbell.left.get(i+1)*50);

}

for (int u = 0; u < bass.bufferSize() - 1; u++)

{

line(u, 100 - bass.left.get(u)*50, u+1, 100 - bass.left.get(u+1)*50);

}

for (int y = 0; y < kick.bufferSize() - 1; y++)

{

line(y, 100 - kick.left.get(y)*50, y+1, 100 - kick.left.get(y+1)*50);

}

for (int t = 0; t < snare.bufferSize() - 1; t++)

{

line(t, 100 - snare.left.get(t)*50, t+1, 100 - snare.left.get(t+1)*50);

}

if (myPort.available() > 0) { // if there is data available to read,

val = myPort.read(); // read it and store it in val

println(val); // print the value of val

}

if (val == 49 ) cowbell.trigger(); // if val is this, play the sound

if (val == 50 ) bass.trigger();

if (val == 51 ) kick.trigger();

if (val == 52 ) snare.trigger();

}

void keyPressed()

{

if ( key == 'k' ) cowbell.trigger(); // if this key is pressed, play this sound

if ( key == 'j' ) bass.trigger();

if ( key == 'h' ) kick.trigger();

if ( key == 'g' ) snare.trigger();

}

void stop()

{

// always close Minim audio classes when you are done with them

cowbell.close();

bass.close();

kick.close();

snare.close();

minim.stop();

super.stop();

}

This one took me a while to figure out. It took me a couple tries to figure out where exactly to save the sound clips so that they could be read in the program, and then when i got that worked out the sound would repeat until another button was pressed, and then that sound would repeat. I solved that by resetting val every time the void draw function was run. I still have no idea where the values change from 1-4 to 49-52, but I got the program working and I didn't want to mess it up. A Fritzing picture of my circuit is attached.