Skip to content

Instantly share code, notes, and snippets.

@veev
Created November 21, 2016 01:22
Show Gist options
  • Save veev/59761f70ee5addda9ff4687b51b3da51 to your computer and use it in GitHub Desktop.
Save veev/59761f70ee5addda9ff4687b51b3da51 to your computer and use it in GitHub Desktop.
import processing.serial.*;
import processing.sound.*;
SoundFile[] soundFiles = new SoundFile[5];
Serial myPort; // Create object from Serial class
int[] ldrValues;
int[] thresholds = {650, 590, 500, 650, 400};
boolean[] states = {false, false, false, false, false};
void setup() {
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
println(Serial.list());
String portName = Serial.list()[3];
myPort = new Serial(this, portName, 9600);
soundFiles[0] = new SoundFile(this, "Canoe_Oar.mp3");
soundFiles[1] = new SoundFile(this, "Dry_Shampoo.mp3");
soundFiles[2] = new SoundFile(this, "Floaties.mp3");
soundFiles[3] = new SoundFile(this, "Galoshes.mp3");
soundFiles[4] = new SoundFile(this, "Respirator.mp3");
}
void draw()
{
background(255); // Set background to white
//serial loop
while (myPort.available() > 0) {
String myString = myPort.readStringUntil(10);
if (myString != null) {
//println(myString);
ldrValues = int(split(myString.trim(), ','));
//println(ldrValues);
}
}
for (int i = 0; i < ldrValues.length; i++) {
println(states[i]);
println(ldrValues[i]);
if (ldrValues[i] > thresholds[i] && !states[i]) {
println("sensor " + i + " is activated");
soundFiles[i].play();
states[i] = true;
}
if (ldrValues[i] < thresholds[i]) {
println("sensor " + i + " is NOT activated");
soundFiles[i].stop();
states[i] = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment