0

我是 pico 的新手,以前只使用过 arduinos。我正在尝试制作一个简单的旋转编码器程序,该程序在 0.96 oled 显示器上显示 0-12 的值,并点亮条带上的许多 LED。我想尝试使用多个内核,因为当我让它们循环时,中断会使 LED 运行不顺畅(转动编码器时一切都会暂停)

然而,当我运行这个程序时,除了编码器有弹性之外,pico 可能会在运行程序 30 秒后崩溃,在显示器上弄得一团糟并停止代码。我觉得有一些我完全忽略的使用多个核心的规则。

这是代码:

from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import _thread
import utime
import neopixel

#general variables section
numOn = 0

#Encoder section
sw = Pin(12,Pin.IN,Pin.PULL_UP)
dt = Pin(11,Pin.IN)
clk = Pin(10,Pin.IN)
encodeCount = 0
lastClk = clk.value()
lastButton = False

#Encoder thread
def encoder(): #don't mind the indentation here, 
               #stackoverflow kinda messed up the code block a bit.
while True:
    #import stuff that I shouldn't need to according to tutorials but it doesn't work without
    global encodeCount
    global lastClk
    global clk
    import utime
    
    if clk.value() != lastClk:
        if dt.value() != clk.value():
            encodeCount += 1
        else:
            encodeCount -= 1
        if encodeCount > 12:
           encodeCount = 0
        elif(encodeCount < 0):
           encodeCount = 12
    lastClk = clk.value()
    print(encodeCount)
    utime.sleep(0.01)

_thread.start_new_thread(encoder,())

#LED section
numLed = 12
ledPin = 26
led = neopixel.NeoPixel(machine.Pin(ledPin),numLed)

#Screen Section
WIDTH = 128
HEIGHT = 64
i2c = I2C(0,scl=Pin(17),sda=Pin(16),freq=200000)
oled = SSD1306_I2C(WIDTH,HEIGHT,i2c)

#loop
while True:
    for i in range(numLed):
        led[i] = (0,0,0)
    
    for i in range(encodeCount):
        led[i] = (100,0,0)
    led.write()

    #Display section
    oled.fill(0)
    oled.text(f'numLed: {numOn}',0,0)
    oled.text(f'counter: {encodeCount}',0,40)
    oled.show()

我可能在这里做一些愚蠢的事情,我只是不知道是什么。此外,任何关于简单地去抖动编码器的建议都会非常有帮助。任何帮助,将不胜感激!谢谢!

更新:上面的代码使 pico 变砖了,所以很明显我做错了。_thread start line 阻止它再次崩溃,所以问题就在那里。

4

0 回答 0