We already have sounds in our racing game that our buzzer generates.
Buzzers can generate very simple sounds. But playing MP3 files instead of using the buzzer will significantly upgrade our race experience!
Let’s start!
Step 1: Make sure the MP3 player is disconnected
Step 2: Open a new file
First, we will learn how the MP3 Player works, then add it to the racing game later in this lesson.
As shown in the video below, create a new file and add the PlayRobotics and pr-advnaced extensions.
After adding the extensions, a new MP3 menu will appear.
Update your code in the same way as the video below, then upload the code.
You will not hear any sound yet, but you will in step 4!
We now have code telling Arduino to set up the MP3 player, set the volume to 20 (30 is maximum), and play file #1.
Since the MP3 player is disconnected, you will not hear anything yet.
Connect the cable like in the video below.
Once the cable is connected, you should hear a sound every time you restart Arduino (try this by pressing the restart button).
The MP3 player has an SD card inside it (more about this later) that we have loaded with two MP3 files.
So far, we have played the first file only. Now change your code (according to the image below) to play the second file.
Remember! You will need to disconnect the MP3 player cable before uploading the code and reconnect after the code is uploaded (restart Arduino after reconnecting to hear the sounds).
We added the wait 4 seconds block because we want to wait for the first file to finish playing before playing the second one.
Step 6: Download files
We have prepared a few racetrack-related narration sounds that you can download and use.
Download the files and save them to your computer:
Step 7: Copy beep file to SD card
Now remove the SD card from the MP3 Player. Then insert it into the USB SD card reader (included in the kit):
You should see the two files we uploaded on the SD card:
Now, copy the file called 8-jing (you should have download it in the previous step).
Remove the SD card from the reader and place it back into the MP3 player.
Update the code according to the image below, then disconnect the MP3 cable and upload the code. Once the code uploads, reconnect the cable.
Important! The Play File block has a number parameter. This parameter represents the order of the file on the SD card, not the name of the file!
The first file you copy to the SD card will be assigned the number 1—the second file, number 2, and so on. The name of the file does not matter.
Try playing different MP3s from the files you downloaded earlier in this lesson.
Don’t forget to place the SD card back in the MP3 Player and reconnect the cable after uploading the code.
Adding MP3 sounds to the race game
Now you are familiar with the MP3 player!
We have prepared the final game code that includes MP3 sounds, so you can download it below and upgrade your racing game!
We finished our racing game course! But you are welcome to use your knowledge to customize your racing game.
Here are some ideas:
1) You can control the volume using the Set MP3 Player Volume block
2) You can customize the code and use the MP3 files you downloaded in this lesson in the game. Copy the files you would like to play to the SD card and use the Play File block to play them during the game.
3) You can also record your very own narration! Save your narration as MP3 files, uploaded them to the SD card, then play them using the Play File block in your game!
On Youtube, you can find many guides that will help you to record your own MP3 files:
https://www.youtube.com/results?search_query=+how+to+record+sound+mp3+file
Thank you for being with us on this exciting journey, and we hope that you had fun during those lessons!
PlayRobotics Team