Crickit Install for MakeCode
You can code your Crickit and Circuit Playground Express using MakeCode! Head on over to the Crickit with MakeCode guide to get set up!
The Stroboscopic Zoetrope code will need to do the following tasks:
- Spin the DC motor connected to the Crickit
- Allow the motor speed to be increased and decreased with the buttons on the Circuit Playground Express (CPX)
- Rapidly flash the NeoPixel ring on the CPX
- Allow the flashing speed to be increased and decreased with the buttons on the CPX
- Switch button modes using the slide switch on the CPX
Let's start off with a simple version that can strobe at a fixed rate and run the motor. Later we'll add some fancy controls.
Flashing Lights
The first thing we’ll do is turn all of the NeoPixels white by adding a show ring
block to an on start loop
block.
Then, we'll flash the LEDs by turning their brightness up high, pausing, setting the brightness to zero, pausing, and repeat!
We also need to run the the DC motor — we can start by setting it to run at, say 80% speed. Use the Crickit category's crickit run motor 1
at 80% block.
Try downloading and running this code now. Turn on the Crickit power and you should see the disk spin and the lights blink rapidly!
You probably won’t get the strobe effect, since it requires a bit of tuning. Instead of hand tuning the numbers in MakeCode, re-uploading and iterating this way, we’ll add controls so that the buttons on the CPX can be used to change the motor speed and strobe rate.
Buttons, Switches, Variables, and Modes
Here's how we'll do it -- three variables are created:
switch_mode
strobe_rate
motor_speed
These are initialized in the on start block as shown. Note how instead of simply typing a number, we'll use a true block for the switch_mode
variable's initial value. This makes the variable a "boolean" which means it can either be true
or false
.
Switch Mode
Then, we’ll get an on switch moved left
block and add the set switch_mode to
block from the variable category. Make the value a true
block from the logic category.
Duplicate this block and change it to on switch moved right
, and set switch_mode to false
as seen here.
Now, when you toggle the Circuit Playground Express switch you'll be flipping the state of switch_mode
between true
and false
.
We’ll used true
to represent strobe mode, and false
to represent motor mode for the A and B buttons.
Buttons
Finally, we’ll set up both buttons. From INPUTS, bring in a on button A click
block and change it to down
instead of click
. Add to it an if true then else
block from the LOGIC category so the button can be used to change either strobe_rate
or motor_speed
depending on the switch mode.
Bring in a 0 = 0
comparison block from the LOGIC category to replace the simple true
check, then change the first 0
to a switch_mode
variable and the second one to true
as shown here.
In the if
section, bring in a change by
block from the VARIABLES category and choose strobe_rate
and a value of 0.5
. This means the strobe pause will increase by 0.5 milliseconds (ms) each time the button is pressed when the switch is in strobe mode, thus slowing down the strobe.
Repeat this for the else
section, but change to motor_speed
and a negative change of -0.5
. This means the motor speed will slow down by half a percent each time the button is pressed.
Now, duplicate this whole button input and change it to be button B
the change values as shown. This way, the B button will reduce the strobe delay, thus speeding up the strobing, and can increase the motor speed when in motor speed mode.
You can click the button below to edit a live copy of the code.
Here's what the final program looks like.
Upload this new code to your CPX and you're ready to run the zoetrope!
Page last edited March 08, 2024
Text editor powered by tinymce.