from machine import Pin
import neopixel
import time
import random
button = Pin(18, Pin.IN)
pin = Pin(25, Pin.OUT)
np = neopixel.NeoPixel(pin, n=4, bpp=3, timing=1) #参数：端口,灯数,3元组RGB,800KHz速率
value=1     #亮度范围0.0-1.0,默认为1.0
def setup():           #重置所有RGB灯，熄灭
    for i in range(0,4):
        np[i] = (0,0,0)
    np.write()

def loop():
    while button.value() == 1:
        pass
    else:
        while True:
            for n in range(0,4):
                rgb_red = int(random.randint(0,255)*value) 
                rgb_green = int(random.randint(0,255)*value)
                rgb_blue = int(random.randint(0,255)*value)
                np[n] = (rgb_red,rgb_green,rgb_blue)
                np.write()
                time.sleep(0.05)
            #for n in range(0,4):
                np[n] = (0,0,0)
                np.write()
                time.sleep(0.05)
if __name__ == "__main__":
    setup()
    loop()