Skip to content

Instantly share code, notes, and snippets.

@yuraj11
Last active March 21, 2020 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuraj11/0206e8edc4fad292b3538b0a04df674b to your computer and use it in GitHub Desktop.
Save yuraj11/0206e8edc4fad292b3538b0a04df674b to your computer and use it in GitHub Desktop.
Version 0.1
// Uses MIDIUSB library - https://www.arduino.cc/en/Reference/MIDIUSB
// Autoengage - when pedal value is below certain value (SWITCH_TRESHOLD_VALUE) for certain time (SWITCH_TRESHOLD_MILLIS) then It sends
// a CC midi message (CC_AUTO_ENGAGE). When pedal changes above the treshold value (SWITCH_TRESHOLD_VALUE) It will immediately
// send a CC midi message (CC_AUTO_ENGAGE).
#include "MIDIUSB.h"
//
// CONFIGURATION - MODIFY THE VALUES IF NEEDED
//
#define PIN_EXPRESSION A0 //Analog input pin
#define SWITCH_TRESHOLD_VALUE 3 //Threshold, Value between 0-127
#define SWITCH_TRESHOLD_MILLIS 500 //Milliseconds, time to wait before sending CC_AUTO_ENGAGE
#define MIDI_CHANNEL 1
#define CC_EXPRESSION 11 //CC midi message for expression pedal value change
#define CC_AUTO_ENGAGE 12 //CC midi message for autoengage/toggle value
//
// END OF CONFIGURATION
//
int lastVal = 0;
long millisSinceLastChange = 0;
bool isEffectOn = false;
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
MidiUSB.flush();
}
void setup() {}
void loop() {
int tempAnalog = analogRead(PIN_EXPRESSION);
// Convert 10 bit to 7 bit
tempAnalog = map(tempAnalog, 0, 1023, 0, 127);
tempAnalog = constrain(tempAnalog, 0, 127);
if (isEffectOn && tempAnalog != lastVal) {
controlChange(MIDI_CHANNEL, CC_EXPRESSION, tempAnalog);
lastVal = tempAnalog;
millisSinceLastChange = millis();
}
if (!isEffectOn && tempAnalog >= SWITCH_TRESHOLD_VALUE) {
controlChange(MIDI_CHANNEL, CC_AUTO_ENGAGE, 127);
isEffectOn = true;
} else if (isEffectOn && (millis() - millisSinceLastChange) > SWITCH_TRESHOLD_MILLIS && tempAnalog < SWITCH_TRESHOLD_VALUE) {
controlChange(MIDI_CHANNEL, CC_AUTO_ENGAGE, 0);
isEffectOn = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment