code.py
 1  # SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import time
 6  
 7  import adafruit_motor.servo
 8  import board
 9  import pwmio
10  from analogio import AnalogIn
11  from digitalio import DigitalInOut, Direction, Pull
12  
13  pwm = pwmio.PWMOut(board.D5, frequency=50)
14  servo = adafruit_motor.servo.Servo(pwm)
15  switch = DigitalInOut(board.D7)
16  switch.direction = Direction.INPUT
17  switch.pull = Pull.UP
18  pot = AnalogIn(board.A0)
19  
20  continuous = adafruit_motor.servo.ContinuousServo(pwm)
21  
22  
23  def val(pin):
24      # divides voltage (65535) to get a value between 0 and 1
25      return pin.value / 65535
26  
27  
28  while True:
29  
30      if switch.value:
31          continuous.throttle = val(pot) * -1
32      else:
33          continuous.throttle = val(pot) * 1
34  
35      time.sleep(0.001)