Advanced Lesson 3 DO RE MI

Share for us

Introduction

Simply controlling the lights is a little bit boring already, isn’t it?So in this lesson, we will use a new device, buzzer. Use different buttons to simulate different piano keys and press them to produce different tones. Let’s make a simple electronic piano.

Components

Circuit

Here we use three buttons to simulate the keys of piano to emit the notes of Do, Re and Mi. Each button should be equipped with a pull-down resistance, so that the default is 0 when the button is released; otherwise, the default changes into 1.

Programming Steps

Step 1

After creating the project, we define pins and store the read values of the components by setting variables. Then click Create variable button to create 8 variables: btn1, btn2, btn3, btn1Val, btn2Val, btn3Val and buz.

Step 2

Assign the values of pins D0-D2 to variables btn1, btn2 and btn3 respectively. Since D0-D2 are connected to buttons, their modes are set as input pull down. Then  assign the value of pin P0 to the variable buzzer.

Step 3

Create a function to control the buzzer. Drag a to do something block from the Functions category into the code area. And then add an input name: Note block. After that, we should name the function Beep.

Step 4

Place the set pwm P0 frequency to block and the set pwm P0 pulse width to 0% block from the Raspberry Pi category into the Beep block. And put a delay block after them. Then change the parameter as shown below.

Step 5

Write another function named Beep off to stop the buzzer.

Step 6

In the Forever block, we should assign the values of the three buttons to variables named btn1Val, btn2Val and btn3Val accordingly.

Step 7

Drag an if do block from the Logic category to below the set btn3Val to block. Then click the setting icon in the if do block. After that, add two else if blocks accordingly and place an else block below the if block.

Step 8

Next, drag the Comparison Operator ( = ) block to the area behind the if. Then put the btn1Val block from the Variables category to the left of Comparison Operator   ( = ) block. After that, change a number ( 0 ) block to 1 on the right.

Step 9

When the value of btn1Val is 1, it means that button 1 is pressed. Put the Beep block from the Functions category into the do, and the Middle C block from the Music category to the area on the right, then the buzzer will make a Do sound.

Step 10

Click on Duplicate after long pressing the Comparison Operator ( = ) block, then do it again. Put the copied blocks into two else if blocks respectively. After that, change the two btn1Val variables into btn2Val and btn3Val respectively.

Step 11

Do as you just did, copy the Beep block and put them into do which means that when the button 2 is pressed, the buzzer will make the sound, Re(Middle D); when the button 3 is pressed, the buzzer will make the sound Mi(MiddleE).

Step 12

Get the Beep off block from the Functions category then put it into the else block. When the buttons are not pressed, the buzzer does not sound.

Step 13

Remember to save the project and then flash it into the Raspberry Pi (refer to the lesson 0 for details). Press button 1, button 2 and button 3 respectively to let the buzzer make sounds Do, Re, Mi.

Fold Cardboard

Step 1

Take out the matching cardboard of lesson 3.

Step 2

Fold a cube like this, according to the creases.

Step 3

Build the circuit as follows. Please do NOT change the position of components optionally when building circuits, in case the card cannot be installed.

Step 4

Install the cube of cardboard on the circuit as shown.