from machine import Pin, PWM
import time
import urandom as random
from neopixel import Neopixel
import led_transform
PIN = 13
NUMPIXELS = 28 # NeoPixel ring size
pixDELAY = 50 # Delay for pixel persistence
button_down = False
# 蜂鳴器接腳
buzzer = PWM(Pin(15))
pixels = Neopixel(64, 0, PIN, "GRB")
def function(index):
print(index)
if index <= 28:
pixels.set_pixel((led_transform.display1(index))-1, (255, 0, 0))
elif index <= 48:
index = index - 28
pixels.set_pixel((led_transform.display2(index))-1, (255, 0, 0))
else:
index = index - 48
pixels.set_pixel((led_transform.display3(index))-1, (255, 0, 0))
pixels.show()
# Function to handle button press interrupt (optional)
def button_press(pin):
global button_down
button_down = True
# Start Function
if __name__ == '__main__':
button_pin = Pin(21, Pin.IN, Pin.PULL_UP) # Internal pull-up resistor
# Attach interrupt to the push button pin
button_pin.irq(trigger=Pin.IRQ_FALLING, handler=button_press)
while True:
if button_down == True:
for x in range(1,61):
buzzer.freq(1000)
buzzer.duty_u16(3000)
function(x)
time.sleep_ms(pixDELAY)
buzzer.duty_u16(0)
pixels.fill((0, 0, 0))
time.sleep_ms(pixDELAY)
num = random.randint(1,60)
for x in range(1,num):
buzzer.duty_u16(3000)
function(x)
time.sleep_ms(pixDELAY)
buzzer.duty_u16(0)
pixels.fill((0, 0, 0))
time.sleep_ms(pixDELAY)
for count in range(5):
buzzer.duty_u16(3000)
function(num)
time.sleep_ms(100)
buzzer.duty_u16(0)
pixels.fill((0, 0, 0))
pixels.show()
time.sleep_ms(100)
buzzer.deinit()
button_down = False