Welcome to Building:bit Superkit repository
2.Music door
Music door
Music door1. Learning Objectives2. Building Blocks3. Servo Wiring4. Code analysis5. Write and download the program6. Experimental phenomenon
1. Learning Objectives
In this course, we mainly learn how to use Python programming to realize that the automatic door gradually opens and closes, the buzzer plays the song "Ode to Joy", and the RGB light switches to different colors.
2. Building Blocks
For the steps of building blocks, please refer to the installation drawings of [Assembly course]-[Auto-door] in the materials or the building block installation album.
3. Servo Wiring
The building block servo wiring is inserted into the S1 interface of the Super:bit expansion board, and the orange servo wiring is inserted into the yellow pin of S1.
As shown in the figure below:

! Notes:
When taking the course related to the building block servo for the first time, we need to remove the blue building block installed on the servo and upload the program of this course to the micro:bit; then turn on the power switch of the Super:bit expansion board and wait for the building block servo to turn to the initial position; then, we can turn off the power, adjust the security automatic door to the open state, as shown in the figure below, and then install the blue building block. (If you have used the security automatic door and servo related program before, you can skip this step)


4. Code analysis
For the program of this course, please see the Music door.py file.
from microbit import *import musicimport superbitimport microbitimport neopixelFirst, import the libraries needed for this lesson from microbit: the superbit library is dedicated to the superbit expansion board; the music library is used to play tones; the neopixel library is used to control RGB lights.
xxxxxxxxxxdisplay.show(Image.HAPPY)np = neopixel.NeoPixel(pin12, 4)superbit.servo270(superbit.S1, 90)display.show(Image.HEART) Dot matrix displays a heart pattern
superbit.servo270(superbit.S1, 90): Initialize the building block servo to rotate to about 90°, that is, open the door;
np = neopixel.NeoPixel(pin12, 4): RGB light initialization settings, a total of 4 RGB lights, connected to the P12 pin of the microbit motherboard (you can check the hardware interface manual);
xxxxxxxxxxwhile True:music.play('E4:4')superbit.servo270(superbit.S1, 70)superbit.motor_control(superbit.M1, 255, 0)superbit.motor_control(superbit.M3, 255, 0)np[0] = (255, 0, 0)np.show()...while True: infinite loop
music.play('E4:4') The buzzer plays music.
superbit.servo270(superbit.S1, 70) The servo of the S1 interface rotates to 70 degrees
superbit.motor_control(superbit.M1, 255, 0) The motor of the M1 interface rotates forward at the maximum speed
np[0] = (255, 0, 0) Set the first RGB light to red
np.show() Control the RGB light.
Loop in sequence.
5. 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 for indentation, and the last line ends with a blank program.
- Click the thumb 'Check' button to check whether 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, 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.
6. Experimental phenomenon
After the program is downloaded successfully, the micro:bit dot matrix will display a smiley face. Turn on the power switch, and we can see a smiley face pattern on the micro:bit dot matrix, the automatic door gradually opens and closes, the buzzer plays the song "Ode to Joy", and the RGB light switches to different colors.
If you need to restart, press the reset button on the back of the micro:bit motherboard.