Smart Piano | UNIHIKER Tutorial: Learn Python with Graphical Programming for Beginners 08
Have you ever had a musical dream? Have you ever imagined playing the piano as gracefully as a pianist? Piano, the "King of Musical Instruments", interprets perfect sound effects and romantic feelings with fluent notes, bringing pure enjoyment to people. But for various reasons, maybe you have never studied piano or failed to own a piano.
Now with the UNIHIKER, we can make a unique piano for ourselves and realize our musical dream.
Goal
Display seven basic musical notes of a piano on the UNIHIKER screen, with a note attached to each key, and the music sheet above the keyboard moves automatically during playing.
Get to Know
1. What is a buzzer?
2. How to use a buzzer?
Preparations
Hardware:
Software: Mind+
Download Address: https://mindplus.cc/
Hands-on Practice
The main task in this project is to display seven basic musical notes on the UNIHIKER screen, and then assign a buzzer note to each key. Press the piano keys on UNIHIKER to play different notes. And there will be a music sheet above the keyboard, which can be automatically moved. The smart piano project will be divided into the following two tasks.
Task 1: Design piano keyboard layout
Design a layout for the piano keyboard with the pictures in the resources file and set a buzzer note for each key.
Task 2: Display sheet music
Display the sheet music above the keyboard, and make it automatically move when playing.
Task 1: Design piano keyboard layout
1. Hardware Connection
Connect UNIHIKER to the computer via a USB cable.
2. Software Preparation
Open Mind+, and complete the software preparation according to the figure below.
3. Write a Program
Before start writing the program, let's analyze what functions need to be implemented in task 1 exactly. First, we have to display the basic musical notes 1-7 in the left part of the UNIHIKER, and then assign a note to each key.
(1)Determine the size of the piano key
Load the images of piano keys in the project.
The screen height of UNIHIKER is 320. To place 7 identical keys, the height of each key is about 45, and there should be a gap of 1 between each key. Therefore, it is more reasonable to set the height of the piano keys to 44.
(2)Determine the position of the piano keys
The seven keys are of the same size, and they should be placed one by one from top to bottom with a gap of 1 between them. To achieve that, the x coordinate of the second should remain unchanged, and its y coordinate is equal to the sum of the value on the y-axis of the first key (3), the key height (44), and the gap distance (1), which means the coordinates of the second key are (0, 48).
Similarly, the y-coordinate of the third piano key should be the sum of the y-coordinate of the second key, the key height(44), and the gap distance(1), which is 48+44+1=93. Thus, the coordinates of the third key will be (0, 93).
In the same way, the x-coordinate of the next piano key remains unchanged, and the number on its y-coordinate equals the sum of the y-coordinate of the current key, the key height 44 and the gap1.
(3)Set musical note
Since the object to be triggered is the key when pressing the piano key, we need to set a callback Function for every piano key object, in which the block The Click callback function of the object name is button_click1 will be used. Please note that The Click callback function of the object name is button_click1 should be used with When the click callback function is triggered, and their name should be kept consistent.
How can we make the piano play the corresponding note when pressing down a key? Here we must use the buzzer-related block Play note beat.
Note: for more information about buzzers, please refer to the Knowledge Base.
This block provides musical notes in three pitches and 8 beats, which can be found in the related drop-down box and should be used in the way below:
We can select a beat according to the beat in the music score or your preferences. Take the “Twinkle, twinkle little star” as an example, it comes with a 4/4 beat, which means four beats should be played in a measure. Since there are four notes in one beat, one note corresponds to 1/4 beat. So, we select 1/4 here.
Now let’s set notes for piano keys! Since it only makes sounds when the key is pressed, we need to add Play note beat commands in the key’s callback function. The complete program is as follows:
4. Run the Program
Click Run and see the display effect on the UNIHIKER screen, and then try playing the“Twinkle, twinkle little star” on it!
Task 2: Display the sheet music
1. Write a Program
It’s hard to play piano without reading music. So here we are going to display the sheet music above the piano keyboard on the UINIHIKER screen and make it move automatically in playing.
(1)Display the sheet music
Load the sheet music images into the project.
Use the Object name shows image at x y block to display the image at the position(160, 300) on the UNIHIKER screen. The piano key width is about 160, so the width for the sheet music should not exceed 80. Here we set the width of the sheet music image to 80 using the command The number parameter of the update object name is.
(2)Make the sheet music move
The movement of the sheet music is actually to make its image move upward repeatedly. Do you still remember how to move an image upward? Yes, we need to use a variable img_y command to change the y-coordinate of the image.
To make the image move up continuously, we can put its movement program into a forever loop.
The complete program is shown below:
2. Run the Program
Click to run the program. Then you can start playing while reading the sheet music on the UNIHIKER screen!
3. Have a Try
When the sheet music moves to the end, if we want to play it again, we have to re-upload the program, which is so inconvenient. Can we add a sheet music button on the screen that enables the sheet music to reappear so we can press the button and then start playing from the beginning again?
Knowledge Base
1. Buzzer
The UNIHIKER comes with an onboard buzzer so it can make sounds. Simply input, it carries an electronic component that can make sounds. We can control it to make various sounds by coding.
Why can the buzzer on the UNIHIKER make different sounds, while some buzzers can only produce one kind of sound? Buzzers are divided into active buzzers and passive buzzers. Let's get to know the difference between them.
Active buzzer
The key difference between active buzzers and passive buzzers is the requirements of the input signal are different. The active buzzer has a built-in oscillating source, so it will beep as long as it is electrified, but it can only beep with a fixed frequency.
Passive buzzer
The passive buzzer does not have an internal oscillating source, and it cannot produce sound with DC power. A passive buzzer should be driven with square wave signals.
Do you know what a square wave signal is?
Let’s do an experience like this. Get a ruler, fix one end of the ruler on the table with your hand, then use the other hand to press down the ruler, and release. During the process, you may find that with different pressing strengths, the vibration frequency and sound of the ruler are also different. We can understand the strength of pressing down the ruler as the square wave signal input in the passive buzzer. The output sound varies with different input square wave signals.
Now have you figured out why the buzzer on UNIHIKER can produce different sounds? The buzzer on UNIHIKER is a passive buzzer. In the program, when we use play note beat to select different keys, we set different square wave signal inputs for the buzzer, so the buzzer can output various notes.
2. Command Learning
Challenge
Some sheet music may involve the symbols "#" and "b". Notes with "#" and "b" added in the sheet music represent that the note is changeable. "#" indicates that the note is to be played higher in pitch, and the symbol “b” indicates that the note is to be played lower in pitch.
In a piano, the white keys are used to play the basic notes, and the black keys are used to play the variations. The corresponding relationship between the changeable notes and the black keys is as follows:
To play sheet music with variable notes, we need to add the black keys to the keyboard on UNIHIKER. How can we achieve that and make the buzzer produce changeable sounds?
Tip 1: Draw black and white piano keys using the way of drawing rectangles.
Tip 2: Click on the black keys in the block Play note beat to select variable notes.
Table of Contents for this Tutorial Series:
03 Journey to the West Stage Play
12 Vital Capacity Data Visualization