Welcome to Building:bit Superkit repository
1.16 Control rocker
Control rocker
Control rocker1. Learning objectives2. Sensor wiring3. Code analysis4. Write and download the program5. Experimental phenomenon
1. Learning objectives
In this course, we mainly learn how to display the joystick status through Python programming.
2. Sensor wiring
The joystick module is connected to the P1P2 interface.

3. Code analysis
For the program of this course, please see the Control-rocker.py file.
from microbit import *import WOM_Sensor_KitFirst, import the library needed for this lesson from microbit: WOM_Sensor_Kit library is used for sensors.
xxxxxxxxxxup = Image('00900:' '09990:' '90909:' '00900:' '00900')down = Image('00900:' '00900:' '90909:' '09990:' '00900')right = Image('00900:' '00090:' '99999:' '00090:' '00900')left = Image('00900:' '09000:' '99999:' '09000:' '00900')Customize four direction patterns (up, down, left, right), each pattern uses 5×5 LED dot matrix to represent the direction arrow
xxxxxxxxxxwhile True:if WOM_Sensor_Kit.WOM_rocker(pin1, pin2, WOM_Sensor_Kit.WOM_up):display.show(up)sleep(100)elif WOM_Sensor_Kit.WOM_rocker(pin1, pin2, WOM_Sensor_Kit.WOM_down):display.show(down)sleep(100)elif WOM_Sensor_Kit.WOM_rocker(pin1, pin2, WOM_Sensor_Kit.WOM_right):display.show(right)sleep(100)elif WOM_Sensor_Kit.WOM_rocker(pin1, pin2, WOM_Sensor_Kit.WOM_left):display.show(left)sleep(100)Continuously detect the rocker module (connected to pin1 and pin2) direction, if up/down/left/right is detected, the corresponding direction icon will be displayed respectively, and there will be a short delay of 100 milliseconds after each pattern is displayed.
4. Write and download the program
- Open the Mu software and enter the code in the editing window. Note! All English and symbols should be entered in English mode, use the Tab key (tab key) for indentation, and the last line ends with a blank program.
- Click the thumb 'Check' button to check if there are any errors in our code. If a cursor or underline appears in a line, it means a syntax error, please check and modify it. If there is no error, the lower left corner will prompt that there is no problem with the detection.

- Click the 'REPL' button to check whether the Superbit library has been downloaded. If not, please refer to [Preparation before class] --> [2.4 Python Programming Guide].

- After the program is written, connect the computer and microbit mainboard with a microUSB data cable, please click the 'Flash' button to download the program to the micro:bit mainboard. (You need to click the 'REPL' button again to turn off the import library file function before you can download the program normally).

- If the download fails, please confirm whether the microbit is connected to the computer normally via the microUSB data cable and the Superbit Python library has been imported.
5. Experimental phenomenon
After the program runs successfully, shake the joystick, the dot matrix displays upward when the joystick is up, downward when the joystick is down, left when the joystick is left, and right when the joystick is right.