Modules are files that typically contain Python functions, methods, and properties. After your script imports a module, it can use the functions, methods and properties it contains. For example, the continuity_tester script has this statement: from microbit import * which gives the script access to anything from the microbit module. Without it, the script would not be able to call sleep(50). The script also needs the microbit module for its display.show method and Image.YES property.
# continuity_tester from microbit import * from multimeter import * import music while True: connected = continuity() if connected == True: display.show(Image.YES) #music.pitch(3000, 100) else: display.show(NC) sleep(50)
The continuity_tester script also uses a function named continuity. That function is from a custom module called multimeter. A custom module is different because the micro:bit module’s file system needs the multimeter.py file module before these statements can work properly: from multimeter import * and connected = continuity(). In this activity, the multimeter.py file was already added to continuity_tester.hex.
See Add Modules to your micro:bit for an example of how custom modules are added to a .hex file. In that example, the cyberbot module is used, but the steps of adding the multimeter.py module are similar.You can also download and view the multimeter.py by clicking these items in the python.microbit.org editor:
- Load/Save button
- Show Files dropdown
- Download arrow by multimeter.py.
To view its code, drag it into the python.microbit.org editor. Keep in mind that this code is not a script and so cannot run on its own. It has to be imported into your script, and then your script calls its functions.
Take a closer look at the while True: loop. The multimeter module has a function named continuity(), which returns True if it detects an electrical connection, or False if it does not. In connected = continuity(), the variable connected stores the True/False result continuity returns. When connected stores True , the statements in if connected == True makes the display show a checkmark with display.show(Image.YES). Otherwise, statements in the else block display the not-connected circle with a slash with display.show(NC). Note: NC is property of the multimeter module.
while True: connected = continuity() if connected == True: display.show(Image.YES) #music.pitch(3000, 100) else: display.show(NC) sleep(50)