# crickit/sequence_motor.py

# Demonstrate timed activation of several DC motors connected to the Crickit motor driver.

# The Crickit has two motor outputs labeled 1 and 2.  Each has two screw
# terminals for the motor wires.

# The Crickit has four drive outputs labeled 1 through 4.  Each has only two
# screw terminal, so each motor should be connected to the driver terminal and
# also to +5V (located on the same terminal strip).

# Related documentation:
# http://docs.circuitpython.org/projects/crickit/en/latest/
# http://docs.circuitpython.org/projects/seesaw/en/latest/
# https://learn.adafruit.com/adafruit-crickit-creative-robotic-interactive-construction-kit

# Course documentation:
#   https://courses.ideate.cmu.edu/16-376/s2023/ref/text/code/crickit-intro.html
#   https://courses.ideate.cmu.edu/16-376/s2023/ref/text/code/crickit-quickref.html
#   https://courses.ideate.cmu.edu/16-376/s2023/ref/text/code/cpb-quickref.html


# ----------------------------------------------------------------
# Import standard Python modules.
import time, math

# Import the Bluefruit interface library.
from adafruit_circuitplayground import cp

# Import the Crickit interface library.
from adafruit_crickit import crickit

# ----------------------------------------------------------------
# Enter the main event loop.

while True:
    print("motor 1 forward...")
    crickit.dc_motor_1.throttle = 1   # full speed forward
    cp.pixels[0] = (0, 127, 0)        # medium green
    time.sleep(2)

    print("motor 1 stop...")
    crickit.dc_motor_1.throttle = 0   # stop
    cp.pixels[0] = (32, 0, 0)         # dim red
    time.sleep(1)

    print("motor 2 forward...")
    crickit.dc_motor_2.throttle = 1    # full speed forward
    cp.pixels[1] = (0, 127, 0)         # medium green
    time.sleep(2)

    print("motor 2 stop...")
    crickit.dc_motor_2.throttle = 0    # stop
    cp.pixels[1] = (32, 0, 0)          # dim red
    time.sleep(1)

    # Please note that the drive output uses 'fraction' instead of 'throttle'.
    print("drive 1 forward...")
    crickit.drive_1.fraction = 1       # full speed forward
    cp.pixels[2] = (0, 127, 0)         # medium green
    time.sleep(2)

    print("drive 1 stop...")
    crickit.drive_1.fraction = 0       # stop
    cp.pixels[2] = (32, 0, 0)          # dim red
    time.sleep(2)

    # this could continue with drive_2, drive_3, and drive_4 if needed
    
    # end of the sequence, it will now loop back from the start
    
