import machine
import neopixel
import time
red =(128,0,0)
green =(0,128,0)#Turn off all the LEDs
def off(np):for i in range(np.n):
np[i]=(0,0,0)
np.write()
def evens_green(np):for i in range(np.n):if i %2==0:
np[i]= green
else:
np[i]= red
np.write()
def evens_red(np):for i in range(np.n):if i %2==0:
np[i]= red
else:
np[i]= green
np.write()
def alternate_colors(np):if np[0]== green:evens_red(np)else:evens_green(np)if __name__ =='__main__':#Data line is on pin 13#andthere are 12 LEDs on our NeoPixel ring
np = neopixel.NeoPixel(machine.Pin(13),8)for i in range(20):alternate_colors(np)
time.sleep(1)
# 关闭显示
off(np)#do_connect()#atthis point we should start a cycle of colors#andalso start listening for any webrepl or api commands
🌻效果示例二
import machine
import neopixel
import time
WIFI_SSID ="#######"
WIFI_PASS ="*******"
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)if not sta_if.isconnected():print('connecting to network...')
sta_if.active(True)
sta_if.connect(secrets.WIFI_SSID, secrets.WIFI_PASS)while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())#cycleone LED around the circle
def cycle(np):
n = np.n
for i in range(4* n):for j in range(n):
np[j]=(0,0,0)
np[i % n]=(68,125,67)
np.write()
time.sleep_ms(25)
def bounce_blue(np):
n = np.n
for i in range(4* n):for j in range(n):
np[j]=(0,0,128)if(i // n) % 2 == 0:
np[i % n]=(0,0,0)else:
np[n -1-(i % n)]=(0,0,0)
np.write()
time.sleep_ms(60)#allthe LEDs make a brighter/dimmer red with fad effect
def fade_red(np):
n = np.n
for i in range(0,4*256,8):for j in range(n):if(i // 256) % 2 == 0:
val = i &0xffelse:
val =255-(i &0xff)
np[j]=(val,0,0)
np.write()#Turn off all the LEDs
def off(np):
n = np.n
for i in range(n):
np[i]=(0,0,0)
np.write()#Make a nice red-orange glow
def fire(np, red=(240,0,0), orange=(240,120,0)):for i in range(np.n):if i %2==0:
np[i]= red
else:
np[i]= orange
np.write()#Calculate an orange based on the max and current brightness
def calc_orange(max_bright, current):return(max_bright - current,(max_bright - current)// 2 + 1, 0)
def flame(np):
max_brightness =120for f in range(4, max_brightness):#Start with bright orange and dim red
red =(f,0,0)
orange =calc_orange(max_brightness, f)fire(np, red, orange)for f in range(max_brightness,4,-1):#Now orange LEDs are dim and the red ones are bright, so switch:
red =(f,0,0)
orange =calc_orange(max_brightness, f)fire(np, red, orange)if __name__ =='__main__':#Data line is on pin 13#andthere are 8 LEDs on our NeoPixel ring
np = neopixel.NeoPixel(machine.Pin(13),8)do_connect()#atthis point we should start a cycle of colors#andalso start listening for any webrepl or api commandsfor i in range(3):flame(np)
time.sleep(1)for i in range(3):fade_red(np)
time.sleep(1)#Blue for i in range(3):bounce_blue(np)
time.sleep(1)for i in range(3):cycle(np)
time.sleep(1)