|
| 1 | +# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import time |
| 5 | +import board |
| 6 | +import pwmio |
| 7 | +import simpleio |
| 8 | +from adafruit_motor import motor |
| 9 | +from analogio import AnalogIn |
| 10 | +from digitalio import DigitalInOut, Direction, Pull |
| 11 | + |
| 12 | +# button setup |
| 13 | +warble_switch = DigitalInOut(board.A0) |
| 14 | +warble_switch.direction = Direction.INPUT |
| 15 | +warble_switch.pull = Pull.UP |
| 16 | +# potentiometer setup |
| 17 | +pot = AnalogIn(board.A1) |
| 18 | + |
| 19 | +# PWM pins for L9110 |
| 20 | +PWM_PIN_A = board.A3 # pick any pwm pins on their own channels |
| 21 | +PWM_PIN_B = board.A2 |
| 22 | +# PWM setup |
| 23 | +pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50) |
| 24 | +pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50) |
| 25 | +# motor setup |
| 26 | +cassette = motor.DCMotor(pwm_a, pwm_b) |
| 27 | + |
| 28 | +# variables for warble switch |
| 29 | +i = 0.4 |
| 30 | +last_i = 0.4 |
| 31 | + |
| 32 | +while True: |
| 33 | + # map range of pot to range of motor speed |
| 34 | + # all the way to the left will run the motor in reverse full speed |
| 35 | + # all the way to the right will run the motor forward full speed |
| 36 | + mapped_speed = simpleio.map_range(pot.value, 0, 65535, -1.0, 1.0) |
| 37 | + # if you press the button... |
| 38 | + # creates a ramping effect |
| 39 | + if not warble_switch.value: |
| 40 | + # checks current pot reading |
| 41 | + # if it's positive... |
| 42 | + if mapped_speed > 0: |
| 43 | + # sets starting speed |
| 44 | + i = 0.4 |
| 45 | + # sets last value to loop |
| 46 | + last_i = i |
| 47 | + # notes that it's positive |
| 48 | + pos = True |
| 49 | + # if it's negative... |
| 50 | + else: |
| 51 | + # sets starting speed |
| 52 | + i = -0.4 |
| 53 | + # sets last value to loop |
| 54 | + last_i = i |
| 55 | + # notes that it's negative |
| 56 | + neg = True |
| 57 | + # loop 8 times |
| 58 | + for z in range(8): |
| 59 | + # if it's positive... |
| 60 | + if pos: |
| 61 | + # increase speed |
| 62 | + i += 0.06 |
| 63 | + # if it's negative |
| 64 | + else: |
| 65 | + # decrease speed |
| 66 | + i -= 0.06 |
| 67 | + # send value to motor |
| 68 | + cassette.throttle = i |
| 69 | + time.sleep(0.1) |
| 70 | + # loop the value while button is held down |
| 71 | + i = last_i |
| 72 | + pos = False |
| 73 | + neg = False |
| 74 | + # run motor at mapped speed from the pot |
| 75 | + cassette.throttle = mapped_speed |
0 commit comments