Hello All
Newbie question:
Running a Pico with MicroPython (Thonny on my PC). I wrote three programs. I am running two sets of servos, each with different timings. The third program "calls" the first two and starts them through a momentary switch. At the end of the sequence, I've used the sys.exit() command to stop the program (I don't want it to loop). While connected to the PC and in Thonny, the program works beautifully. All servos move as they should, and it does MPY: soft reboot at the end. All well and good. I am very happy with it.
As I am running four servos, I am powering the servos through an external power source, and this works well also
So, what's the problem? I want the Pico to run from the same power source as the servos, so when I switch on the power, the program runs, then shuts down at the end (just like it does while running from Thonny on the PC) Problem is that when I unplug the program cable to the PC, then use the VSYS to power the Pico, it starts the program, and it runs fine, but it does not stop at the end, it loops. I don't understand why it does not loop while running from Thonny, but it does loop while running from VSYS
This is a guess based on my knowledge of how microPython and thonny work so could be wrong.
When running from thonny, the code is pushed into RAM on the Pico which then gets erased when the Pico resets.
When running without thonny, code is read from the onboard EEPROM at start up, compiled into byte code then run. If there is a boot.py and/or a main.py in the microPython filesystem they are run (in that order) on every (re)boot. This is by design otherwise you'd not be able to run anything.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.
All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides
Thank You, that makes sense. It sounds like I need to put the pico into a halt state. I'll work on it and let you know how it goes.
BTW- Really enjoying this learning process. My first foray into Python
So, what's the problem? I want the Pico to run from the same power source as the servos, so when I switch on the power, the program runs, then shuts down at the end (just like it does while running from Thonny on the PC) Problem is that when I unplug the program cable to the PC, then use the VSYS to power the Pico, it starts the program, and it runs fine, but it does not stop at the end, it loops. I don't understand why it does not loop while running from Thonny, but it does loop while running from VSYS uPython expects to run BOOT.PY then MAIN.PY. Not sure how you are set up but utilizing these could help. Here's a good illustration https://techexplorations.com/guides/esp ... m-at-boot/
I presume, you've already looked at the difference between soft and hard resets https://docs.micropython.org/en/latest/ ... boot.html# .
According to -
https://docs.micropython.org/en/latest/library/sys.html#sys.exit wrote: sys.exit(retval=0, /)
Terminate current program with a given exit code. Underlyingly, this function raises a SystemExit exception. If an argument is given, its value given as an argument to SystemExit.
On embedded ports (i.e. all ports but Windows and Unix), an unhandled SystemExit currently causes a Soft Reset of MicroPython. That soft reset executes 'main.py' before reaching the REPL -

Code: Select all

>>> import sys
>>> sys.exit()
MPY: soft reboot
In 'main.py'
MicroPython v1.25.0-preview.180.g495ce91ca.dirty on 2025-01-11; Raspberry Pi Pico W with RP2040
Type "help()" for more information.
So it would seem to depend on where the code is stored and how it is executed.
A Thonny 'run' will load the source to RAM and run it, will return to the REPL if there is no 'main.py' on the device preventing that.
When there is a 'main.py' that will prevent the REPL being reached, will re-run 'main.py' whenever 'sys.exit' is executed.
This will allow 'run once when powered-on, exit to REPL on sys.exit' functionality.
main.py

Code: Select all

print("In 'main.py'")
  import yourcode
except SystemExit:
  print("Caught 'sys.exit'")
print("Exit 'main.py'")
yourcode.py

Code: Select all

print("In 'yourcode.py'")
import time
import sys
for n in range(1, 100):
  print(n)
  if n == 4:
    print("Execute 'sys.exit'")
    sys.exit()
  time.sleep(1)

Code: Select all

>>> Ctrl-D
MPY: soft reboot
In 'main.py'
In 'yourcode.py'
Execute 'sys.exit'
Caught 'sys.exit'
Exit 'main.py'
MicroPython v1.25.0-preview.180.g495ce91ca.dirty on 2025-01-11; Raspberry Pi Pico W with RP2040
Type "help()" for more information.
You will need to power-cycle to make it do the same again.
An alternative to doing it this way is to not call 'sys.exit' just let the program which does things to fall off its end. For example -
main.py

Code: Select all

print("In 'main.py'")
import time
for n in range(1, 5):
  print(n)
  time.sleep(1)
print("Leave 'main.py'")

Code: Select all

>>> Ctrl-D
MPY: soft reboot
In 'main.py'
Exit 'main.py'
MicroPython v1.25.0-preview.180.g495ce91ca.dirty on 2025-01-11; Raspberry Pi Pico W with RP2040
Type "help()" for more information.
					
I have been pulling my hair out for weeks trying to figure out why my code kept looping when run on batteries. The approach you described above worked for me! Thank you!!
print("In 'main.py'")
import yourcode
except SystemExit:
print("Caught 'sys.exit'")
print("Exit 'main.py'")