Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

softservo library and circuit python
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

softservo library and circuit python

by bguenther on Mon Oct 18, 2021 3:59 pm

Hello -

I'm following the instructions for the Bionic Eye project and am not sure if I'm doing everything correctly on this page:

https://learn.adafruit.com/3d-printed-b ... ython-code

My Trinket M0 is up and running fine. The blink test works. I put the Adafruit_motor library in the "lib" folder and saved the code to "code.py."

Where I am lost is the code instructions say, "Required library is the Adafruit_SoftServo library available at https://github.com/adafruit/Adafruit_SoftServo" and there is code at the bottom of the page that I'm not sure what to do with:

$ mkdir /Volumes/CIRCUITPY/lib
$ cp -pr adafruit_motor /Volumes/CIRCUITPY/lib

Do I add the Adafruit_SoftServo library to my CIRCUITPY drive? What do I do with that bit of code at the bottom of the page?

Thanks for your help

bguenther
 
Posts: 10
Joined: Sun Dec 28, 2014 7:01 pm

Re: softservo library and circuit python

by mikeysklar on Wed Oct 20, 2021 11:11 am

The Adafruit_SoftServo library is for Arduino code. Everything else you have done correctly using CircuitPython so let's keep the SoftServo stuff out of this.

You can copy the code from this page into code.py.

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2018 Bill Earl and Mikey Sklar for Adafruit Industries
#
# SPDX-License-Identifier: MIT
#
# Bionic Eye sketch for Adafruit Trinket.
#
# written by Bill Earl for Arduino
# ported to CircuitPython by Mikey Sklar
# for Adafruit Industries
#
# Required library is the Adafruit_SoftServo library
# available at https://github.com/adafruit/Adafruit_SoftServo
# The standard Arduino IDE servo library will not work with 8 bit
# AVR microcontrollers like Trinket and Gemma due to differences
# in available timer hardware and programming. We simply refresh
# by piggy-backing on the timer0 millis() counter
#
# Trinket:        Bat+    Gnd       Pin #0  Pin #2
# Connection:     Servo+  Servo-    Tilt    Rotate
#                 (Red)   (Black)   Servo   Servo
#                                   (Orange)(Orange)

import time
import random
import board
import pwmio
from adafruit_motor import servo

# we are intentionally avoiding Trinket Pin #1 (board.A0)
# as it does not have PWM capability
tilt_servo_pin = board.A2   # servo control line (orange) Trinket Pin #0
rotate_servo_pin = board.A1 # servo control line (orange) Trinket Pin #2

# servo object setup for the M0 boards:
tilt_pwm = pwmio.PWMOut(tilt_servo_pin, duty_cycle=2 ** 15, frequency=50)
rotate_pwm = pwmio.PWMOut(rotate_servo_pin, duty_cycle=2 ** 15, frequency=50)
tilt_servo = servo.Servo(tilt_pwm)
rotate_servo = servo.Servo(rotate_pwm)

# servo timing and angle range
tilt_min = 120      # lower limit to tilt rotation range
max_rotate = 180    # rotation range limited to half circle

while True:

    # servo tilt - on average move every 500ms
    if random.randint(0,100) > 80:
        tilt_servo.angle = random.randint(tilt_min, max_rotate)
        time.sleep(.25)

    # servo rotate - on average move every 500ms
    if random.randint(0,100) > 90:
        rotate_servo.angle = random.randint(0, max_rotate)
        time.sleep(.25)


https://learn.adafruit.com/3d-printed-b ... ython-code

mikeysklar
 
Posts: 6283
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.