Welcome to World of Module Sensor Kit repository
7.2 Rocker control light
1. Learning target
In this course, we will earn how to use Micro:bit, RGB light module and rocker module to realize gesture control light.
2. Preparation
Connect the module to Micro:bit board by Micro:bit expansion board, as shown below.
3. About code
# -*- coding: utf-8-*-# Encoding cookie added by Mu Editor
from microbit import *
import WOM_Sensor_Kit
display.off()
while True:
if WOM_Sensor_Kit.WOM_rocker(pin0, pin1, WOM_Sensor_Kit.WOM_up):
WOM_Sensor_Kit.WOM_rgb(1023, 1023, 1023)
elif WOM_Sensor_Kit.WOM_rocker(pin0, pin1, WOM_Sensor_Kit.WOM_down):
WOM_Sensor_Kit.WOM_rgb(0, 0, 1023)
elif WOM_Sensor_Kit.WOM_rocker(pin0, pin1, WOM_Sensor_Kit.WOM_left):
WOM_Sensor_Kit.WOM_rgb(1023, 0, 0)
elif WOM_Sensor_Kit.WOM_rocker(pin0, pin1, WOM_Sensor_Kit.WOM_right):
WOM_Sensor_Kit.WOM_rgb(0, 1023, 0)
Shake the joystick.
If the joystick is moved to the far left in the X direction, the RGB light will become red, move to the far right and the RGB light will become green, move in the Y direction to the bottom and the RGB light will become blue, and when it is moved to the top, the RGB light will become white.
4.Writing and download code
4.1 You should open the Mu software, and enter the code in the edit window, , as shown below.
Note! All English and symbols should be entered in English, use the Tab key (tab key) to indent and the last line must be a space.
4.2 You can click the “Check” button to check if our code has an error.
If a cursor or underline appears on a line, it indicates a syntax error, please check and modify. If there is no error in the program, the bottom left of the interface will prompt that there is no problem in detection.
4.3 Click the ‘REPL’ button to check whether the WOM_Sensor_Kit Python library has been downloaded.
If not, please refer to [Preparation before class] --> [Python Programming Guide] .
4.4 After the program is written, use a micro USB cable to connect the computer and the micro:bit board. Please click the ‘Flash’ button to download the program to the micro:bit motherboard (You need to click the ‘REPL’ button again to close the function of importing library files before you download the program).
4.5 If the download failed, please confirm whether the micro:bit is connected to the computer through the micro USB data cable, and confirm whether the WOM_Sensor_Kit Python library has been imported.
6. Phenomenon
After the program is downloaded successfully. We can control the RGB light by shaking the rocker. If the rocker moves to the left-most in the X direction, the RGB light become red;
If the rocker moves to the right-most in the X direction, the RGB light become green;
If the rocker moves to the up-most in the Y direction, the RGB light become blue;
If the rocker moves to the down-most in the Y direction, the RGB light become white.