The Code

In this section we will upload and test the code for this project.

Getting Familiar

The code was created using Microsoft MakeCode for Adafruit, a web-based code editor. MakeCode provides a block editor, similar to Scratch or Code.org, and also a JavaScript editor for more advanced users.

If you'd like to learn more about MakeCode, this guide is a good place to start.

Bootloader Mode

When you connect Circuit Playground Express to your computer for the first time, it will appear as a flash (or thumb) drive named CIRCUITPY.

BUT we'd like to make it work with MakeCode. 

All that's required to do this is to connect the board to your computer with a micro USB cable and click the small reset button in the center of the board. The available thumb drive should eject itself and show up again as CPLAYBOOT. Your code file will be placed on CPLAYBOOT.

Click this link or the button below to interact with and change the code for this project.

How to Upload Code

To upload code, connect you Circuit Playground Express to your computer using the micro USB cable, click the Download button to download the .uf2 file to your computer, and drag 'n drop that .UF2 file onto the CPLAYBOOT drive in your computer's file explorer or finder.

The drive will automatically eject itself (your computer may give you a "failed to eject drive correctly" error, you can ignore this). The code is now on your Circuit Playground Express and ready to run!

How it Works

The code for this project uses MakeCode's Variables and Functions to create two modes, one in which the NeoPixel strip changes its brightness based on readings from its light sensor, and one in which it changes its color based on its temperature reading.

In the forever block we can see the two modes that the NeoPixel strip is capable of displaying.

To detect light we are using the onboard analog light sensor. This can be used to detect ambient light, with similar spectral response to the human eye. This sensor will read between 0 and 1023 with higher values corresponding to higher light levels (a reading of about 300 is common for most indoor light levels).

 

Temperature is measured using an NTC thermistor in the top right part of the board. While it isn't an all-in-one temperature sensor, it's easy to calculate the temperature based on the analog voltage output. 

These two pink blocks determine how we change from one mode to the other.

The color change function is calculated for a minimum of 60 degrees and maximum of 85 degrees, for a 25 degree swing. This means that at 60 degrees the NeoPixel strip will be full blue, at 85 degrees, full red. We'll show you how to change the range and temperature scale in a following page.

This guide was first published on Jan 04, 2019. It was last updated on Jan 04, 2019. This page (The Code) was last updated on Jul 15, 2019.