# cpb_servo_sweep.py

# Demonstrate oscillating movement of a hobby servo.  Note that only a tiny
# micro-servo (e.g. "SG90 9g") is safe to drive directly off the board; in
# general, hobby servos require a separate battery supply.  The wiring for this
# is as follows:
#
#  1. The servo control line (typ. orange) connects to SDA A5  
#  2. The servo ground line  (typ. brown)  connects to GND     
#  3. The servo power line   (typ. red)    connects to 6V battery

# Related documentation:
# https://circuitpython.readthedocs.io/en/latest/shared-bindings/pwmio/index.html#module-pwmio
# https://circuitpython.readthedocs.io/projects/motor/en/latest/api.html#module-adafruit_motor.servo

# ----------------------------------------------------------------
# Import the standard Python time functions.
import time

# Import the low-level hardware libraries.
import board
import pwmio

# Import the Adafruit helper library.
from adafruit_motor import servo

# ----------------------------------------------------------------
# Initialize hardware.

# Create a PWMOut object on pad SDA A5 to generate control signals.
pwm = pwmio.PWMOut(board.A5, duty_cycle=0, frequency=50)

# Create a Servo object which controls a hobby servo using the PWMOut.
actuator = servo.Servo(pwm, min_pulse=1000, max_pulse=2000)

# ----------------------------------------------------------------
# Begin the main processing loop.
while True:
    print("Starting cycle.")
    for angle in range(0, 180, 5):
        actuator.angle = angle
        time.sleep(0.02)

    print("Reversing cycle.")
    for angle in range(180, 0, -5):
        actuator.angle = angle
        time.sleep(0.02)

    print("Pausing.")
    time.sleep(0.5)
    
