|
| 1 | +# SPDX-FileCopyrightText: 2022 John Park for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# Motorized fader demo |
| 4 | + |
| 5 | +import time |
| 6 | +import board |
| 7 | +import pwmio |
| 8 | +import analogio |
| 9 | +import touchio |
| 10 | +import neopixel |
| 11 | +from digitalio import DigitalInOut, Pull |
| 12 | +from adafruit_debouncer import Debouncer |
| 13 | +from adafruit_motor import motor |
| 14 | + |
| 15 | +MIDI_DEMO = False |
| 16 | + |
| 17 | +# optional MIDI setup |
| 18 | +if MIDI_DEMO: |
| 19 | + import usb_midi |
| 20 | + import adafruit_midi |
| 21 | + from adafruit_midi.control_change import ControlChange |
| 22 | + midi = adafruit_midi.MIDI(midi_out=usb_midi.ports[1], out_channel=0) |
| 23 | + fader_cc_number = 16 |
| 24 | + |
| 25 | + |
| 26 | +# Button setup to store four saved values |
| 27 | +button_pins = (board.D10, board.D9, board.D6, board.D5) |
| 28 | +buttons = [] |
| 29 | +for button_pin in button_pins: |
| 30 | + tmp_pin = DigitalInOut(button_pin) |
| 31 | + tmp_pin.pull = Pull.UP |
| 32 | + buttons.append(Debouncer(tmp_pin)) |
| 33 | + |
| 34 | +saved_positions = (240, 180, 120, 40) # pre-saved positions for the buttons to call |
| 35 | + |
| 36 | +# Slide pot setup |
| 37 | +fader = analogio.AnalogIn(board.A0) |
| 38 | +fader_position = fader.value # ranges from 0-65535 |
| 39 | +fader_pos = fader.value // 256 # make 0-255 range |
| 40 | +last_fader_pos = fader_pos |
| 41 | + |
| 42 | + |
| 43 | +# Motor setup |
| 44 | +PWM_PIN_A = board.D12 # pick any pwm pins on their own channels |
| 45 | +PWM_PIN_B = board.D11 |
| 46 | + |
| 47 | +# DC motor driver setup -- these pins go to h-bridge driver such as L9110 |
| 48 | +pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50) |
| 49 | +pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50) |
| 50 | +motor1 = motor.DCMotor(pwm_a, pwm_b) |
| 51 | + |
| 52 | +# Touch setup pin goes from touch pin on slide pot to touch capable pin and then 1MΩ to gnd |
| 53 | +touch = touchio.TouchIn(board.A4) |
| 54 | + |
| 55 | +# NeoPixel setup |
| 56 | +led = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2, auto_write=True) |
| 57 | +led.fill(0xff0000) |
| 58 | + |
| 59 | +def clamp(num, min_value, max_value): # function for clamping motor throttle -1.0 to 1.0 |
| 60 | + return max(min(num, max_value), min_value) |
| 61 | + |
| 62 | +def go_to_position(new_position): |
| 63 | + |
| 64 | + global fader_pos # pylint: disable=global-statement |
| 65 | + fader_pos = int(fader.value//256) |
| 66 | + while abs(fader_pos - new_position) > 2 : |
| 67 | + if fader_pos > new_position : |
| 68 | + speed = 2.25 * abs(fader_pos - new_position) / 256 + 0.12 |
| 69 | + speed = clamp(speed, -1.0, 1.0) |
| 70 | + motor1.throttle = speed |
| 71 | + led[0] = (fader_pos, 0, 0) |
| 72 | + |
| 73 | + if MIDI_DEMO: |
| 74 | + global fader_cc # pylint: disable=global-statement |
| 75 | + fader_cc = int(fader_pos / 2) # cc is 0-127 |
| 76 | + midi.send(ControlChange(fader_cc_number, fader_cc)) |
| 77 | + |
| 78 | + if fader_pos < new_position: |
| 79 | + speed = -2.25 * abs(fader_pos - new_position) / 256 - 0.12 |
| 80 | + speed = clamp(speed, -1.0, 1.0) |
| 81 | + motor1.throttle = speed |
| 82 | + led[0] = (fader_pos, 0, 0) |
| 83 | + |
| 84 | + if MIDI_DEMO: |
| 85 | + fader_cc = int(fader_pos / 2) # cc is 0-127 |
| 86 | + midi.send(ControlChange(fader_cc_number, fader_cc)) |
| 87 | + |
| 88 | + fader_pos = int(fader.value//256) |
| 89 | + motor1.throttle = None |
| 90 | +print("--__ Flying Fader Demo __--") |
| 91 | +print("\n"*4) |
| 92 | + |
| 93 | +go_to_position(120) |
| 94 | +go_to_position(60) |
| 95 | +go_to_position(180) |
| 96 | +go_to_position(40) |
| 97 | +go_to_position(200) |
| 98 | +time.sleep(.6) |
| 99 | + |
| 100 | +current_saved_position = 0 # state to store which is current position from the list |
| 101 | + |
| 102 | +while True: |
| 103 | + for i in range(len(buttons)): |
| 104 | + buttons[i].update() |
| 105 | + if buttons[i].fell: # if a button is pressed, update the position from list |
| 106 | + current_saved_position = i |
| 107 | + |
| 108 | + if touch.value: |
| 109 | + motor1.throttle = None # idle |
| 110 | + else: |
| 111 | + go_to_position(saved_positions[current_saved_position]) |
| 112 | + |
| 113 | + filter_amt = 0.1 # higher number will be a slower filter between 1.0 and 0.1 is good |
| 114 | + fader_pos = int((filter_amt * last_fader_pos) + ((1.0-filter_amt) * fader.value//256)) |
| 115 | + led[0] = (fader_pos, 0, 0) |
| 116 | + if abs(fader_pos - last_fader_pos) > 1 : # do things in here, e.g. send MIDI CC |
| 117 | + fader_width = 90 |
| 118 | + print("-" * (fader_width - int(fader_pos/3)), fader_pos, "-" * int(fader_pos/3), end='\r') |
| 119 | + last_fader_pos = fader_pos |
| 120 | + |
| 121 | + if MIDI_DEMO: |
| 122 | + fader_cc = int(fader_pos / 2) # cc is 0-127 |
| 123 | + midi.send(ControlChange(fader_cc_number, fader_cc)) |
0 commit comments