/ Published in: Processing
Using minim to trigger MP3 sounds via flex and force sensors connected to an arduino.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import processing.serial.*; import cc.arduino.*; Arduino arduino; import ddf.minim.*; Minim minim; AudioSnippet sound1; AudioSnippet sound2; AudioSnippet sound3; AudioSnippet sound4; AudioSnippet sound5; color off = color(4, 79, 111); color on = color(84, 145, 158); //force sensor int force = 0; //flex sensor 1 int flex1 = 1; //forec switch case int forceCase = 0; void setup() { size(470, 280); arduino = new Arduino(this, Arduino.list()[0], 115200); //start minim minim = new Minim(this); //load sounds sound1 = minim.loadSnippet("sound1.mp3"); sound2 = minim.loadSnippet("sound2.mp3"); sound3 = minim.loadSnippet("sound3.mp3"); sound4 = minim.loadSnippet("sound4.mp3"); sound5 = minim.loadSnippet("sound5.mp3"); } void draw() { background(off); stroke(on); force = arduino.analogRead(0); flex1 = arduino.analogRead(1); //println(force); println(flex1); playSound(); } void playSound(){ if (force > 1000){ sound1.unmute(); sound1.play(); } else { sound1.rewind(); sound1.mute(); } if (force > 800 && force < 1000){ sound2.unmute(); sound2.play(); } else { sound2.rewind(); sound2.mute(); } if (flex1 > 1 && flex1 < 400){ sound3.unmute(); sound3.play(); } else { sound3.rewind(); sound3.mute(); } if (flex1 > 400 && flex1 < 600){ sound4.unmute(); sound4.play(); } else { sound4.rewind(); sound4.mute(); } } void stop() { // always close Minim audio classes when you are done with them sound1.close(); sound2.close(); sound3.close(); sound4.close(); sound5.close(); minim.stop(); super.stop(); }