Using MakeCode, we'll create a program to control the slider crank. We'll give it two modes -- automatic and manual. In automatic mode, the servo will oscillate back and forth between extended and retracted positions on the slider
In manual mode, you will be able to press the A and B buttons on the Circuit Playground Express to extend or retract the slider.
If you're new to MakeCode, start by going through the introductory guide here. Then, return to this guide to continue.
Servo Control
First, let's have a look at controlling a servo with MakeCode. Start a new program by going to makecode.adafruit.com and clicking New Project.
In the MakeCode editor, click on the ADVANCED button to reveal additional categories. Then, click on PINS and from the Servo section, drag a servo write pin A1 to 180 block onto the program canvas area and into the forever block.
With the servo attached to power, ground, and pad A1 on your Circuit Playground Express, download the program, connect the CPX to your computer over USB, press the reset button to enter bootloader mode. Then drag the downloaded .uf2 file to the CIRCUITPY drive.
When the program finishes uploading, the servo will quickly rotate to 180 degrees.
Back and Forth
We can make the servo move back and forth with a pause in between by duplicating the servo write A1 to 180 block, changing its value to 0, and then adding a pair of pause blocks.
Angle Variables
Now, these angles are a bit extreme for our slider crank mechanism. Let's make the maximum angle 170 and the minimum 95.
Since we'll use these values in a number of places throughout our code, and we may want to fine tune them, we can create variables to represent each. Then we only need to change the variables' values in one place.
In the VARIABLES category, click on Make a Variable... and create a variable called maxAngle and another called minAngle. Also, create one called servoOn which we'll use later.
We'll want to set the values of the servo angle variables when the program begins. Drag an on start block from the LOOPS category. Then, put into it a pair of set maxAngle to blocks into the on start. You'll used the drop down menu to pick the variables in each block, change one of them to minAngle. You can then set the values as shown.
Pulse Timing
Different servos expect different timing for their command pulses. If you need to change this value, you can add a servo set pulse pin A1 to block to the on start as shown here.
Complete the on start block by adding one more set maxAngle to block, and change the dropdown to the servoOn variable with a value of 0. We'll use this a little bit later.
Use the Angle Variables
Let's put those variables to use. From the VARIABLES category, drag a minAngle and maxAngle into the servo write pin A1 blocks in your forever loop.
Add Lights
We'll add some NeoPixel lighting now. We can make the CPX LEDs light up to indicate the direction of travel.
From the LIGHT category, add a couple of show ring blocks as shown here. You can click the circles twice to "blank" them into these patterns.
Mode Switch
You may get tired of the servo swinging back and forth, so next you can set up a mode switch. This will read the small selector switch on the CPX. If it is moved to the right, the servo swings back and forth, if not, it won't!
Get an if true then else block from the LOGIC category. Transfer the contents of the forever block into the if section as shown.
Conditions
We now need some condition to be met that will be tested by if statement. From the LOGIC category get a 0 = 0 comparison block and move it onto the true block.
Drag in a servoOn variable from VARIABLES and set the value to 1.
Switch Reading
We will now set up blocks to read the switch position, and change the servoOn value accordingly.
From INPUT, drag in two on switch moved left blocks. Change one of them to right.
Add from the VARIABLES category a set servoOn to block to each switch block, setting the right one's value to 1.
Now, when you flip the switch left and right you'll toggle the value of the servoOn variable!
To give visual feedback when the mode switch if flipped, add show ring blocks as seen here, as well as resetting the servos to their minimum angles.
Button Clicking
The final element is to add button control. from INPUT get a couple of on button A click blocks. Change one to button B.
Add to these a servo write pin A1 each, and a show ring as seen here.
The program is complete!
Here's the final code, you can now download it to your computer and then reset the CPX and drag the .uf2 program file onto the CIRCUITPY drive:
Now, we'll build the base and guide to secure the mechanism onto our cardboard box.
Page last edited March 08, 2024
Text editor powered by tinymce.