Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <MIDI.h>
- #include "LPD8806.h"
- #include "SPI.h"
- int LED = 13; // on-board LED
- int nLEDs = 32; //LED strip num LEDs
- int dataPin = 2; //LED strip data
- int clockPin = 3;
- LPD8806 strip = LPD8806(nLEDs, dataPin, clockPin);
- void setup() {
- pinMode(LED, OUTPUT);
- MIDI.begin(MIDI_CHANNEL_OMNI); // Launch MIDI with default options
- // Start up the LED strip
- strip.begin();
- // Update the strip, to start they are all 'off'
- strip.show();
- }
- void loop() {
- int i;
- for(i=0; i< strip.numPixels() ; i++) strip.setPixelColor(i, 0); strip.show(); // Refresh LED states
- int type, note, velocity, channel, d1, d2;
- digitalWrite(LED,LOW); // Is there a MIDI message incoming ?
- if(MIDI.read() && MIDI.getType() == NoteOn){ // Is it a Note ON?
- digitalWrite(LED,HIGH); // Blink on-board LED for debugging purposes
- note = MIDI.getData1();
- velocity = MIDI.getData2();
- channel = MIDI.getChannel();
- if (velocity > 1) {
- colorExplode(
- RandomColor(), //color
- 30, //speed of explosion
- note, //note
- 5 //max spread
- );
- }
- }
- }
- void colorExplode(uint32_t c, uint8_t wait,int note,int maxSpread){
- // Start by turning all pixels off:
- int i = 0;
- int start = note - 60;
- for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);
- unsigned long startTime = millis();
- unsigned long currTime;
- // first hold the note for 1000 miliseconds
- do{
- currTime = millis();
- strip.setPixelColor(start, c); // Set new pixel 'on'
- strip.setPixelColor(start, c); // Set new pixel 'on'
- strip.show(); // Refresh LED states
- if(MIDI.read()){
- if(MIDI.getType() == NoteOn ){
- for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);
- strip.show();
- return;
- }
- }
- }while(currTime - startTime < 400);
- //then explode out
- int direction = 1;
- for(i=0;i <= maxSpread; i = i + direction){
- // 1 is "out", -1 is "in";
- uint32_t color;
- if (direction == 1){
- color = c;
- }else{
- color = 0;
- }
- strip.setPixelColor(start + i, color); // Set new pixel 'on'
- strip.setPixelColor(start - i, color); // Set new pixel 'on'
- strip.show(); // Refresh LED states
- delay(wait);
- if( i == maxSpread ){
- direction = direction * -1;
- strip.setPixelColor(start + maxSpread, 0);
- strip.setPixelColor(start - maxSpread, 0);
- }
- if(MIDI.read()){
- if(MIDI.getType() == NoteOn){
- for(i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, 0);
- strip.show();
- return;
- }
- }
- }
- }
- /* Helper functions */
- uint32_t RandomColor(){
- return Wheel( rand() % 384);
- }
- uint32_t Wheel(uint16_t WheelPos)
- {
- byte r, g, b;
- switch(WheelPos / 128)
- {
- case 0:
- r = 127 - WheelPos % 128; //Red down
- g = WheelPos % 128; // Green up
- b = 0; //blue off
- break;
- case 1:
- g = 127 - WheelPos % 128; //green down
- b = WheelPos % 128; //blue up
- r = 0; //red off
- break;
- case 2:
- b = 127 - WheelPos % 128; //blue down
- r = WheelPos % 128; //red up
- g = 0; //green off
- break;
- }
- return(strip.Color(r,g,b));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement