# crickit/toggle_motor.py

# Demonstrate touch-activated control of a DC motor connected to the Crickit motor driver.

# 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

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

# Import the Crickit interface library.
from adafruit_crickit import crickit

# ----------------------------------------------------------------
# Global state
moving = False
touched_last = False

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

while True:

    # Check whether the touch state changed.
    touched_now = crickit.touch_1.value

    if touched_now and not touched_last:
        # starting new touch, toggle motor state
        moving = not moving
        crickit.dc_motor_1.throttle = 1 if moving else 0

    # Save the current touch state.
    touched_last = touched_now
