Hello, I have written a function to send MIDI messages, that automatically uses the right settings for the right board (Uno or Leonardo). It looks something like this:
#define BLINK // Comment this line out to stop the LED from blinking whenever a MIDI message is sent.
#define LED_PIN 13
//#define DEBUG // Uncomment this line to use debug mode. MIDI messages will be sent as HEX over serial @9600 baud.
#define NOTE_OFF 0x80
#define NOTE_ON 0x90
#define CC 0xB0
void setupMidi(){
#if defined (__AVR_ATmega328P__) //only include these lines when compiling for Arduino Uno
Serial.begin(115200); // Start communication with ATmega16U2 @115200 baud (for MIDI firmware)
#endif
#if defined DEBUG //only include these lines when in debug mode
Serial.begin(115200); // Start communication with ATmega16U2 @9600 baud (for Arduino Serial firmware)
#endif
#ifdef BLINK
pinMode(LED_PIN,OUTPUT); // The pin with the LED connected is set to output.
#endif
}
void sendMidi(byte m, byte c, byte d1, byte d2) // Custom function to send MIDI messages: message, channel, data1, data2
{
#ifdef BLINK
digitalWrite(LED_PIN,1);
#endif
#ifdef DEBUG //When in debug mode, send every part of the message seperately, in HEX
Serial.print(m, HEX);
Serial.print('\t');
// Etc. etc. to print out all variables in the serial monitor
#else
#if defined (__AVR_ATmega32U4__) //only include these lines when compiling for Arduino Leonardo
if(m == NOTE_ON){
usbMIDI.sendNoteOn(d1, d2, c);
}
// Etc. etc. use the Teensy usbMIDI library to send the right MIDI message.
#endif
#if defined (__AVR_ATmega328P__) //only include these lines when compiling for Arduino Uno
Serial.write( // Write the message over serial, to the ATmega16U2, running custom MIDI <-> USB firmware
#endif
#endif
#ifdef BLINK
digitalWrite(LED_PIN,0);
#endif
}
I put this in a separate file, named it midiSend.cpp, added:
#include "Arduino.h"
#include "midiSend.h"
and I created the midiSend.h file with just the prototypes:
#include "Arduino.h"
void sendMidi(byte m, byte c, byte d1, byte d2);
void setupMidi();
Then I added the line #include "midiSend.h" to a library I wrote myself, that needs MIDI output.
It compiles for both the Uno and the Leonardo, the debug mode works too, but ...
I was wondering if there is a way to move the #define DEBUG line in the sendMIDI.cpp file to my main .ino file, so I can choose there whether or not I want to compile in debug mode. Now I have to change my .cpp file, every time I want to debug.
The problem is that I have to do this with preprocessor directives, since the compiler refuses to continue if it sees a MIDI-mode specific function when the 'board' is set to 'Leonardo'.
Next, how can I use predefined constants, like "INPUT_PULLUP" for example, in my own libraries and functions? (The ones that show up in blue.)
If I have a function 'run(int speed)' for example, and I want to be able to use both 'run(FAST_AS_POSSIBLE)' and 'run(5)'.
Thanks in advance!
tttapa