Arduino
MIDI
NeoPixel

Arduino MIDI in Neopix control.

MIDIシールドを使って、Note onでNeoPixel LED光るおもちゃ作ったよ。
https://youtu.be/dEj8hHYBta4

最終的には、光ナビキーボードにするのが目的。
現在のところ、特定のパターンで点灯しても消えない問題がある。

◆開発環境
[HardWare]
Arduino Uno + SparkFun MIDIシールド

[Software]
Arduino IDE 1.6.10
MIDI.h
Adafruit_NeoPixel.h

[CONNECT to Parts]
2Pin => Beep Piezo Buzzar
3Pin => NeoPixel Data Out

追記

2017-08-18 10:20: Note on Velocity 0 対策追加
MIDI-to-NeoPixel.ino
#include <Adafruit_NeoPixel.h>
#define NUMBER_PIEXEL 40
#define LEDPIN    3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMBER_PIEXEL, LEDPIN, NEO_GRB + NEO_KHZ800);


#include <MIDI.h>
#define Speaker1 2
MIDI_CREATE_DEFAULT_INSTANCE();
int m2t[127] = {
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 0,
 3,
 11,
 19,
 27,
 35,
 43,
 51,
 59,
 67,
 75,
 83,
 91,
 99,
 107,
 115,
 123, //NOTE_B2
 131, //NOTE_C3
 139, //NOTE_CS3
 147, //NOTE_D3
 156, //NOTE_DS3
 165, //NOTE_E3
 175, //NOTE_F3
 185, //NOTE_FS3
 196, //NOTE_G3
 208, //NOTE_GS3
 220, //NOTE_A3
 233, //NOTE_AS3
 247, //NOTE_B3
 262, //NOTE_C4
 277, //NOTE_CS4
 294, //NOTE_D4
 311, //NOTE_DS4
 330, //NOTE_E4
 349, //NOTE_F4
 370, //NOTE_FS4
 392, //NOTE_G4
 415, //NOTE_GS4
 440, //NOTE_A4
 466, //NOTE_AS4
 494, //NOTE_B4
 523, //NOTE_C5
 554, //NOTE_CS5
 587, //NOTE_D5
 622, //NOTE_DS5
 659, //NOTE_E5
 698, //NOTE_F5
 740, //NOTE_FS5
 784, //NOTE_G5
 831, //NOTE_GS5
 880, //NOTE_A5
 932, //NOTE_AS5
 988, //NOTE_B5
 1047, //NOTE_C6
 1109, //NOTE_CS6
 1175, //NOTE_D6
 1245, //NOTE_DS6
 1319, //NOTE_E6
 1397, //NOTE_F6
 1480, //NOTE_FS6
 1568, //NOTE_G6
 1661, //NOTE_GS6
 1760, //NOTE_A6
 1865, //NOTE_AS6
 1976, //NOTE_B6
 2093, //NOTE_C7
 2217, //NOTE_CS7
 2349, //NOTE_D7
 2489, //NOTE_DS7
 2637, //NOTE_E7
 2794, //NOTE_F7
 2960, //NOTE_FS7
 3136, //NOTE_G7
 3322, //NOTE_GS7
 3520, //NOTE_A7
 3729, //NOTE_AS7
 3951, //NOTE_B7
 4186, //NOTE_C8
 4435, //NOTE_CS8
 4699, //NOTE_D8
 4978 //NOTE_DS8
};

int Ch = 1;             //Select watch MIDI CH
//int tone1Interval = 0;  
int note;
int note_sum = 0; 
int offset = 50;        //= note +- LED Pos Offset
int velo;
int randR;
int randG;
int randB;

void setup() {
 randomSeed(1024);
 MIDI.begin(0);
 strip.begin();

 //Check LED Really Bright.
  colorWipe(strip.Color(128, 0, 0), 3); // Red
  colorWipe(strip.Color(0, 127, 0), 3); // Green
  colorWipe(strip.Color(0, 0, 127), 3); // Blue
  colorWipe(strip.Color(0, 0, 0), 3); // Blue
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}


void loop() {
 if (MIDI.read()) {
 MIDI.setInputChannel(Ch); // Channel

 switch (MIDI.getType()) {
 case midi::NoteOn:
 note = MIDI.getData1(); // note no
 velo = MIDI.getData2();    //Velosity?

 if (velo == 0) {
 note_sum = note-offset;
 noTone(Speaker1);
 strip.setPixelColor(note_sum, strip.Color(0,0,0)); // All Off
 strip.show(); // This sends the updated pixel color to the hardware.
 break;
 } else {

// tone1Interval = note;
 note_sum = note-offset;  //Calc note Offset
 tone(Speaker1, m2t[note]);

//Select Single Color or RANDAM color.
  randR= random(255);  randG= random(255); randB= random(255); // Generator 0-255 RANDAM.
  strip.setPixelColor(note_sum, strip.Color(randR,randG,randB)); //RANDOM color.
 //strip.setPixelColor(note_sum, strip.Color(0,30,0)); //Green color.

 strip.show(); // This sends the updated pixel color to the hardware.
 break;
 }

 case midi::NoteOff:
 note = MIDI.getData1(); // note no
// tone1Interval = 0;
 note_sum = note-offset;
 noTone(Speaker1);
 strip.setPixelColor(note_sum, strip.Color(0,0,0)); // All Off
 strip.show(); // This sends the updated pixel color to the hardware.
 break;
 default:
 break;
 }
 }

}