How can I use the interface I built to play melodies that I create?
Think About
How can I make sound with a microcontroller?
How can I program melodies in the Arduino IDE?
What do I want my instrument to do?
Get Started
Watch the following video that shows you how to understand the Arduino IDE. Directions are available below in text:
- Let’s learn a little more about the Arduino Sketches. There are three main parts of an Arduino Sketch
- Now, let’s examine our music_output sketch more closely to understand what it’s all about.
- NOTE: Return to the last XP if you do not have the music_output sketch loaded on your microcontroller
Go Further
Watch the following video that shows you how to play with code to make your own melodies in the Arduino IDE. Directions are below in text:
You can play with the code in the void loop section of the music_output sketch.
- Change the numbers after the word “speaker”.
- These numbers represent frequency in hertz.
- You don’t have to know what that means to try new numbers.
What happens if you type in a higher number?
What happens when you type in a lower number?
- Remember to upload your code to your microcontroller every time you make a change to the sketch.
- You can also change the length of your delays.
What happens when you type in a lower number?
What happens when you type in a higher number?
What if all of your delays are different lengths (different numbers)?
How can you make your melody longer?
- Try adding a new line of code that says
tone(speaker, );
delay( );
Fill in the blanks with your own numbers.
Remember to upload your code to your microcontroller every time you make a change to the sketch.
Go Even Further
Watch this video that shows you how to program music with note names in your Arduino IDE. Directions are also available below as text:
In this sketch, I’ve pasted in some variables that are helpful to people who are used to thinking about notes using letter names: A, B, C, D, E, F, G.
The new variables are named after the letter names (A, B, C, D, E, F, G).
I looked up the frequencies measured in hertz of these notes online and typed in them in here.
- Now in the void loop section, try typing some letter names of the notes (you don’t need to use any numbers).
Remember to upload your code to your microcontroller every time you make a change to the sketch.
Share What You Learned
Upload a recording or short video of your microcontroller playing the melody you coded and uploaded to the microcontroller.