| layout | default |
|---|---|
| title | L3: Button Piano |
| description | Build your first interactive CPX program in MakeCode: a simple button piano that plays tones and lights NeoPixels using the board's built-in A and B buttons, speaker, and button events. |
| image | https://img.youtube.com/vi/wCSWP6PhNvY/hqdefault.jpg |
| parent | Circuit Playground Express |
| has_toc | true |
| comments | true |
| nav_exclude | false |
| usetocbot | true |
{: .no_toc }
{: .no_toc .text-delta }
- TOC {:toc}
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.
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.
We use the following MakeCode blocks in this example. See the Adafruit MakeCode Reference guide.
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
For Input blocks, we used:
- on button runs code segment when the button is clicked (or down, up, etc.)
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
In the next lesson, we'll create a light-responsive instrument!