# Write your code here :-)import time
import board
import pwmio
from adafruit_motor import servo
from adafruit_circuitplayground import cp

# --- Servo setup ---
pwm = pwmio.PWMOut(board.A1, duty_cycle=2 ** 15, frequency=50)
my_servo = servo.Servo(pwm)

# --- Settings ---
LIGHT_THRESHOLD = 150 # how bright light is to trigger sensor
HOLD_TIME = 1.0 # leaves it open this long
SHAKE_THRESHOLD = 20  # adjust for sensitivity

# --- Variables ---
triggered_until = 0

# --- Store previous accelerometer readings ---
prev_x, prev_y, prev_z = cp.acceleration

while True:
    # --- Read sensors ---
    light_level = cp.light
    button_pressed = cp.button_a or cp.button_b
    x, y, z = cp.acceleration

    # --- Compute acceleration change ---
    accel_change = abs(x - prev_x) + abs(y - prev_y) + abs(z - prev_z)
    shake_detected = accel_change > SHAKE_THRESHOLD # this helps it know where origin is to see if its shaken

    # --- Update previous values ---
    prev_x, prev_y, prev_z = x, y, z

    # --- Check triggers ---
    if light_level > LIGHT_THRESHOLD or button_pressed or shake_detected:
        triggered_until = time.monotonic() + HOLD_TIME

    # --- Servo + LED behavior ---
    if time.monotonic() < triggered_until:
        cp.pixels.fill((0, 5, 0))  # green = triggered
        my_servo.angle = 120 # 90 degrees wasn't perpendicular enough
    else:
        cp.pixels.fill((5, 0, 0))  # red = idle
        my_servo.angle = 0 # return to normal

    # --- Debug info ---
    print(f"Light: {light_level}, Button: {button_pressed}, Shake: {shake_detected}, Δaccel: {accel_change:.2f}")
    # this will help you see if there are errors in code as well as indicate how it operates

    time.sleep(0.05)

