Radio Project


October 2004

 
 
 
 
 
 
 

 

code:

Sample[] rightwing = new Sample[6];
Sample[] leftwing = new Sample[6];
int[] data = new int[2];
Sample clip;

int index; //serial buffer counter
boolean leftMotion, rightMotion;
int randompic;
int playing;
String pSide;

String leftVolume,rightVolume;

Timer clipTimer,mainTimer;
Timer leftMotionTimer, rightMotionTimer;

void setup() {
size(100,100);
beginSerial();
serialWrite(65);
clipTimer = new Timer();
mainTimer = new Timer();
leftMotionTimer = new Timer();
rightMotionTimer = new Timer();

Sonia.start(this);
leftwing[0] = new Sample("Kennedy.aif");
leftwing[1] = new Sample("VivaKennedy.aif");
leftwing[2] = new Sample("ILovetheGov.aif");
leftwing[3] = new Sample("OldMacdonald.aif");
leftwing[4] = new Sample("Doubletalk.aif");
leftwing[5] = new Sample("CountingGirl.aif");

rightwing[0] = new Sample("PledgeOfAlligance.aif");
rightwing[1] = new Sample("IkeForPresident.aif");
rightwing[2] = new Sample("ManFromAblain.aif");
rightwing[3] = new Sample("InYourHeart.aif");
rightwing[4] = new Sample("HopefulCampaign.aif");
rightwing[5] = new Sample("HighGasPrices.aif");

}

void loop() {
if ((mainTimer.getElapsedTime() > 10000) &&(leftMotionTimer.getElapsedTime() != 0) &&(rightMotionTimer.getElapsedTime() != 0)) {
//println(leftMotionTimer.getElapsedTime() + " - " + rightMotionTimer.getElapsedTime());
if (leftMotionTimer.getElapsedTime() < rightMotionTimer.getElapsedTime()) { //play the side whose sensor went off last. or shortest time.
//play leftwing clip
if (!isPlaying("left")) {
// println("left");
play("left");
}
}
else {
// play rightwing clip

if (!isPlaying("right")) {
play("right");
// println("right");
}
}
mainTimer.startTimer();

}

}

public void stop(){
Sonia.stop();
super.stop();
}

void serialEvent() {
println(serial);
data[index] = serial;
index++;

if (index==2) {

if (data[0] == 1) {
leftMotionTimer.stopTimer();
leftMotionTimer.startTimer();

}

if (data[1] == 1) {
rightMotionTimer.stopTimer();
rightMotionTimer.startTimer();
}
println(data[0] + "," + data[1]);
serialWrite(65); //talk to pic
index=0;
}
}

void play(String side) {
stop("left");
stop("right");
pSide = side;
if (side == "left") {
playing = int(random(leftwing.length)); // random number
leftwing[playing].play(); // play left wing
clipTimer.startTimer();
}
if (side == "right") {
playing = int(random(rightwing.length)); // random number
rightwing[playing].play(); // play left wing
clipTimer.startTimer();

}

}

void stop(String side) {

if (side == "left") {
for (int i=0;i<leftwing.length;i++) {
if (leftwing[i].isPlaying()) { //stop all the leftwing media
leftwing[i].stop();
}
}
}

if (side == "right") {
for (int i=0;i<rightwing.length;i++) {
if (rightwing[i].isPlaying()) { //stop all the leftwing media
rightwing[i].stop();
}
}
}
}

boolean isPlaying(String side) {
boolean playValue;
playValue = false;

if (side == "left") {
for (int i=0;i<leftwing.length;i++) {
if (leftwing[i].isPlaying()) { //stop all the leftwing media
playValue = true;
}
}
}

if (side == "right") {
for (int i=0;i<rightwing.length;i++) {
if (rightwing[i].isPlaying()) { //stop all the rightwing media
playValue = true;
}
}

}

return playValue;
}

//this is the blueprint for the timer object
class Timer {
int lastTime;

Timer(){
lastTime = 0;
}

int getElapsedTime(){
int now = millis();
return now-lastTime;
}

void startTimer(){
lastTime = millis();
}
void stopTimer() {
lastTime = 0;
}
}