STEP1: connect micro: bit board to computer with USB cable, plug the earphone in the expansion board; connect the motion sensor to pin P1 of the expansion board.
1. Drag “pin P0 play sound…until done” block from “micro: bit” module to script area. There are many built-in sounds to choose. We select the sound “dadadadum” here.
2. Run the program
1. Program to generate a piece of music of Twinkle, Twinkle, Little Star.
2. Drag “pin P0 play note-C2 for 1beat” from the “micro: bit” module to script area. Revise the parameter “C2” to play different notes, and change the parameter ”1” to adjust the beat.
3. Motion sensor has been connected to pin P1, if someone passes near the device, the value of P1 will be 1, otherwise, it will be 0. Therefore, combining it a “if then…else” statement, we can make the smart music box come to life.