markbowenpiano:
The thing that I don't get is that yes you're right that it's (in the example above) spitting out 226 however it only does that if I attempt to use that startLED variable that I've created which should really only ever at the most be 174 (top note played on the piano is 87 and then times that by 2).
Maybe I'm missing something but when your code prints 226 it is printing the is the value from the event message itself (well, minus 21):
int ledToLight = rx.byte2 - 21; // Gives a number from 0-88
Serial.println(ledToLight);
Serial.println("---------------------------");
This is why I was saying you need to verify the event type and the bounds on rx.byte2. Don't just assume they're as you expect.
I will however try out your code above tomorrow and thank you very much for that.
I've updated it to check for both noteOff events (0x8) and for noteOn (0x9) with a velocity of 0x0:
/*
MIDIUSB_test.ino
Created: 4/6/2015 10:47:08 AM
Author: gurbrinder grewal
Modified by Arduino LLC (2015)
*/
// a lookup table to convert pitch to text
const char pitchLUT[][4] =
{
" A0", "B0b", " B0", " C1", "D1b", " D1", "E1b", " E1", " F1", "G1b",
" G1", "A1b", " A1", "B1b", " B1", " C2", "D2b", " D2", "E2b", " E2",
" F2", "G2b", " G2", "A2b", " A2", "B2b", " B2", " C3", "D3b", " D3",
"E3b", " E3", " F3", "G3b", " G3", "A3b", " A3", "B3b", " B3", " C4",
"D4b", " D4", " E4b", " E4", " F4", "G4b", " G4", "A4b", "A4", "B4b",
" B4", " C5", "D5b", " D5", "E5b", " E5", " F5", "G5b", " G5", "A5b",
" A5", "B5b", " B5", " C6", "D6b", " D6", "E6b", " E6", " F6", "G6b",
" G6", "A6b", " A6", "B6b", " B6", " C7", "D7b", " D7", "E7b", " E7",
" F7", "G7b", " G7", "A7b", " A7", "B7b", " B7", " C8",
};
#include "MIDIUSB.h"
#include "FastLED.h"
#define DEBUG 1 //comment out to stop verbose serial messages
#define PIN 3 // Output Pin to Data Line on Strip
#define COLOR_ORDER GRB // I had to change this for my strip if your color is off then you know.
#define NUM_LEDS 176 // # of LEDS in the strip
#define BRIGHTNESS 10
CRGB leds[NUM_LEDS];
int fadeAmount = 5; // Set the amount to fade I usually do 5, 10, 15, 20, 25 etc even up to 255.
uint8_t
evtType,
evtChan,
evtPitch,
evtVel,
pitch;
char
szStr[40];
void setup()
{
// sanity check delay - allows reprogramming if accidently blowing power w/leds
delay(2000);
FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS); // GRB ordering is typical
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
Serial.begin(115200);
}
void loop()
{
midiEventPacket_t rx = MidiUSB.read();
if( rx.header != 0 )
{
do
{
#ifdef DEBUG
sprintf( szStr, "Packet: %02X - %02X - %02X - %02X",
rx.header,
rx.byte1,
rx.byte2,
rx.byte3 );
Serial.println( szStr );
#endif
evtType = (rx.byte1 >> 4) & 0xf;
evtChan = rx.byte1 & 0xf;
evtPitch = rx.byte2;
evtVel = rx.byte3;
switch( evtType )
{
case 0x8:
case 0x9:
if( pitch >= 21 && pitch <= 108 )
{
pitch -= 21;
if( evtType == 0x8 || (evtType == 0x9 && evtVel == 0) )
{
leds[pitch << 1] = CRGB::Black;
leds[(pitch << 1)+1] = CRGB::Black;
}
else
{
leds[pitch << 1] = CRGB::Red;
leds[(pitch << 1)+1] = CRGB::Red;
}//else
}//if
else
{
//illegal note
Serial.println( "Unknown note" );
}//else
break;
default:
break;
}//switch
FastLED.show();
#ifdef DEBUG
sprintf( szStr, "Type: %1X Chan: %d Note: %s Velocity: %d",
evtType,
evtChan,
pitchLUT[pitch],
evtVel );
Serial.print( szStr );
#endif
rx = MidiUSB.read();
}while(rx.header != 0);
}//if
}//loop