##################################################
# UPTV           Fecha: 18/03/2024               #
# Nombre y apellido: Isabel Graterol             #
# C.I.: 30782561                                 #
# Seccion: IN332                                 #
################################################## 

from time import sleep
from machine import Pin, ADC, I2C, PWM
from pico_i2c_lcd import I2cLcd
import math

i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000)
I2C_ADDR = i2c.scan()[0]

#Luces lcd
pines = [i for i in range(8)]
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
#Potenciometro
pot = ADC(Pin(28))
#Buzzer
out = PWM(Pin(21))

def sonido(tiempo):
    out.freq(440)
    out.duty_u16(32768)
    sleep(tiempo)
    out.duty_u16(0)

while True:
    #Min BPM = 40 (1,5s) y Max BPM = 208 (0,28s)) 
    bpm = int((pot.read_u16() * (168 / 65535)) + 40)
    bps = 60 / bpm
    seconds = bps / 8
    print(f"BPS = {bps}")

    #Convertir seconds a BPM
    lcd.move_to(0, 0)
    print(f"BPM = {bpm}")
    lcd.putstr(f"BPM: {bpm} ")

    pines.reverse()
    #Encender luces de izquierda a derecha
    for pin in pines:
        led = Pin(pin, Pin.OUT)
        led.value(1)
        sleep(seconds)
        led.value(0)
    sonido(seconds)
    
    
  
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT