// https://wokwi.com/projects/355570007735623681
// https://forum.arduino.cc/t/midi-notes-dont-stop-on-cv-to-midi-box/1084044
// MONOPHONIC CV to MIDI adaptor for Monophonic synthisizers.
void xprintf(const char *format, ...); // programmer misses printf, so sue me.
int gateLevel = 0;
int gateOld = 0;
int noteNum = 0;
int midCh = 1;
class fakeMIDI {
public:
void begin(unsigned char);
void sendNoteOn(unsigned char, unsigned char, unsigned char);
void sendNoteOff(unsigned char, unsigned char, unsigned char);
private:
};
void fakeMIDI::begin(unsigned char x)
{
xprintf("begin MIDI object %d\n", x);
}
void fakeMIDI::sendNoteOn(unsigned char theNote, unsigned char theIDK, unsigned char theChannel)
{
xprintf("note %2d ON w/ IDK %3d.\n", theNote, theIDK);
}
void fakeMIDI::sendNoteOff(unsigned char theNote, unsigned char theIDK, unsigned char theChannel)
{
xprintf("note %2d OFF w/ IDK %3d.\n", theNote, theIDK);
}
fakeMIDI MIDI;
//
void setup()
{
mySetup(115200);
xprintf("\nhello world.\n");
MIDI.begin(222);
pinMode(A1, INPUT_PULLUP);
}
void loop() {
int pitchPin = analogRead(A0); // read analog pins
int gatePin = !digitalRead(A1); // IRL gate is a variable voltage threshold
float voltage = pitchPin * (5.0 / 1023.0); // get voltage for pitch cv input
int noteNum = (voltage / 0.08) + 24;
// remove this, it is just to make fewer notes on the slide fader
noteNum = map(pitchPin, 0, 1024, 48, 62); // remove. just making the steps big
gateLevel = gatePin; // digital input here
if (gateLevel == 1) // if the gate is open
myMidiNoteOn(noteNum, 127, midCh); // turn on, if necessary, the note
if (gateLevel != gateOld) { // when the gate goes down
if (!gateLevel)
myMidiNoteOff(); // turn off any playing note
}
gateOld = gateLevel;
delay(50); // why?
}
bool noteIsOn;
unsigned char noteIs;
void myMidiNoteOn(unsigned char newNote, unsigned char IDK, unsigned char theChannel)
{
if (noteIsOn && noteIs != newNote) {
MIDI.sendNoteOff(noteIs, 0, midCh);
delay(29); // why?
}
if (noteIsOn && noteIs == newNote)
return;
noteIsOn = true; // remember note is on
noteIs = newNote; // and which, so we can turn it off
MIDI.sendNoteOn(noteIs, 127, midCh);
}
// we want an independent means to turn off a note
void myMidiNoteOff()
{
if (!noteIsOn)
return;
MIDI.sendNoteOff(noteIs, 0, midCh);
delay(29); // why?
noteIsOn = false;
}
//
// programmer misses printf...
void xprintf(const char *format, ...)
{
char buffer[256];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
Serial.print(buffer);
}
// programmer forgets day of week, version
void mySetup(unsigned long bandRate)
{
Serial.begin(bandRate);
char s[] = __FILE__;
byte b = sizeof(s);
while ( (b > 0) && (s[b] != 47)) b--;
char *u = s + b + 1;
xprintf("\nHEllo WOrld!\n");
xprintf("%s %s\n\n", __DATE__, u);
xprintf("!\n");
}
GATE
PITCH
CLOSE