diff --git a/main.py b/main.py index 517abf4..7e44c60 100644 --- a/main.py +++ b/main.py @@ -12,6 +12,8 @@ cc = 68 # Target Control Change number - this is for Behringer X32 Matrix 5 cc_min = 0 # Minimum desired CC output cc_max = 97 # Maximum desired CC output (only want fader to go to unity gain - hence not 127) +exp_calibration_threshold = 40000 # This is the minimum amount of change required successful for calibration + # Devices exp = machine.ADC(Pin(26)) # Expression pedal device on pin 31 uart = machine.UART(1, 31250) # UART Midi device on pin 6 @@ -20,20 +22,11 @@ led = machine.Pin(25, Pin.OUT) # Pico onboard led # Initialise variables # Set these to reverse thresholds to enable self calibration exp_min = 65535 -exp_max = 0 - -exp_calibration_threshold = 40000 # This is the minimum amount of change required successful for calibration - -cc_previous = 0 -cc_current = 0 +exp_max = 1 # This function translates the expression pedal value to the equivalent CC value def translate(exp_val): - if exp_max - exp_min > exp_calibration_threshold: - ret = int((((exp_val - exp_min) * (cc_max - cc_min)) / (exp_max - exp_min)) + cc_min) - else: - return 0 - + ret = int((((exp_val - exp_min) * (cc_max - cc_min)) / (exp_max - exp_min)) + cc_min) if ret > 0: return ret else: @@ -42,18 +35,19 @@ def translate(exp_val): exp_previous = exp.read_u16() while True: exp_current = exp.read_u16() - if abs(exp_current - exp_previous) > 800: - exp_previous = exp_current - cc_current = translate(exp_current) + + # Only process if the change ratio is greater than the possible number of CC values + if abs(exp_current - exp_previous)/exp_max > 1/(cc_max - cc_min): if exp_current > exp_max: exp_max = exp_current elif exp_current < exp_min: exp_min = exp_current + exp_previous = exp_current - if cc_current != cc_previous: - led.value(1) # Turn led on - uart.write(ustruct.pack("bbb",ControlChange + midi_channel - 1,cc,cc_current)) - led.value(0) # Turn led off - cc_previous = cc_current - print("Writing Midi Channel: {}, ControlChange: {}, Value {}. Exp Pedal: cur: {}, min: {}, max: {}".format(midi_channel, cc, cc_current, exp_current, exp_min, exp_max)) - time.sleep(0.05) \ No newline at end of file + # Only send midi when calibration threshold has been reached + if exp_max - exp_min > exp_calibration_threshold: + cc_val = translate(exp_current) + led.value(1) # Turn led on + uart.write(ustruct.pack("bbb",ControlChange + midi_channel - 1,cc,cc_val)) + led.value(0) # Turn led off + print("Writing Midi Channel: {}, ControlChange: {}, Value {}. Exp Pedal: cur: {}, min: {}, max: {}".format(midi_channel, cc, cc_val, exp_current, exp_min, exp_max)) \ No newline at end of file