fadeenk

LED Piano

Oct 17th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.51 KB | None | 0 0
  1. // libraries
  2. import cc.arduino.*;
  3. import processing.serial.*;
  4. import oscP5.*;
  5. import netP5.*;
  6. import ddf.minim.*;
  7. import ddf.minim.signals.*;
  8.  
  9. //declare library entities
  10. Minim minim;
  11. AudioOutput out;
  12. SineWave sine;
  13. OscP5 oscP5;
  14. Arduino arduino;
  15.  
  16. //declare pins & global var
  17. int knob = 0;
  18. int pin[] = {12, 11, 10, 9, 8, 7, 6, 5, 4};
  19.  
  20. float a,b,c,d,e,f,g,h,i;
  21. float tone[] = {200, 1000, 300, 1200, 500, 1500, 700, 1900, 900};
  22. boolean pressed[] = {false,false,false,false,false,false,false,false,false};
  23.  
  24. void setup() {
  25.   size(480,420);
  26.   background(0);
  27.  
  28.   //set up library entities
  29.   arduino = new Arduino(this, "COM5", 57600);
  30.   oscP5 = new OscP5(this,8000);
  31.   minim = new Minim(this);
  32.   out = minim.getLineOut();
  33.   sine = new SineWave(0, 0, out.sampleRate());    
  34.   out.addSignal(sine);
  35.  
  36.   //declare pin types
  37.   for(int i =0; i<9;i++){
  38.     arduino.pinMode(pin[i], arduino.OUTPUT);
  39.   }
  40.   arduino.pinMode(knob, Arduino.INPUT);
  41. }
  42.  
  43. void draw() {
  44.   noStroke();
  45.   float values[] = {a,b,c,d,e,f,g,h,i};
  46.  
  47.   int pos = 0;
  48.   for(int i = 0; i < 9; i = i+2){
  49.     fill(255);
  50.     rect((95 * pos) + 5, 5, 90, 290);
  51.     if(values[i] == 0.0f){
  52.       fill(255);
  53.       ellipse((95* pos) + 50, 260, 50, 50);
  54.       arduino.digitalWrite(pin[i], arduino.LOW);
  55.       if (pressed[i] == true)
  56.       {
  57.         soundOff(i);
  58.       }
  59.     }
  60.     else{
  61.       fill(0,255,0);
  62.       ellipse((95* pos) + 50, 260, 50, 50);
  63.       arduino.digitalWrite(pin[i], arduino.HIGH);
  64.       if (pressed[i] == false)
  65.       {
  66.         soundOn(i);
  67.       }
  68.     }
  69.     pos++;
  70.   }
  71.   pos = 0;
  72.   for(int i = 1; i <9; i = i+2){
  73.     fill(50);
  74.     rect((95 * pos) + 85, 5, 25, 200);
  75.     if(values[i] == 0.0f){
  76.       ellipse((95 * pos) + 97, 190, 20, 20);
  77.       arduino.digitalWrite(pin[i], arduino.LOW);
  78.       if (pressed[i] == true)
  79.       {
  80.         soundOff(i);
  81.       }
  82.     }
  83.     else{
  84.       fill(0,255,0);
  85.       ellipse((95 * pos) + 97, 190, 20, 20);
  86.       arduino.digitalWrite(pin[i], arduino.HIGH);
  87.       if (pressed[i] == false)
  88.       {
  89.         soundOn(i);
  90.       }
  91.     }
  92.     pos++;
  93.   }
  94.   readSound(); // read the amplitude value from knob
  95.   drawSound(); // draw the sound wave
  96. }
  97.  
  98. void soundOff(int x){
  99.  float freq = sine.frequency() - tone[x];
  100.  sine.setFreq(freq);
  101.  pressed[x] = false;
  102.  delay(50);
  103. }
  104.  
  105. void soundOn(int x){
  106.   float freq = sine.frequency() + tone[x];
  107.   sine.setFreq(freq);
  108.   pressed[x] = true;
  109.   delay(50);
  110. }
  111.  
  112. void readSound(){
  113.   float val = arduino.analogRead(knob);
  114.   float amp = map(val, 0, 1023, 0, 1);
  115.   print("The function's amplitude: ");
  116.   println(amp);
  117.   println("");
  118.   println("");
  119.   sine.setAmp(amp);
  120. }
  121.  
  122. void drawSound(){
  123.   fill(255,125,0);
  124.   rect(5,305,470,110);
  125.   stroke(0);
  126.   for(int i = 0; i < out.bufferSize() - 1; i++)
  127.   {
  128.     float x1 = map(i, 0, out.bufferSize(), 0, width-5);
  129.     float x2 = map(i+1, 0, out.bufferSize(), 0, width-5);
  130.     line(x1, 360 + out.left.get(i)*50, x2, 360 + out.left.get(i+1)*50);
  131.   }
  132. }
  133.  
  134. void stop()
  135. {
  136.   out.close();
  137.   minim.stop();
  138.   super.stop();
  139. }
  140.  
  141. void oscEvent(OscMessage theOscMessage){
  142.   String addr = theOscMessage.addrPattern();
  143.   float val = theOscMessage.get(0).floatValue();
  144.  
  145.   if(addr.equals("/1/1")){ a = val;}
  146.   if(addr.equals("/1/2")){ b = val;}
  147.   if(addr.equals("/1/3")){ c = val;}
  148.   if(addr.equals("/1/4")){ d = val;}
  149.   if(addr.equals("/1/5")){ e = val;}
  150.   if(addr.equals("/1/6")){ f = val;}
  151.   if(addr.equals("/1/7")){ g = val;}
  152.   if(addr.equals("/1/8")){ h = val;}
  153.   if(addr.equals("/1/9")){ i = val;}
  154. }
Advertisement
Add Comment
Please, Sign In to add comment