1

I am running a Timer periodically to read from multiple ultrasonic sensors. This is done in Micropython on a Raspberry Pi Pico W. The time this read takes is dependent on the distance of the nearest object to the sensor. The further away, the longer it takes.

In some cases the function takes longer than the period between two executions. If that's the case, the program is not reacting to Keyboard Interrupts.

How can that be fixed?

I tried setting a boolean flag and once the interrupt is sent, the flag is toggled and the executed function deinits the timer. This logic is also wrapped in a try-catch-finally-block:

running = True
timer = Timer()


def some_function():
    if running:
        time.sleep(0.05)
        print('other timed function')
        return
    timer.deinit()


def main():

    timer.init(period=50, mode=Timer.PERIODIC, callback=lambda t: some_function())


    while running:
        print('running')
        time.sleep(1)

try:
    main()
except:
    print('ctrl c')
    running = False
finally:
    print('finally')

But interrupts are completely ignored here. The above code was my approach to consistently simulate the situation where the program is not reacting:

...
other timed function
running
other timed function
Traceback (most recent call last):
  File "main.py", line 30, in <lambda>
  File "main.py", line 21, in some_other
KeyboardInterrupt: 
other timed function
...

So now I'm not able to stop the program at all.

I am using the VSCode Plugin Pico-W-Go and tried Ctrl-C and Ctrl-D. The plugin also provides a 'hard-reset' function which is not helping either.

Pressing the Reset-Button on the pico is not helping Refreshing the pico with micropython is not helping

Edit:

The only working solution the get the pico out of that 'loop' was to reset the flash with the .uf2 file provided here: https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html#resetting-flash-memory

ningelsohn
  • 81
  • 7

0 Answers0