Skip to content

Instantly share code, notes, and snippets.

@lizkhoo
Created December 5, 2012 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizkhoo/4211725 to your computer and use it in GitHub Desktop.
Save lizkhoo/4211725 to your computer and use it in GitHub Desktop.
PComp Final: Loop homeowner stories, 2 triggers
import ddf.minim.*;
import processing.serial.*;
AudioPlayer[] sounds = new AudioPlayer[7];
Minim minim;
boolean firstContact = false;
boolean switch1Ready = false;
boolean switch2Ready = false;
Serial myPort;
float switch1;
float switch2;
int switch1State = 0;
int switch2State = 0;
int pSwitch1State = 0;
int pSwitch2State = 0;
long marker1, marker2 = 0; //holds millis reading
int threshold = 1000; //limits number of button starts to 1 per sec
int counter = 0; //counts which song in array to play
void setup() {
size(200, 200);
println(Serial.list());
minim = new Minim(this);
sounds[0] = minim.loadFile("foreclosure01_paybills.wav");
sounds[1] = minim.loadFile("foreclosure02_taxes.wav");
sounds[2] = minim.loadFile("foreclosure07_loanprocessing.wav");
sounds[3] = minim.loadFile("foreclosure04_negotiating.wav");
sounds[4] = minim.loadFile("foreclosure05_cycle.wav");
sounds[5] = minim.loadFile("foreclosure06_underwater.wav");
sounds[6] = minim.loadFile("foreclosure03_cutcosts.wav");
String portName = Serial.list()[4];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void draw() {
background(255);
if (switch1Ready == true) {
if (switch1State == 1 && pSwitch1State == 0 && millis()-marker1 > threshold) {
marker1 = millis();
sounds[counter].play();
sounds[counter].rewind();
if (counter < sounds.length-1) {
counter++;
}
else {
counter = 0;
}
pSwitch1State = switch1State;
}
//need to store the off position as the previous state in order to turn on again
if (switch1State == 0 && pSwitch1State == 1) {
pSwitch1State = switch1State;
switch1Ready = false;
//println("switch1Ready: "+ switch1Ready);
}
}
if (switch2Ready == true) {
if (switch2State ==1 && pSwitch2State == 0 && millis()-marker2 > threshold) {
marker2 = millis();
sounds[counter].play();
sounds[counter].rewind();
if (counter < sounds.length-1) {
counter++;
}
else {
counter = 0;
}
pSwitch2State = switch2State;
}
//end else if
if (switch2State ==0 && pSwitch2State == 1) {
pSwitch2State = switch2State;
switch2Ready = false;
}
}
}
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil('\n');
if (myString != null) {
myString = trim(myString);
// if you haven't heard from the microncontroller yet, listen:
if (firstContact == false) {
if (myString.equals("hello")) {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
println(myString);
}
}
else {
int switches[] = int(split(myString, ','));
switch1State = switches[0];
switch2State = switches[1];
// print out the values you got:
for (int i = 0; i < switches.length; i++) {
//print("Switch " + i + ": " + switches[i] + "\t" + counter + "\t");
}
//println();
if (switches.length > 1) {
if (switches[0] == 1) {
switch1Ready = true;
}
if (switches[1] == 1) {
switch2Ready = true;
}
}//end check switchs.length
}//end Else statement
myPort.write("A");//get more data
}//end check serial port String
}//end void serialEvent
void stop() {
//need these steps to close I/O classes before program exit
for (int i=0; i< sounds.length-1; i++) {
sounds[i].close();
}
minim.stop();
super.stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment