In the example below the green led (Pico onboard one) flashes 8 times a second (with a timer) while the other two (external leds connected to pins 0 and 1 with 330ohm resistors) both flash 4 times a second: one is running in a thread while the other one runs in the main.
When the program is run, the Pico freezes after a few seconds. Interestingly enough...
a) if you use the SAME constant for both the THREAD run led_white and the MAIN run led_red, (eg 0.25 and 0.25 or 1.0 and 1.0) the Pico ALWAYS fails as long as the constant is the same.
b) if you use TWO DIFFERENT constants (say 0.25 and 0.24, or 0.50 and 1.00) the Pico NEVER fails.
c) the same is true if you use import sleep_ms() from utime and specify delays in milliseconds
The issue seems to be with the sleep() function. I don't know the root cause but will raise that as an issue on the github forum
As a workaround for the problem, one can use 2 different but close constants (say 250 and 251 in milliseconds).
But the issue remains. Opinions, ideas, analysis ????
jmb
Code: Select all
# Using Thonny 3.3.5 on Windows PC with Pico connected to PC USB port
# tested on Pico running 2 different uf2 versions (same results)
# rp2-pico-20210305-unstable-v1.14-83-g680ce4532.uf2 AND
# rp2-pico-20210222-unstable-v1.14-80-g75db0b907.uf2
from machine import Pin, Timer
from time import sleep
import _thread
led_green = Pin(25, Pin.OUT)
led_green.off()
led_red = Pin(0, Pin.OUT)
led_red.off()
led_white = Pin(1, Pin.OUT)
led_white.off()
tim1 = Timer()
def blink_green(timer):
led_green.toggle()
#
tim1.init(freq=8, mode=Timer.PERIODIC, callback=blink_green)
#
def blink_white(delay):
while True:
led_white.toggle()
sleep(delay)
#
_thread.start_new_thread(blink_white, (1.00,))
#
def blink_red():
while True:
led_red.toggle()
sleep(1.00)
#
blink_red()