I'm working on a project for a unit that will recognize midi notes from Ableton live and then either light an LED or display text on an LCD screen. I've built a ardweeny and have it receiving MIDI through a 6N138. Everything seems to be working except my code.
It will only recognize 2 notes (36, and 38) which should be C2 and D2, but what the ardweeny is recognizing them as is when I play an F#2 and G2. I don't get it. Also, the light and text doesn't stay on while the note is held, it just stays on. But at times when I play the note softer it goes off and clears. I'm trying to figure out if this a code problem or something with the way Ableton is putting out the midi info. Here's my code any help is appreciated.
#include <MIDI.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define LED1 9 // LED1 is pin 9
#define LED2 10 // LED2 is pin 10
void HandleNoteOn(byte channel, byte note, byte velocity)
{
if (velocity > 0) { // So midi devices that send note-on at zero
// velocity for note-off don't double trigger.
if (note == 36) {
digitalWrite(LED1, HIGH);
}
if (note == 38) {
digitalWrite(LED2, HIGH);
}
if (note == 38) {
lcd.print("1");
}
if (note == 40) {
lcd.print("2");
}
if (note == 42) {
lcd.print("3");
}
if (note == 44) {
lcd.print("4");
}
}
}
void HandleNoteOff(byte channel, byte note, byte velocity)
{
if (velocity == 0 ){ // So midi devices that send note-on at zero
// velocity for note-off don't double trigger.
if (note == 36) {
digitalWrite(LED1, LOW);
lcd.clear();
} //
if (note == 38) { //
digitalWrite(LED2, LOW);
lcd.clear(); //
}
if (note == 40) {
lcd.clear();
}
if (note == 39) {
lcd.clear();
}
if (note == 40) {
lcd.clear();
}
if (note == 41) {
lcd.clear();
}
}
}
void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
MIDI.begin(1); // Launch MIDI and listen to all channels
MIDI.setHandleNoteOn(HandleNoteOn); // Initiates HandleNoteOn function
MIDI.setHandleNoteOff(HandleNoteOff); //Initiates HandleNoteOff function
}
void loop()
{
MIDI.read(); // Tells Arduino to start listening for
} // midi info on the serial port