MakeCode is a web-based code editor for physical computing made by Microsoft.
More on MakeCode for Adafruit boards here.
Head over to https://makecode.adafruit.com/ and create a new project.
Click the button below to access the code for this project.
start, set the
volumeof the CPX speaker, set the variable
0, then set all the NeoPixels to red.
- In a
forever loop, we check to see if the variable
playis set to 1. If it is, call the
songfunction to play the tune "Auld Lang Syne" then set play to 0. As it's a forever loop, this code block loops forever regardless of other events that happen in the code!
- An abbreviated version of the song The "Auld Lang Syne" can be separated into 2 parts. The tune starts with part 1, then goes to part 2, before repeating again. Creating functions for these song parts then "calling" the functions later allows us to have much cleaner looking and more organized code. It also allows us to reduce the amount of code needed for the project making the program more efficient (more on that in the next code block).
- When the CPX is shaken, we call the function
countingdown, set the variable
playto 1, turn the NeoPixels rainbow, turn the servo clockwise for
3 seconds, pause the servo for
15 seconds, turn the servo counter-clockwise for
3 secondsthen turn the NeoPixels back to all red.
- In the
countingdownfunction, we want to count the NeoPixels down one at a time, per second, from all red to all white. To do this with out making the code clunky, we can use something called a
while loop. First, the NeoPixels are set to all red, then the variable
countdownis set to
9. In the
while loop, as long as the variable
countdownis greater than or equal to 0, each concurrent NeoPixel will change from red to white, every second. In each iteration of the loop,
countdownis decremented by 1 until it reaches -1 at which point the program will exit the function and continue with the rest of the code to play the song and turn the servo.
- In the
on shakecode block, the piece of code that says "Servo write pin A1 to (value between 0 and 180)" determines how fast or slow and what direction the servo spins. Values between 0 and 90 spin the servo clock wire and 90 - 180 is counter clockwise. The closer the values are to 90, the slower the servo will spin in either direction.
Now that we have the code for the project, we need to upload it to the CPX.
Let's name our file and download it.
- Choose a name at the bottom of the page.
- Then click the pink Download button.
Follow the directions and connect your CPX to your computer via the usb cable. Click the CPX Reset button once to go into programming mode (all NeoPixels will turn green).
In some cases, you may need to press the reset button twice to get into programming mode.
Next, look for the file in your downloads folder and drag it onto your CPLAYBOOT drive that should have showed up in your file manager/finder when you plugged in your board and entered programming mode.
You should now see the CPLAYBOOT drive disappear.
The code should now be running and the NeoPixels should be all red. Before we test it out let's wire up the servo!