Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 3rd, 2013  |  syntax: C++  |  size: 3.57 KB  |  views: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <MIDI.h>
  2. #include "LPD8806.h"
  3. #include "SPI.h"
  4.  
  5. int LED = 13;           // on-board LED
  6.  
  7. int nLEDs = 32;         //LED strip num LEDs
  8. int dataPin  = 2;       //LED strip data
  9. int clockPin = 3;
  10.  
  11. LPD8806 strip = LPD8806(nLEDs, dataPin, clockPin);
  12.  
  13.  
  14.  
  15.  
  16. void setup() {
  17.   pinMode(LED, OUTPUT);
  18.   MIDI.begin(MIDI_CHANNEL_OMNI);                // Launch MIDI with default options                    
  19.  
  20.   // Start up the LED strip
  21.   strip.begin();
  22.  
  23.   // Update the strip, to start they are all 'off'
  24.   strip.show();
  25.  
  26.  
  27. }
  28.  
  29. void loop() {
  30.   int i;
  31.   for(i=0; i< strip.numPixels() ; i++) strip.setPixelColor(i, 0);  strip.show();              // Refresh LED states
  32.  
  33.  
  34.   int type, note, velocity, channel, d1, d2;
  35.   digitalWrite(LED,LOW);                  // Is there a MIDI message incoming ?
  36.     if(MIDI.read() && MIDI.getType() == NoteOn){       // Is it a Note ON?
  37.         digitalWrite(LED,HIGH);  // Blink on-board LED for debugging purposes
  38.          note = MIDI.getData1();
  39.          velocity = MIDI.getData2();
  40.          channel = MIDI.getChannel();
  41.          if (velocity > 1) {
  42.            colorExplode(
  43.               RandomColor(),   //color
  44.               30,              //speed of explosion
  45.               note,            //note
  46.               5                //max spread
  47.            );  
  48.          }
  49.     }  
  50. }
  51.  
  52.  
  53. void colorExplode(uint32_t c, uint8_t wait,int note,int maxSpread){
  54.   // Start by turning all pixels off:
  55.   int i = 0;
  56.   int start = note - 60;
  57.   for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);
  58.  
  59.   unsigned long startTime = millis();
  60.   unsigned long currTime;
  61.  
  62.   // first hold the note for 1000 miliseconds
  63.   do{
  64.     currTime = millis();
  65.     strip.setPixelColor(start, c); // Set new pixel 'on'
  66.     strip.setPixelColor(start, c); // Set new pixel 'on'
  67.     strip.show();              // Refresh LED states  
  68.     if(MIDI.read()){
  69.       if(MIDI.getType() == NoteOn ){
  70.         for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);
  71.         strip.show();
  72.         return;
  73.       }
  74.     }
  75.   }while(currTime - startTime < 400);
  76.  
  77.   //then explode out
  78.   int direction = 1;
  79.   for(i=0;i <= maxSpread; i = i + direction){
  80.    
  81.     // 1 is "out", -1 is "in";
  82.     uint32_t color;
  83.     if (direction == 1){
  84.         color = c;
  85.     }else{
  86.         color = 0;
  87.     }
  88.     strip.setPixelColor(start + i, color); // Set new pixel 'on'
  89.     strip.setPixelColor(start - i, color); // Set new pixel 'on'
  90.     strip.show();              // Refresh LED states
  91.     delay(wait);
  92.    
  93.     if( i == maxSpread  ){
  94.        direction = direction * -1;
  95.        strip.setPixelColor(start + maxSpread, 0);
  96.        strip.setPixelColor(start - maxSpread, 0);
  97.     }
  98.    
  99.     if(MIDI.read()){
  100.       if(MIDI.getType() == NoteOn){
  101.         for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);
  102.         strip.show();
  103.         return;
  104.       }
  105.     }
  106.    
  107.    
  108.   }
  109.  
  110.  
  111. }
  112.  
  113. /* Helper functions */
  114.  
  115. uint32_t RandomColor(){
  116.    
  117.  return Wheel( rand() % 384);
  118.  
  119. }
  120.  
  121.  
  122. uint32_t Wheel(uint16_t WheelPos)
  123. {
  124.   byte r, g, b;
  125.   switch(WheelPos / 128)
  126.   {
  127.     case 0:
  128.       r = 127 - WheelPos % 128;   //Red down
  129.       g = WheelPos % 128;      // Green up
  130.       b = 0;                  //blue off
  131.       break;
  132.     case 1:
  133.       g = 127 - WheelPos % 128;  //green down
  134.       b = WheelPos % 128;      //blue up
  135.       r = 0;                  //red off
  136.       break;
  137.     case 2:
  138.       b = 127 - WheelPos % 128;  //blue down
  139.       r = WheelPos % 128;      //red up
  140.       g = 0;                  //green off
  141.       break;
  142.   }
  143.   return(strip.Color(r,g,b));
  144. }