
import board, time, neopixel
import digitalio
import busio
import touchio
import adafruit_mpr121
from adafruit_motor import servo
import pwmio
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile

#configure the speaker

audio = AudioOut(board.D8)


#strip
strip_pin= board.D7
strip_num_of_lights = 20
strip = neopixel.NeoPixel(strip_pin, strip_num_of_lights)



#servo
pwm = pwmio.PWMOut(board.D3, frequency =50)
servo_1 = servo.Servo(pwm)
servo_1 = servo.Servo(pwm, max_pulse = 2500)

pwm2 = pwmio.PWMOut(board.D4, frequency =50)
servo_2 = servo.Servo(pwm2)
servo_2 = servo.Servo(pwm2, max_pulse = 2500)

pwm3 = pwmio.PWMOut(board.D5, frequency =50)
servo_3 = servo.Servo(pwm3)
servo_3 = servo.Servo(pwm3, max_pulse = 2500)



i2c= board.I2C()
touch_pad = adafruit_mpr121.MPR121(i2c)

def pulse_red():
    strip.fill((0,0,0))
    strip.fill((129, 19, 49))
    for i in range(0,100):
        strip.brightness = i/100
        time.sleep(.01)
    for i in range (99,-1,-1):
        strip.brightness = i/100
        time.sleep(.01)

#servo sweep
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
        servo_2.angle = angle
        servo_3.angle = angle
        time.sleep (.05)

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

path = "sounds/"

def play_sound(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        #PUT CODE TO HAPPEN WHILE AUDIO IS PLAYING
        while audio.playing:
            sweeping()




sweep = False
lights = False
playsound= False
while True:

    if touch_pad[1].value:
        print("hi")#if it touches touchpad A4
        lights = True
        playsound = True
        print("hi")


    if sweep:
        sweeping()
    if playsound:
        play_sound("kmk.wav")
    if lights:
        strip.fill((250,0,0))
        strip.brightness = 100

    if touch_pad[9].value:
        playsound= False
        sweep = False
        strip.fill((0,0,0))

# Write your code here :-)
