Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MIDI reader
#include <MIDI.h>
#define NOTES 26
#define OCTAVE_LENGTH 12
#define BASELINE 43
#define CHANNELS 3
int latchPin1 = 8;
int clockPin1 = 12;
int dataPin1 = 11;
int latchPin2 = 9;
int clockPin2 = 6;
int dataPin2 = 7;
int g3 = 10;
int gSharp3 = 5;
byte notes[CHANNELS][NOTES];
byte dataArrayRED[8];
byte dataArrayGREEN[8];
byte dataArrayYELLOW[8];
MIDI_CREATE_DEFAULT_INSTANCE();
void handleNoteOn(byte channel, byte pitch, byte velocity) {
// ignore anything outside our range
if (channel > 3 || (channel == 2 && pitch - OCTAVE_LENGTH - BASELINE >= NOTES) || (channel != 2 && pitch - BASELINE >= NOTES)) {
return;
}
// set a new note on (channel 2 gets down an octave)
if (channel == 2) {
pitch -= OCTAVE_LENGTH;
}
notes[channel - 1][pitch - BASELINE] = 1;
refreshLeds();
}
void handleNoteOff(byte channel, byte pitch, byte velocity) {
// ignore anything outside our range
if (channel > 3 || (channel == 2 && pitch - OCTAVE_LENGTH - BASELINE >= NOTES) || (channel != 2 && pitch - BASELINE >= NOTES)) {
return;
}
// set a new note off (channel 2 gets down an octave)
if (channel == 2) {
pitch -= OCTAVE_LENGTH;
}
notes[channel - 1][pitch - BASELINE] = 0;
refreshLeds();
}
void setup() {
int i, j;
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
pinMode(latchPin1, OUTPUT);
pinMode(latchPin2, OUTPUT);
pinMode(g3, OUTPUT);
pinMode(gSharp3, OUTPUT);
for (j = 0; j < CHANNELS; j++) {
for (i = 0; i < NOTES; i++) {
notes[j][i] = 0;
}
}
dataArrayRED[0] = 0x01; //00000001
dataArrayRED[1] = 0x02; //00000010
dataArrayRED[2] = 0x04; //00000100
dataArrayRED[3] = 0x08; //00001000
dataArrayRED[4] = 0x10; //00010000
dataArrayRED[5] = 0x20; //00100000
dataArrayRED[6] = 0x40; //01000000
dataArrayRED[7] = 0x80; //10000000
dataArrayGREEN[0] = 0x01; //00000001
dataArrayGREEN[1] = 0x02; //00000010
dataArrayGREEN[2] = 0x04; //00000100
dataArrayGREEN[3] = 0x08; //00001000
dataArrayGREEN[4] = 0x10; //00010000
dataArrayGREEN[5] = 0x20; //00100000
dataArrayGREEN[6] = 0x40; //01000000
dataArrayGREEN[7] = 0x80; //10000000
dataArrayYELLOW[0] = 0x01; //00000001
dataArrayYELLOW[1] = 0x02; //00000010
dataArrayYELLOW[2] = 0x04; //00000100
dataArrayYELLOW[3] = 0x08; //00001000
dataArrayYELLOW[4] = 0x10; //00010000
dataArrayYELLOW[5] = 0x20; //00100000
dataArrayYELLOW[6] = 0x40; //01000000
dataArrayYELLOW[7] = 0x80; //10000000
digitalWrite(latchPin1, 0);
shiftOut(dataPin1, clockPin1, 0x00);
shiftOut(dataPin1, clockPin1, 0x00);
digitalWrite(latchPin1, 1);
digitalWrite(latchPin2, 0);
shiftOut(dataPin2, clockPin2, 0x00);
digitalWrite(latchPin2, 1);
digitalWrite(g3, 0);
digitalWrite(gSharp3, 0);
}
void loop() {
MIDI.read();
}
void refreshLeds() {
int i, j;
byte dataRED = 0x00;
byte dataGREEN = 0x00;
byte dataYELLOW = 0x00;
byte g3on = 0;
byte gSharp3on = 0;
for (j = 0; j < CHANNELS; j++) {
for (i = 0; i < NOTES; i++) {
if (i < 8) {
if (notes[j][i]) {
dataRED |= dataArrayRED[i];
}
}
else if (i < 16) {
if (notes[j][i]) {
dataGREEN |= dataArrayGREEN[i % 8];
}
}
else if (i < 24) {
if (notes[j][i]) {
dataYELLOW |= dataArrayYELLOW[i % 8];
}
}
else if (i == 24) {
if (notes[j][i]) {
g3on = 1;
}
}
else { // i == 25
if (notes[j][i]) {
gSharp3on = 1;
}
}
}
}
digitalWrite(latchPin1, 0);
shiftOut(dataPin1, clockPin1, dataGREEN);
shiftOut(dataPin1, clockPin1, dataRED);
digitalWrite(latchPin1, 1);
digitalWrite(latchPin2, 0);
shiftOut(dataPin2, clockPin2, dataYELLOW);
digitalWrite(latchPin2, 1);
digitalWrite(g3, g3on);
digitalWrite(gSharp3, gSharp3on);
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
int i = 0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
for (i = 7; i >= 0; i--) {
digitalWrite(myClockPin, 0);
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
digitalWrite(myDataPin, pinState);
digitalWrite(myClockPin, 1);
digitalWrite(myDataPin, 0);
}
digitalWrite(myClockPin, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.