I am stuck with my project.
I am using a OnePinCapSense library and I need to put out MIDI notes.
While OnePinCapSense works fine with Arduino 1.8.2 MIDI librarie acts weird.
When I use example given with the MIDI library it compiles OK. When I try to use MIDI.sendNoteOn(42, 127, 1); in my scetch I get the error "MIDI is not declared...."
You don't need a MIDI library to send MIDI, just one function is enough:
/* The format of the message to send via serial. Create a new data structure, that can store 3 bytes at once. This will be easier to send as MIDI. */
typedef struct {
unsigned int channel : 4; // second nibble : midi channel (0-15) (channel and status are swapped, because Arduino is Little Endian)
unsigned int status : 4; // first nibble : status message (NOTE_ON, NOTE_OFF or CC (control change)
uint8_t data1; // second byte : first value (0-127), controller number or note number
uint8_t data2; // third byte : second value (0-127), controller value or velocity
}
t_midiMsg; // We call this structure 't_midiMsg'
void MIDISend(uint8_t status, uint8_t channel, uint8_t data1, uint8_t data2) {
t_midiMsg msg;
msg.status = status & 0xF;
msg.channel = (channel-1) & 0xF; // channels are 0-based
msg.data1 = data1 & 0x7F;
msg.data2 = data2 & 0x7F;
Serial.write((uint8_t *)&msg,3);
}
The MIDI protocol is pretty straightforward, you can find the specs on https://www.midi.org/specifications.
Pieter
P.S. You could also use a shorter function, without a struct, but it's less clear:
void MIDISend(uint8_t status, uint8_t channel, uint8_t data1, uint8_t data2) {
Serial.write(((status & 0xF) << 4) | ((channel - 1) & 0xF));
Serial.write(data1 & 0x7F);
Serial.write(data2 & 0x7F);
}