Lesson 3: Button Piano

Table of Contents

  1. Video Tutorial
  2. Code
  3. Blocks
    1. Output
    2. Input
    3. Event
  4. Next Lesson

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:

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


This website was developed by Professor Jon E. Froehlich and the Makeability Lab using Just the Docs. If you found the website useful or use it in your teaching, we'd love to hear from you: jonf@cs.uw.edu. This website and all code is open source (website GitHub, Arduino GitHub, p5js GitHub). You can find the MakeabilityLab_Arduino_Library here. Found an error? File a GitHub Issue.