5. Selection in the Event Loop
We can do more interesting things if we combine what we know with the event loop and selection.
5.1 Selection with the Buttons
We can run selection statements inside the event loop to get the micro:bit to respond to events. In the below example the micro:bit will keep checking for button A to be pressed. When button A is pressed it will play a beep sound. Remember that the loop runs continuously from the moment the program starts:
1 2 3 4 5 6 7 8 9 | |
Note
Here the micro:bit is constantly checking button A. When it detects a press it will execute the indented code underneath.
music.pitch(440, 200) plays a tone at 440 Hz (the note A4) for 200 milliseconds. You can change the number to get a higher or lower pitch.
Notice the indentation: the if statement is indented to indicate that it belongs to the while loop. The music.pitch() and sleep() calls are indented again to indicate that they belong to the if statement.
This indentation is very important in Python as it dictates the structure of the code/program.
5.2 Selection with Indentation
1 2 3 4 5 6 7 8 9 | |
Note
With a slight change to the program, we see completely different behaviour from the micro:bit.
In this case the micro:bit will beep all the time because the last two lines (music.pitch() and sleep()) belong to the while loop, not the if statement. This means that on every loop / iteration those lines will always run.
5.3 More Interactions
Obstacle Detection (Distance Sensor)
The micro:bit can read analog signals from its pins. Below is an example using an IR distance sensor connected to pin 1 — a low reading means something is close:
1 2 3 4 5 6 7 8 9 | |
Note
Connect an IR distance sensor to pin 1 and GND/3V on the edge connector. pin1.read_analog() returns a value from 0 to 1023. A low value means an obstacle is close. Inside the loop we check the sensor value every single iteration.
Before you run this program, have a guess at what the micro:bit does when running it.
Clap / Sound Sensing (micro:bit V2)
1 2 3 4 5 6 7 8 9 10 11 12 | |
Note
microphone.set_threshold(SoundEvent.LOUD, 150) sets the sensitivity — higher values mean only very loud sounds will trigger it. microphone.was_event(SoundEvent.LOUD) checks whether a loud sound occurred since the last time it was called, and clears the history each time — similar to resetting the sensor each loop.
Note: The built-in microphone is only available on the micro:bit V2.
Light Levels
1 2 3 4 5 6 7 8 9 10 11 12 | |
Note
The micro:bit uses its LED display to read ambient light levels via display.read_light_level(), which returns a value from 0 (dark) to 255 (bright). The threshold value can be adjusted to suit your environment.
Unlike the Edison which has separate left and right light sensors, the micro:bit has a single combined light sensor through the display. Two different pitches could be used to simulate different responses — try changing the frequency passed to music.pitch().
Random Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |