from machine import Pin,PWM
from time import ticks_ms, sleep_ms, sleep

ledvermelho = Pin(23, Pin.OUT)
ledamarelo = Pin(22, Pin.OUT)
ledverde = Pin(21, Pin.OUT)
ledazul = Pin(19, Pin.OUT)
buzzer = Pin(18, Pin.OUT)
botazul = Pin(32, Pin.IN, Pin.PULL_DOWN)
botamarelo = Pin(35, Pin.IN, Pin.PULL_DOWN)
botverde = Pin(34, Pin.IN, Pin.PULL_DOWN)
botemergencia = Pin(33, Pin.IN, Pin.PULL_DOWN)
botdesligado = Pin(25, Pin.IN, Pin.PULL_DOWN)

pwm = PWM(buzzer)
pwm.duty_u16(0)
estado = "Desligado"

botem = bante = botemergencia.value()
botaz = bantaz = botazul.value()
botamar = bantamar = botamarelo.value()
botverd = bantver = botverde.value()
botdesl = bantdesl = botdesligado.value()

while True:

    botdesl = botdesligado.value()
    if botdesl != bantdesl:
        if botdesl ==1:
            sleep_ms(200)
            estado = 'Desligado'
            print('desligado')
        bantdesl = botdesl

    botem = botemergencia.value()
    if botem != bante:
        if botem == 1:
            sleep_ms(200)
            estado = 'Emergencia'
            print ('Emergencia')
        bante = botem

    botamar = botamarelo.value()
    if botamar != bantamar:
        if botamar == 1:
            sleep_ms(200)
            estado = 'Atenção'
            print ('atenção')
        bantamar = botamar

    botverd = botverde.value()
    if botverd != bantver:
        if botverd == 1:
            sleep_ms(200)
            estado = 'Segurança'
            print ('segurança')
        bantver = botverd
        
    botaz = botazul.value()    
    if botaz != bantaz:
        sleep_ms(200)
        if botaz ==1:
            estado = 'Rearme'
            print('rearme')
        bantaz = botaz   

    if estado == 'Desligado':
         ledvermelho.off()
         ledamarelo.off()
         ledverde.off()
         ledazul.off()
         pwm.duty_u16(0)
         
    elif estado == 'Emergencia':
         ledamarelo.off()
         ledvermelho.on()
         ledazul.off()
         ledverde.off()
         pwm= PWM(buzzer)
         pwm.freq(440)
        
    elif estado == 'Atenção':
         ledamarelo.on()
         ledvermelho.off()
         ledverde.off()
         ledazul.off()
         pwm.duty_u16(0)

    elif estado == 'Segurança':
         ledamarelo.off()
         ledvermelho.off()
         ledazul.off()
         ledverde.on()
         pwm.duty_u16(0)

    elif estado == 'Rearme':
         ledamarelo.off()
         ledvermelho.off()
         ledverde.off()
         ledazul.on()
         pwm.duty_u16(0)