11# SPDX-FileCopyrightText: 2022 John Park for Adafruit Industries
22# SPDX-License-Identifier: MIT
33# Motorized fader demo
4-
54import time
65import board
76import pwmio
1211from adafruit_debouncer import Debouncer
1312from adafruit_motor import motor
1413
15- MIDI_DEMO = False
14+ MIDI_DEMO = False # set to True to send MIDI CC
1615
1716# optional MIDI setup
1817if MIDI_DEMO :
2221 midi = adafruit_midi .MIDI (midi_out = usb_midi .ports [1 ], out_channel = 0 )
2322 fader_cc_number = 16
2423
25-
2624# Button setup to store four saved values
2725button_pins = (board .D10 , board .D9 , board .D6 , board .D5 )
2826buttons = []
3129 tmp_pin .pull = Pull .UP
3230 buttons .append (Debouncer (tmp_pin ))
3331
34- saved_positions = (240 , 180 , 120 , 40 ) # pre-saved positions for the buttons to call
32+ saved_positions = (230 , 180 , 120 , 60 ) # pre-saved positions for the buttons to call
3533
3634# Slide pot setup
3735fader = analogio .AnalogIn (board .A0 )
3836fader_position = fader .value # ranges from 0-65535
3937fader_pos = fader .value // 256 # make 0-255 range
4038last_fader_pos = fader_pos
4139
42-
4340# Motor setup
4441PWM_PIN_A = board .D12 # pick any pwm pins on their own channels
4542PWM_PIN_B = board .D11
5047motor1 = motor .DCMotor (pwm_a , pwm_b )
5148
5249# 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 )
50+ touch = touchio .TouchIn (board .A3 )
51+ touch .threshold = touch .raw_value + 30 # tune for fader knob cap
5452
5553# NeoPixel setup
5654led = neopixel .NeoPixel (board .NEOPIXEL , 1 , brightness = 0.2 , auto_write = True )
@@ -60,7 +58,6 @@ def clamp(num, min_value, max_value): # function for clamping motor throttle -1
6058 return max (min (num , max_value ), min_value )
6159
6260def go_to_position (new_position ):
63-
6461 global fader_pos # pylint: disable=global-statement
6562 fader_pos = int (fader .value // 256 )
6663 while abs (fader_pos - new_position ) > 2 :
@@ -90,11 +87,8 @@ def go_to_position(new_position):
9087print ("--__ Flying Fader Demo __--" )
9188print ("\n " * 4 )
9289
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 )
90+ go_to_position (saved_positions [3 ]) # boot up demo
91+ go_to_position (saved_positions [0 ])
9892time .sleep (.6 )
9993
10094current_saved_position = 0 # state to store which is current position from the list
@@ -114,10 +108,9 @@ def go_to_position(new_position):
114108 fader_pos = int ((filter_amt * last_fader_pos ) + ((1.0 - filter_amt ) * fader .value // 256 ))
115109 led [0 ] = (fader_pos , 0 , 0 )
116110 if abs (fader_pos - last_fader_pos ) > 1 : # do things in here, e.g. send MIDI CC
117- fader_width = 90
111+ fader_width = 90 # for text visualization in serial output
118112 print ("-" * (fader_width - int (fader_pos / 3 )), fader_pos , "-" * int (fader_pos / 3 ), end = '\r ' )
119113 last_fader_pos = fader_pos
120-
121114 if MIDI_DEMO :
122115 fader_cc = int (fader_pos / 2 ) # cc is 0-127
123116 midi .send (ControlChange (fader_cc_number , fader_cc ))
0 commit comments