#using a servo motor
import board
import neopixel
import time
import pwmio
import digitalio #need this for buttons
from adafruit_motor import servo
from adafruit_led_animation.color import (
    AMBER, #(255, 100, 0)
    AQUA, # (50, 255, 255)
    BLACK, #OFF (0, 0, 0)
    BLUE, # (0, 0, 255)
    CYAN, # (0, 255, 255)
    GOLD, # (255, 222, 30)
    GREEN, # (0, 255, 0)
    JADE, # (0, 255, 40)
    MAGENTA, #(255, 0, 20)
    OLD_LACE, # (253, 245, 230)
    ORANGE, # (255, 40, 0)
    PINK, # (242, 90, 255)
    PURPLE, # (180, 0, 255)
    RED, # (255, 0, 0)
    TEAL, # (0, 255, 120)
    WHITE, # (255, 255, 255)
    YELLOW, # (255, 150, 0)
    RAINBOW # a list of colors to cycle through
    # RAINBOW is RED, ORANGE, YELLOW, GREEN, BLUE, and PURPLE ((255, 0, 0), (255, 40, 0), (255, 150, 0), (0, 255, 0), (0, 0, 255), (180, 0, 255))
)

strip_pin= board.A5
strip_num_of_lights = 30
strip = neopixel.NeoPixel(strip_pin, strip_num_of_lights)
wait_time = .002



#code for setting up button A on the board
button_A = digitalio.DigitalInOut(board.BUTTON_A)
#line to make object button a wait for input
button_A.switch_to_input(pull=digitalio.Pull.DOWN)

#code for Butoon b on board
button_B = digitalio.DigitalInOut(board.BUTTON_B)
button_B.switch_to_input(pull= digitalio.Pull.DOWN)



# create a PWNOut object
pwm = pwmio.PWMOut(board.A2, frequency =50)

#create a servo object
servo_1 = servo.Servo(pwm)

#calibrating a servo
servo_1 = servo.Servo(pwm, max_pulse = 2500)

strip.fill(BLACK)
pulsing = False
sweep = False

def sweeping():
     for angle in range (0, 181, 5): #up to 181 but never reaches it and 5 for increasing by 5 degrees each time
            print(angle)
            servo_1.angle = angle
            time.sleep (.05)

def sweep1():
    for angle in range (180, -1, -5):
        servo_1.angle = angle
        time.sleep (.05)


while True:
    if button_A.value:
        pulsing = True
        sweep = True

        #pulse. the period is how long it takes for the pulse to complete
    if pulsing:
        strip.fill(YELLOW)
        for index in range(0,10):
            print(index)
            strip.brightness = index/10
            sweeping()
            sweep1()
            time.sleep(wait_time)
        for index in range(9,-1,-1):
            print(index)
            strip.brightness = index/10
            sweeping()
            sweep1()
            time.sleep(wait_time)



    elif button_B.value:
       servo_1.angle = 0
       time.sleep (.2)
       strip.fill(BLACK)
       sweep = False
       pulsing = False

 # Write your code here :-)
# Write your code here :-)
