You can code your Crickit and Circuit Playground Express using MakeCode! Head on over to the Crickit with MakeCode guide to get set up!
Plug the motor leads into the Crickit's Motor 1 port. Connect the red wire to the outer terminal and the black one to the inner terminal.
Also, plug in the battery pack to the Crickit's DC power jack.
You can optionally connect the speaker to the Crickit's Speaker terminal if you'll be using the advanced version of the code later that includes a bonus song.
The program for basic operation is incredibly simple -- it only uses four blocks!
From the Input category, get two on button A click blocks, and use the drop down menu to switch one to on button B click.
From the Crickit category, drag the crickit run motor 1 at 50% block into the on button A click block.
This will cause the motor to run at half speed. Change the value to 100%. You can tune this later to make the hazard harder or easier.
We want a neat way to stop the motor, too. From the Crickit category, drag a crickit stop motor 1 block into the on button B click block.
That's all it takes to get things moving!
Change the program's name from 'untitled' to 'Hammer Time' and then download it to your local hard drive. With the Circuit Playground Express (CPX) plugged in via USB, press the CPX's reset button once so the NeoPixels turn green. This places it into bootloader mode.
Drag the 'circuitplayground-Hammer Time.uf2' file you downloaded onto the CPLAYBOOT drive.
The CPX will reboot and is ready for golfing!
Here's a more elaborate version that adds many more features and interactivity elements, including a bonus easter egg song playback when you press button A!
Have a look at this one to see how it works. These are the key concepts:
- NeoPixel ring is initialized as a strip so we can use advanced features, such as rotating the color values
- The two buttons each have a "state" so that pressing button B toggles a variable to start and stop the motor, and pressing button A toggles a variable to start and stop a song
- A function is used to encapsulate the song composition
- Moving the switch changes a motor_speed variable between 50% and 100%, which is used elsewhere in the code instead of an explicit number to determine the motor speed
Here's a link to the finished program so you can edit it if you like, or simply download it and use it as is! Optionally, plug in the small metal speaker to the Speaker output terminals on the Crickit for a bigger sound!