Flex and force sensor to trigger mp3\'s


/ Published in: Processing
Save to your folder(s)

Using minim to trigger MP3 sounds via flex and force sensors connected to an arduino.


Copy this code and paste it in your HTML
  1. import processing.serial.*;
  2.  
  3. import cc.arduino.*;
  4.  
  5. Arduino arduino;
  6.  
  7. import ddf.minim.*;
  8.  
  9. Minim minim;
  10. AudioSnippet sound1;
  11. AudioSnippet sound2;
  12. AudioSnippet sound3;
  13. AudioSnippet sound4;
  14. AudioSnippet sound5;
  15.  
  16. color off = color(4, 79, 111);
  17. color on = color(84, 145, 158);
  18.  
  19. //force sensor
  20. int force = 0;
  21.  
  22. //flex sensor 1
  23. int flex1 = 1;
  24.  
  25. //forec switch case
  26. int forceCase = 0;
  27.  
  28. void setup() {
  29. size(470, 280);
  30. arduino = new Arduino(this, Arduino.list()[0], 115200);
  31.  
  32. //start minim
  33. minim = new Minim(this);
  34.  
  35. //load sounds
  36. sound1 = minim.loadSnippet("sound1.mp3");
  37. sound2 = minim.loadSnippet("sound2.mp3");
  38. sound3 = minim.loadSnippet("sound3.mp3");
  39. sound4 = minim.loadSnippet("sound4.mp3");
  40. sound5 = minim.loadSnippet("sound5.mp3");
  41.  
  42.  
  43. }
  44.  
  45. void draw() {
  46.  
  47. background(off);
  48. stroke(on);
  49.  
  50. force = arduino.analogRead(0);
  51. flex1 = arduino.analogRead(1);
  52.  
  53. //println(force);
  54. println(flex1);
  55.  
  56. playSound();
  57.  
  58. }
  59.  
  60. void playSound(){
  61.  
  62. if (force > 1000){
  63. sound1.unmute();
  64. sound1.play();
  65. } else {
  66. sound1.rewind();
  67. sound1.mute();
  68. }
  69.  
  70. if (force > 800 && force < 1000){
  71. sound2.unmute();
  72. sound2.play();
  73. } else {
  74. sound2.rewind();
  75. sound2.mute();
  76. }
  77.  
  78. if (flex1 > 1 && flex1 < 400){
  79. sound3.unmute();
  80. sound3.play();
  81. } else {
  82. sound3.rewind();
  83. sound3.mute();
  84. }
  85.  
  86. if (flex1 > 400 && flex1 < 600){
  87. sound4.unmute();
  88. sound4.play();
  89. } else {
  90. sound4.rewind();
  91. sound4.mute();
  92. }
  93.  
  94. }
  95.  
  96. void stop()
  97. {
  98. // always close Minim audio classes when you are done with them
  99. sound1.close();
  100. sound2.close();
  101. sound3.close();
  102. sound4.close();
  103. sound5.close();
  104. minim.stop();
  105.  
  106. super.stop();
  107.  
  108. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.