Lesson 3: Button Piano
Table of Contents
In this lesson, we will make our first interactive CPX program in MakeCode—a simple Button Piano—which makes sounds when we press the CPX’s built-in buttons.
Video Tutorial
Video. Creating a button piano. Here’s the full code and a link to the video on YouTube.
Code
Here’s the final code. Right-click on the code below and select “Open link in a new tab” to open it directly in the MakeCode editor.
Blocks
We use the following MakeCode blocks in this example. See the Adafruit MakeCode Reference guide.
Output
For output, we used Light and Music blocks, particularly:
- play tone plays a tone on the speaker for a set amount of time (set the beat length with tempo)
- set all pixel color sets all the NeoPixels to one color
- set volume sets the volume of the output speaker
Input
For Input blocks, we used:
- on button runs code segment when the button is clicked (or down, up, etc.)
Event
To ensure that our volume is properly set, we initialize it to 255 (the highest value) when the program first starts using the on start block
- on start runs once and only once when the program starts
Next Lesson
In the next lesson, we’ll create a light-responsive instrument!
Previous: How to use MakeCode Next: Light-Responsive Instrument