Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4810

MicroPython • How to blink a LED in background without delay the thread.

$
0
0
Hi,
I write a program with micropython,I use two threads,and I want insert each thread with a LED blink function.
But I don't want to delay the thread,such as "led.value(1) time.sleep(0.2) led.value(0)",this will cause my program miss some data to receive.My wish is the core send a command then the led blinks,my thread will not delay or delay just few ms.But I have no idea how to do.
So I read the SDK documents, and it has an example blink LED with timer.As the follow pic:
Screenshot 2024-12-17 230902.png
My question is I don't want my LED blink in background forever,I just hope to blink it to indicate something finished,so just blink once or twice that is enough.
It seems I should use one shot of the timer mode,as below:
# one shot firing after 1000ms
tim.init(mode=Timer.ONE_SHOT, period=1000, callback=mycallback)

https://docs.micropython.org/en/latest/ ... imer.html
But how to use this in each thread?
insert the tim.init() in the place I want to blink?then deinit() it and init() again? It seems not elegance.
Hope for some guidelines .thanks.

Statistics: Posted by keepzen — Tue Dec 17, 2024 3:17 pm



Viewing all articles
Browse latest Browse all 4810

Trending Articles