1

I have just started with programming on a raspberry pi pico, and I was wondering if you can make python start another function, even though the other one is still running, so I want multiple functions to run at once (but with a bit of delay between them). What I have found out myself, is that there is an extension called "Threading" but I do not know how you can install that on the Pico. My current code:

The function:

def dimming(PinNumber):
    PinN = PWM(Pin(PinNumber))
    PinN.freq(1000)
    for duty in range(65025):
        PinN.duty_u16(duty)
        time.sleep(0.0001)
    for duty in range(65025, 0, -1):
        PinN.duty_u16(duty)
        time.sleep(0.0001)

The code that starts the function:

while True:  
    for i in range(6):
        dimming(i)
        time.sleep(0.3)
David Grayson
  • 84,103
  • 24
  • 152
  • 189
luuf
  • 29
  • 5
  • You probably have it installed already. Just look at the thread-related code in this other question: https://stackoverflow.com/questions/74669306/raspberry-pi-pico-w-micropython-threading-and-requests-code-stops-working But also in general I would recommend that you find another way to solve your problem without threads, because you probably don't need them and they introduce their own difficulties. – David Grayson Mar 04 '23 at 06:40
  • look into `asyncio` module for micropython – Lixas Mar 04 '23 at 19:17

2 Answers2

2

i have no experience with raspberry pi, however it would be possible to run multi function with threading lib that makes specific pipe lines for each process.

import threading

sow after declaring our functions, for example :

def func1 ():
   pass
def func2 ():
   pass

now we have to create thread lines :

th1 = threading.Thread(target=func1)
th2 = threading.Thread(target=func2)

in order to start processing :

th1.start()
th2.start()

end of process :

th1.join()
th2.join()

hope to be useful ...

0

You can actually go ahead and use multiprocessing library which is available in python to run two functions in parallel at a same time. You can follow the below syntax

from multiprocessing import Process

def func1():
    pass

def func2():
    pass

p1 = Process(target=func1)
p1.start()
p2 = Process(target=func2)
p2.start()
p1.join()
p2.join()
Darkness
  • 21
  • 4
  • I currently have the problem that the raspberry Pi doesn't detect the module named 'multiprocessing', I hope I can figure out how to use it on the Pi – luuf Mar 04 '23 at 08:16
  • @luuf I don't have much experience on using python on raspberry pi's, but however I came across this article on using multiprocessing on raspberry pi's. Hope this helps you with the task https://forums.raspberrypi.com/viewtopic.php?t=221505 – Darkness Mar 05 '23 at 07:39
  • Also can you share the error which you are getting related to 'raspberry Pi doesn't detect the module named 'multiprocessing'. – Darkness Mar 05 '23 at 07:50
  • This is a pico which is an embedded board. multiprocessing would do nothing for this product so it makes sense that it would not be available. I write a lot of Python for the raspberry pi computer and use both multiprocessing and threading. However, the pico is very different. I recommended studying both asyncio and threading. I don't have experience with asyncio but I know it is newer than the threading module. – Mark Mar 05 '23 at 17:09