1

I have this code for a parking system. When the number of spaces goes above 20 it sends and error message due to 20 being the limit of spaces. I want to try shutdown the program after this point.

I have tried doing what you can do in Python. This is:

import sys
display.scroll("Error: Limit exceeded.")
sys.exit()

This gives me an attribute error.

from microbit import *
import sys

elif spaces > 20:

        display.scroll("Error: The spaces have exceeded the limit.")
        sys.exit()

This should shutdown the program, not letting it to function, after the elif statement. There is more code (if statements, loops, function) but it is irrelevant.

Thanks :)

Jeff
  • 11
  • 1

2 Answers2

3

There's a couple ways I can think of.

In general, you can just enter an infinite loop, which will effectively halt everything, if there's no way to interrupt the loop:

while True:
    microbit.sleep(1000000)  # wait for 1000 seconds until the end of time

In micro:bit's documentation there's also microbit.panic(), which, quote, "requires a restart" of the micro:bit:

microbit.panic(0)

You could see if that works for you.

And since the micro:bit uses MicroPython as its Python implementation, you can look here in the MicroPython documentation:

import pyb

pyb.stop() # stop CPU, waiting for external interrupt

However, if an external interrupt does occur (and one might), the program would then probably continue.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • 1
    `import pyb` is pyboard only code. It's only available in the pyboard version of micropython not in microbit – phil Apr 05 '19 at 09:47
  • @rhubarbdog Good to know. Glad I didn't suggest it as my first alternative :) – AKX Apr 05 '19 at 12:39
1

Your code snippet is a bit misleading as it must be in a while True: loop. Just breakout of that outer loop.

phil
  • 561
  • 3
  • 10