Skip to content

Latest commit

 

History

History
110 lines (74 loc) · 7.16 KB

File metadata and controls

110 lines (74 loc) · 7.16 KB
layout default
title L4: Light-Level Instrument
description Build an interactive CPX instrument in MakeCode that turns light levels into sound and color, using the onboard light sensor, speaker, and NeoPixels to make a playful light theremin.
image https://img.youtube.com/vi/RlEPQqyQGEk/hqdefault.jpg
parent Circuit Playground Express
has_toc true
comments true
nav_exclude false
usetocbot true

{{ page.title | replace_first:'L','Lesson '}}

{: .no_toc }

Table of Contents

{: .no_toc .text-delta }

  1. TOC {:toc}

<iframe width="100%" src="https://www.youtube.com/embed/RlEPQqyQGEk" title="Building a light-level instrument on the Circuit Playground Express in MakeCode" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
**Video.** Where we're headed: a light-level instrument that turns light readings into sound and color using the CPX's onboard light sensor, speaker, and NeoPixels. [Full code](https://makecode.com/_drYKXH5UeV1r). {: .fs-1 }

In this lesson, we will build on our last lesson—the Button Piano—to make an interactive instrument that translates light levels into sound and light. It won't sound great but it will sound fun!

Code

Here's the final code. Right-click on the code below and select "Open link in a new tab" to open it in the MakeCode editor.

<iframe style="position:absolute;top:0;left:0;width:100%;height:100%;" src="https://makecode.adafruit.com/---codeembed#pub:_2dVi02gquH6h" allowfullscreen="allowfullscreen" frameborder="0" sandbox="allow-scripts allow-same-origin"></iframe>

Blocks

We use the following blocks in this example. See the Adafruit MakeCode Reference guide.

Output

For output, we used Light, Music, and Console blocks, particularly:

  • graph makes the on-board NeoPixels into a real-time "bar graph"
  • ring tone plays a tone at a given frequency
  • set volume sets the volume of the output speaker
  • stop all sounds stops all sounds from playing
  • console log writes a line of text to the console output

Input

For Input blocks, we used:

  • light level measures light level between 0 (dark) and 255 (light)
  • switch right is true if the switch is to the right; however, I could not find documentation for this block

Logic

We also used one Logic block to check to see if the switch was to the right and, if it was, to play the sound. Otherwise, to stop all sounds.

  • if runs code depending on whether a statement is true

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

Design Activity

{: .note } The examples we include here are intentionally more complicated to help demonstrate the power and potential of MakeCode with the CPX. It's OK if you don't understand something. We'll get there!

How might you use the other built-in sensors to create music? Try playing with acceleration (motion), sound level, and temperature. For example, we've created an example accelerometer-based instrument that changes the pitch and volume based on the y and x acceleration, respectively. You need not create something so complicated but do try to experiment with different sensors!

A screenshot of MakeCode showing the accelerometer instrument Figure. An accelerometer-based instrument that changes the pitch and volume based on the y and x acceleration, respectively. Here's the full code. {: .fs-1 }

Relatedly, how might you improve how the music sounds? In the example below, we map the light level to notes in the C scale using a predefined array of frequencies (each frequency maps to a musical note in the C scale, which is indexed based on the light level).

A screenshot of the MakeCode showing the light sensor instrument mapped to the C Scale

Figure. A more advanced light sensor instrument that translates the raw light levels to notes in the C scale. Here's the full code. {: .fs-1 }

Similarly, here's a version that maps the x component of the accelerometer to notes in the C musical scale.

Next Lesson

In the next lesson, we'll introduce a multi-part series on using capacitive sensing to interact with everyday objects and more!

← Previous Lesson
Button Piano
Next Lesson →
Capacitive Sensing