How to exit Pico program and run another

I have several programs which are quite large and would lke to do this:

Run a main program and depending on a sensor settings or a menu option, close the main program and auto run another. Within that 2nd program, depending on GPIO inputs or a sensors, close it and open another program, and so on, such that if in each of the sub-programs you quit, it returns back to the main master program.

The question I am asking is how programmatically you can close a running progrom and open/run another.

If I can do this the size of the overall program structure can be quite large, exspecially when using an SD card for storage of the multiple programs and data.

If the solution is already in the forum, apologies as I must have missed it.

Thanks for looking

One thing to note: on a micro controller you only have single program that has full control over everything. If this single program exits then you have to reset the device.

So independently of which programming language you use, you have this one program in charge of running all code. If it is implemented in C/C++, it would call various functions, but all of these are linked into the program at compile time and cannot be stored on an SD.

With (Micro) Python you have some more freedom. The single program running on the MCU is Python, but since it is an interpreter, it can start arbitrary scripts which could also be stored on an SD.

So assuming you are running MicroPython, your main (toplevel) script would look like this:

# query program to run, e.g. foo (see below)
...
# load run-method from foo and start it
from foo import run
run()

Your “individual programs” would be stored in files foo.py, bar.py etc. and in each of these files you would have a def run(): ... method.

At the end of each of these run-methods, you would store the name of the next program to run in NVRAM (non-volatile memory) and then call machine.reset(). This will clean up any ressources (e.g. memory and so on). At the start of your top-level program, you would read NVRAM for the name of the program to start next.

You should read the MicroPython docs to learn about machine and about NVRAM.

Final notes: with CircuitPython this is much simpler, since there is an API for it. With C/C++ you would also use the NVRAM-trick but you would have to link all functions to run into the program at compile time. Python is more flexible in this respect.

Thanks @bablokb for your explanation of the principles. I should have mentioned I am using Pimoroni Micropython for the Pico W, v1.22.2

I am have difficulty understanding what to practically do. As a very simple short test I have 3 small programs, called main.py, prog1.py and prog2.py, where they each flash an LED 3 times, stop then auto run the next program, eg

main.py

print ('in main boot up program for 3 counts')
for i in range (0, 3):
    setrgbled(BLUE)
    utime.sleep(1)
    setrgbled(BLACK)
    utime.sleep(1)

At this point exit the above program and run the following prog1

prog1.py

print ('in prog 1 for 3 counts')
for i in range (0, 3):
    setrgbled(RED)
    utime.sleep(1)
    setrgbled(BLACK)
    utime.sleep(1)

At this point exit the above program and run the following prog2

prog2.py

print ('in prog 2 for 3 counts')
for i in range (0, 3):
    setrgbled(GREEN) # different colour
    utime.sleep(1)
    setrgbled(BLACK)
    utime.sleep(1)

Now end this prog2 and start another program, eg prog3

I hope you can let me know what to add to the above simple examples to clarify things.

Many thanks, Steve

Your main.py is actually your prog0.py. Wrap the code within each of these files in a method definition:

def run():
  print('in xxx....')
  for i in range ...

In your main.py, you would then code:

from prog0 import run
run()
from prog1 import run
run()
from prog2 import run
run()

(you can name the method names like you wish and they can be different, this is not relevant, (e.g. run0, run1, run2) as long as you also change your main.py).
This will execute each script one after the next.

I would recommend that you search for a Python-tutorial, this is all basic Python stuff and spending some time learning about Python, especially its module and program structure, will save you a lot of time when it comes to implementing your ideas.

Thanks @bablokb - I have followed what you said and it is now up and running. I have only used Python since swapping over from Microchip (using asm and C on them from 1990) to using Pico W devices. I got all the Micropython books from Raspberry Pi and did not come across the solution so, as you suggest, I need to find some other books!

Maybe you want to try this: Python Tutorial This is a online tutorial and has the benefit that you have a small integrated editor where you can try things.

BTW: I am still using PICs today. I do prototyping on the Pico and if I really need it small and efficient I port it to the PIC. Mainly using 12F675 and 12F1840 (in SO-8). The larger PICs are not interesting any more, since the Pico is so cheap and much more powerful.