import machine, neopixel
import utime
n = 5
p = 22
np = neopixel.NeoPixel(machine.Pin(p), n)
pins = [23, 22, 21, 19, 18, 5, 17, 16, 4, 0]
led_pins = [machine.Pin(pin, machine.Pin.OUT) for pin in pins]
ADC_MAX = 4095
adc = machine.ADC(machine.Pin(33))
adc.atten(machine.ADC.ATTN_11DB)
buzzer = machine.Pin(14, machine.Pin.OUT)
def map_range(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def can_play_buzzer(valor, antirrebote):
return valor > 3 and not antirrebote
def fill_np(n, color):
for led in range(n):
np[led] = color
np.write()
antirrebote_time = 500
antirrebote = False
valor = 0
last_buzzer_time = utime.ticks_ms()
while True:
value = adc.read()
valor = int(map_range(value, 0, ADC_MAX, 0, 5))
fill_np(n, (0, 0, 0))
fill_np(valor, (0, 255, 0))
if can_play_buzzer(valor, antirrebote):
print("Playing")
buzzer.on()
antirrebote = True
last_buzzer_time = utime.ticks_ms()
if utime.ticks_diff(utime.ticks_ms(), last_buzzer_time) >= antirrebote_time:
buzzer.off()
antirrebote = False
utime.sleep_ms(10)