You can code your Circuit Playground Express using MakeCode to open and close with buttons presses and also using the light sensor! If you aren't already familiar with MakeCode, check out this guide to get started, then return here when you're ready.
Here's what we will set up the Circuit Playground Express to do:
- Open the garage door by turning the servo when the A button is pressed
- Close the garage when the B button is pressed
- Watch for a bright light on the light sensor
- When a bright light is detected, open the door, wait five seconds, and then close the door
Those are the basics, but we'll also get fancy and set up some lights and sounds to accompany these functions.
First, we'll set up some things that happen as soon as the program starts. Begin by dragging an on start block from the LOOPS category into your program canvas area.
Place a set all pixels to block into the on start block, and change the color to orange. This will be the color of the NeoPixel ring on the Circuit Playground Express when the program starts.
It's also nice to have a startup sound. Pull in a play tone at block. Change the note to High A and the duration to 1/2 beat.
Since we'll be setting open and closed values for the servo angles multiple times in the program, it's helpful to set up a couple of variables with these numbers which we can then use over and over again.
From the variables category click Make a Variable... and then create one and name it 'closed'. Repeat this a second time for one called 'open'.
Drag in a block called set closed to into the on start loop. Then duplicate this block and change the drop down menu to the open variable.
You may need to adjust these values later but for now use 170 and 70 as shown here.
We'll have the servo automatically go to the closed position when the program starts.
From the pins category, drag in a servo right block. Change the pin from the default A1 to A2. (You can use either of these two pads on the Circuit Playground Express to control a servo.)
Then from the variables category drag in closed variable into the servo right pin block.
From the input category drag in the light threshold block. Change the first drop-down item from dark to bright. Then set the value to something pretty high such as 240. This will vary depending on how bright the light source is that you want to use to open the garage door.
Finally we will turn off all of the NeoPixel LEDs by dragging in a set all pixels block from light category, and then changing the color value to black.
Next we will set up the Circuit Playground Express so that pressing the A button will sound a tone and open the garage. We will set up to the B button to close the garage.
Create a set of blocks just like the ones shown in the image below.
Finally, we will set up a light sensor block so that when a bright light is detected the following actions occur:
- The LEDs will turn blue
- A melody will play
- The servo will open the door.
- Then an animation will play for five seconds, giving a toy car time to pull into the garage
- The lights will change to red
- A different melody will play
- The door will close
- The LEDs will turn off
Add the blocks needed to do these actions, as shown in the block set below.
Your program is complete! There's an interactive embedded version of it below. You can now download your program from your MakeCode session, press the reset button on your Circuit Playground Express, and drag the program onto it.
You're ready to test it out!