I'm trying to convert my midi key numbers to midi format, as hairless midi cannot understand just the numbers alone. Almost all the code in my snippet is taken from else where, but its the only code I can get to read my midi and output as a genuin number.
Can I please get some help just to convert Serial.println(midiNote); to a format hairless midi will understand. Spent hours debugging this already
//MIDI Note receive with clock
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX
// constants
#define PIN_LED 13
// MIDI commands
#define MIDI_NOTE_ON 0x90
#define MIDI_NOTE_OFF 0x80
#define MIDI_START 0xFA
#define MIDI_STOP 0xFC
#define MIDI_CONTINUE 0xFB
#define MIDI_CLOCK 0xF8
//Clock
int play_flag = 0;
// filter MIDI events on channel and note number
const int filterChannel = 0; // MIDI channel 1
// states
#define STATE_NONE 0
#define STATE_NOTE_ON 1
#define STATE_NOTE 2
int state;
// keep action (LED) going for actionDuration milliseconds
bool actionOn;
unsigned long actionTime;
unsigned long actionDuration = 100;
// received MIDI data
byte midiByte;
byte midiChannel;
byte midiCommand;
byte midiNote;
byte midiVelocity;
//Counter
int counter_a = 0;
int counter_b = 0;
long int bpmcount_a;
int bpm;
int bpmprevious = 0;
void setup() {
mySerial.begin(31250);
Serial.begin(112500);
delay(100);
pinMode(PIN_LED, OUTPUT);
state = STATE_NONE;
actionOn = false;
pinMode(13,OUTPUT);
pinMode(5,OUTPUT);
resetcounters();
}
void loop () {
// Is there any MIDI waiting to be read?
if (mySerial.available() > 0) {
// read MIDI byte
midiByte = mySerial.read();
switch (state) {
case STATE_NONE:
// remove channel info
midiChannel = midiByte & B00001111;
midiCommand = midiByte & B11110000;
if (midiChannel == filterChannel)
{
if (midiCommand == MIDI_NOTE_ON){state = STATE_NOTE_ON;}
}
if (midiByte == MIDI_START){play_flag = 1; resetcounters(); Serial.println("Start");}
if (midiByte == MIDI_CONTINUE){play_flag = 1; resetcounters();}
if (midiByte == MIDI_STOP){play_flag = 0; resetcounters(); Serial.println("Stop");}
if((midiByte == MIDI_CLOCK) && (play_flag == 1)) {Sync();}
break;
case STATE_NOTE_ON:
midiNote = midiByte;
state = STATE_NOTE;
break;
case STATE_NOTE:
midiVelocity = midiByte;
state = STATE_NONE;
if ( midiVelocity > 0)
{
// digitalWrite(PIN_LED, HIGH);
actionTime = millis();
actionOn = true;
Serial.println(midiNote);
}
break;
} // switch
} // mySerial.available()
if (actionOn)
{
if ((millis() - actionTime) > actionDuration)
{
actionOn = false;
digitalWrite(PIN_LED, LOW);
}
}
} // loop
void Sync() {
// do something for every MIDI Clock pulse when the sequencer is running
if(counter_a == 6){counter_a = 0;}
if(counter_b == 24){ counter_b = 0; calcbpm(bpmcount_a,micros());}
if(counter_a == 0){digitalWrite(13,HIGH);}
if(counter_b == 0){digitalWrite(5,HIGH); bpmcount_a = micros();}
if(counter_a == 2){digitalWrite(13,LOW);}
if(counter_b == 11){digitalWrite(5,LOW);}
counter_a ++;
counter_b ++;
}
void calcbpm(long int bpmcount, long int microelapsed){
bpm = 60000 / ((microelapsed - bpmcount) /1000);
if (bpm != bpmprevious){Serial.print("BPM"); Serial.print(bpm); Serial.println("");}
bpmprevious = bpm;
}
//Functions to avoid repeted code.
void resetcounters(){
counter_a = 0;
counter_b = 0;
digitalWrite(13,LOW);
digitalWrite(5,LOW);
}