|
| 1 | +# SPDX-FileCopyrightText: 2022 John Park and Tod Kurt for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +# Darth Faders motorized slide potentiometer sculpture/prop |
| 5 | + |
| 6 | +import time |
| 7 | +import board |
| 8 | +import analogio |
| 9 | +from adafruit_debouncer import Debouncer |
| 10 | +from adafruit_motorkit import MotorKit |
| 11 | +from adafruit_seesaw import seesaw, rotaryio, digitalio |
| 12 | + |
| 13 | +num_faders = 3 |
| 14 | + |
| 15 | +motorwing = MotorKit(i2c=board.I2C()) |
| 16 | +motorwing.frequency = 122 # tune this 50 - 200 range |
| 17 | +max_throttle = 0.18 # tune this 0.2 - 1 range |
| 18 | + |
| 19 | +# make arrays for all the things we care about |
| 20 | +motors = [None] * num_faders |
| 21 | +faders = [None] * num_faders |
| 22 | +faders_pos = [None] * num_faders |
| 23 | +last_faders_pos = [None] * num_faders |
| 24 | + |
| 25 | +# set up motors in motor array |
| 26 | +motors[0] = motorwing.motor1 |
| 27 | +motors[1] = motorwing.motor2 |
| 28 | +motors[2] = motorwing.motor3 |
| 29 | + |
| 30 | +# set motors to "off" |
| 31 | +for i in range(num_faders): |
| 32 | + motors[i].throttle = None |
| 33 | + |
| 34 | +# STEMMA QT Rotary encoder setup |
| 35 | +seesaw = seesaw.Seesaw(board.I2C(), addr=0x36) # default address is 0x36 |
| 36 | +seesaw.pin_mode(24, seesaw.INPUT_PULLUP) |
| 37 | +button_in = digitalio.DigitalIO(seesaw, 24) |
| 38 | +button = Debouncer(button_in) |
| 39 | +encoder = rotaryio.IncrementalEncoder(seesaw) |
| 40 | +last_encoder_pos = 0 |
| 41 | + |
| 42 | + |
| 43 | +# set up faders |
| 44 | +fader_pins = (board.A0, board.A1, board.A2) |
| 45 | +for i in range(num_faders): |
| 46 | + faders[i] = analogio.AnalogIn(fader_pins[i]) |
| 47 | + faders_pos[i] = faders[i].value // 256 # make it 0-255 range |
| 48 | + last_faders_pos[i] = faders_pos[i] |
| 49 | + |
| 50 | + |
| 51 | +def update_position(fader, new_position, speed): |
| 52 | + global faders_pos # pylint: disable=global-statement |
| 53 | + faders_pos[fader] = int(faders[fader].value//256) |
| 54 | + |
| 55 | + if abs(faders_pos[fader] - new_position) > 2 : |
| 56 | + if faders_pos[fader] > new_position : |
| 57 | + motors[fader].throttle = speed |
| 58 | + if faders_pos[fader] < new_position: |
| 59 | + motors[fader].throttle = speed * -1 |
| 60 | + faders_pos[fader] = int(faders[fader].value//256) |
| 61 | + if faders_pos[fader] == new_position: |
| 62 | + motors[fader].throttle = None |
| 63 | + |
| 64 | +# pre-saved positions for the buttons to call |
| 65 | +H = 240 # high |
| 66 | +M = 127 # mid |
| 67 | +L = 10 # low |
| 68 | + |
| 69 | +# create custom animation patterns here: |
| 70 | +saved_positions = ( |
| 71 | + (M, H, L, L, H, M, L), # fader 1 |
| 72 | + (M, H, L, H, H, M, L), # fader 2 |
| 73 | + (M, L, L, H, H, M, L) # fader 3 |
| 74 | +) |
| 75 | + |
| 76 | + |
| 77 | +num_positions = len(saved_positions[0]) # how many moves in our move list |
| 78 | +position = 0 # which column of our 'saved_position' move list we're currently on |
| 79 | + |
| 80 | +last_time = time.monotonic() |
| 81 | +period = 4 # time in seconds between new desitnation picks |
| 82 | + |
| 83 | +print('Darth Fader will see you know.') |
| 84 | + |
| 85 | +move_state = False |
| 86 | +encoder_delta = 0 |
| 87 | + |
| 88 | +while True: |
| 89 | + button.update() |
| 90 | + if button.fell: |
| 91 | + move_state = not move_state |
| 92 | + print("move_state is " + str(move_state)) |
| 93 | + if not move_state: |
| 94 | + for i in range(num_faders): |
| 95 | + motors[i].throttle = None |
| 96 | + |
| 97 | + # always move all motors toward destinations |
| 98 | + for i in range(num_faders): |
| 99 | + update_position(i, saved_positions[i][position], max_throttle) |
| 100 | + |
| 101 | + # has encoder been turned? |
| 102 | + encoder_pos = -encoder.position |
| 103 | + if encoder_pos != last_encoder_pos: |
| 104 | + encoder_delta = encoder_pos - last_encoder_pos |
| 105 | + last_encoder_pos = encoder_pos |
| 106 | + |
| 107 | + # if we are not moving automatically, allow encoder tuning animation frames |
| 108 | + if not move_state: |
| 109 | + if encoder_delta: # encoder was turned |
| 110 | + direction = 1 if (encoder_delta > 0) else -1 # which direction encoder was turned |
| 111 | + position = (position + direction) % num_positions # increment/decrement |
| 112 | + encoder_delta = 0 |
| 113 | + |
| 114 | + # else we are moving automatically |
| 115 | + if move_state: |
| 116 | + # if it is time to go to a new destination, do it |
| 117 | + if time.monotonic() - last_time > period: # after 'period' seconds, change |
| 118 | + last_time = time.monotonic() |
| 119 | + position = (position + 1) % num_positions |
0 commit comments