I'd like to write a library which uses the MIDI.h library within it. The idea is to take a reading from a potentiometer and pass it through a noise filter and then finally send it via MIDI control Change. I got this working when i was just sending Serial.print but as soon as I changed the send to MIDI CC it started throwing up errors. Any help would be appreciated. thanks
Here is what I've tried so far: -
The MIDI_CREATE_DEFAULT_INSTANCE(); line is required in the main sketch to make the MIDI library work so I tried adding it in the cpp file as well but it didn't help. therefore i've commented it out.
ALso... the MIDI.h and MIDI.cpp files are in a folder called 'MIDI_Library-5.0.2' which is located inside \Documents\Arduino\libraries
And the stablePot.h and stablePot.cpp are inside a folder named 'stablePot' which is in the same location.
You should add another parameter to your stablePot constructor, that is a reference to the MidiInterface instance, then use this reference to call its methods. At least that's how I would do it, even if there is only one instance of MidiInterface in the program.
Example (untested)
#ifndef stablePot_h
#define stablePot_h
#include "MIDI.h"
class stablePot
{
public:
stablePot(MidiInterface & midi, int pin);
void midiSendCC();
private:
int _pin;
int _val;
int _lastVal;
MidiInterface * _midiInterface;
};
#endif
GUIX: -
sorry I should probably have mentioned...
I am using Hiduino so there isn't a MIDI interface. USB straight to the computer. But the MIDI.sendControlChange function usually sends it fine. (from a normal sketch)
GFVALVO: -
here is the error message: -
Arduino: 1.8.13 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
C:\Users\Liam\Documents\Arduino\libraries\stablePot\stablePot.cpp: In member function 'void stablePot::midiSendCC()':
C:\Users\Liam\Documents\Arduino\libraries\stablePot\stablePot.cpp:18:5: error: 'MIDI' was not declared in this scope
MIDI.sendControlChange(0, 0, 1);
^~~~
C:\Users\Liam\Documents\Arduino\libraries\stablePot\stablePot.cpp:18:5: note: suggested alternative: 'MISO'
MIDI.sendControlChange(0, 0, 1);
^~~~
MISO
exit status 1
Error compiling for board Arduino Mega or Mega 2560.
This report would have more information with"Show verbose output during compilation"option enabled in File -> Preferences.
#ifndef stablePot_h
#define stablePot_h
class stablePot
{
public:
stablePot(int pin);
void midiSendCC();
private:
MIDI_CREATE_DEFAULT_INSTANCE();
int _pin;
int _val;
int _lastVal;
};
#endif
the other two files remain the same. and now im getting the following error...
Arduino: 1.8.13 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
In file included from C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/MIDI.h:35:0,
from C:\Users\Liam\Documents\Arduino\libraryTest\_midiTest\_midiTest.ino:1:
C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:113:46: error: 'Serial' is not a type
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);
C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:100:51: note: in definition of macro 'MIDI_CREATE_INSTANCE'
MIDI_NAMESPACE::SerialMIDI<Type> serial##Name(SerialPort);\
^~~~~~~~~~
C:\Users\Liam\Documents\Arduino\libraries\stablePot/stablePot.h:10:5: note: in expansion of macro 'MIDI_CREATE_DEFAULT_INSTANCE'
MIDI_CREATE_DEFAULT_INSTANCE();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:101:74: error: expected identifier before '(' token
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);
^
C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:113:9: note: in expansion of macro 'MIDI_CREATE_INSTANCE'
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);
^~~~~~~~~~~~~~~~~~~~
C:\Users\Liam\Documents\Arduino\libraries\stablePot/stablePot.h:10:5: note: in expansion of macro 'MIDI_CREATE_DEFAULT_INSTANCE'
MIDI_CREATE_DEFAULT_INSTANCE();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:101:109: error: expected ',' or '...' before 'serialMIDI'
MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<Type>> Name((MIDI_NAMESPACE::SerialMIDI<Type>&)serial##Name);
^
C:\Users\Liam\Documents\Arduino\libraries\MIDI_Library-5.0.2\src/serialMIDI.h:113:9: note: in expansion of macro 'MIDI_CREATE_INSTANCE'
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);
^~~~~~~~~~~~~~~~~~~~
C:\Users\Liam\Documents\Arduino\libraries\stablePot/stablePot.h:10:5: note: in expansion of macro 'MIDI_CREATE_DEFAULT_INSTANCE'
MIDI_CREATE_DEFAULT_INSTANCE();
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
exit status 1
Error compiling for board Arduino Mega or Mega 2560.
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I just added those but im still getting errors relating to this: -
C:\Users\Liam\Documents\Arduino\libraries\stablePot/stablePot.h:12:5: note: in expansion of macro 'MIDI_CREATE_DEFAULT_INSTANCE'
MIDI_CREATE_DEFAULT_INSTANCE();