I'm using neopixel to control leds, when I run the code directly in the console,everything works fine, but when I upload main.py to the board,after the first boot it's not running as expected, then press the reboot button it just works, I don't know how to find out what's wrong with my code:
Code: Select all
import machine
import neopixel
import time
import random
# from ntptime import settime
brightSteps = 20
brightness = 3
pin = machine.Pin(14, machine.Pin.OUT) # D5
np = neopixel.NeoPixel(pin, 20, timing=True)
# 单个数字的初始颜色序列
blank_colors = [(0, 0, 0) for _ in range(20)]
def get_number_index(num):
"""获取点亮数字num的led下标"""
tmp = int(num / 2)
if num % 2 == 0:
return tmp, tmp + 10
else:
return 9 - tmp, 19 - tmp
def get_color_number(num, color):
"""获取指定颜色的数字序列"""
sequence = blank_colors[:]
led1, led2 = get_number_index(num)
sequence[led1] = color
sequence[led2] = color
return sequence
def show_single_number(num, color):
colors = get_color_number(num, color)
for i, c in enumerate(colors):
np[i] = c
np.write()
def clear():
for i, c in enumerate(blank_colors):
np[i] = c
np.write()
for i in range(10):
print(get_number_index(i))
def do_connect(ssid, passwd):
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(ssid, passwd)
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
start_loop()
#do_connect('_python_', 'py87654331')
delay = 0.01
cycle = 3
wait_mode = False
count = 0
app_running = False
def main():
global delay
global cycle
global wait_mode
global count
global app_running
if app_running:
return
else:
app_running = True
while True:
for i in range(10):
light = 50 + i * 10
show_single_number(i, (light, light, light))
time.sleep(delay)
if (count > cycle and random.randint(0, 9) == i) or delay > 0.01:
delay = delay * 1.2
if delay > 1.2:
wait_mode = True
break
clear()
count = count + 1
if wait_mode:
print(i)
time.sleep(5)
delay = 0.01
count = 0
wait_mode = False
main()