In this lesson, we are going to make a music box to listen to for Elise.
Connect a buzzer to P1 by using a triansistor, and program the buzzer to play for Elise.
After creating the project, click Variables category to create a variable, buz and then set buz to P1 that is changed from P0.
Drag a to do something block from the Functions category into the code area then add 2 input name x blocks. After that, name the function Beep and change the 2 x to Note and Beat respectively.
Under PWM subclass of Raspberry Pi, find a set pwm P0 frequency to block and 2 set pwm P0 pulse width to 0% block. Place them in the Beep function block and put the buz block into the the slot of pin.
Place two delay blocks behind the two set pwm buz pulse width to 0% blocks. In Variables category, find Beat block and Note block. Put the Beat block on the right of the delay block and the Note in the set pwm buz frequency toblock.
Since we have several repeated notes in the music, to keep the code as short as possible, we create two more functions to store the repeated notes. Create 2 functions and name them Music1 and Music2.
Find some Beep blocks in Functions category and put them into Music1 as follows. Drag the blocks, Note and Beat to proper position and change their values as shown.
The contents of Music2 are as follows:
Find two Music functions you just created in the Functions category and place them in the Forever block.
Place 3 Beep blocks below Music2 to write music for non-repeating sections.
Place the blocks, Music1 and Music2 repeatedly.
Put three Beep blocks below Music2 and write the rest notes of the music.
Save the project and flash it into the Raspberry Pi (refer to lesson 0 for details). Now listen to the music, for Elise.
Get the matching cardboard of this lesson.
Refer to the creases and fold the cardboard into the cube as shown.
Build the circuit as shown in the picture below. Please do NOT change the position of components optionally when building circuits, in case the card cannot be installed.
Get the cube of cardboard mounted on the circuit as shown.