T1W3: Adding Features to the Autoclicker
For our third class we will add some nice features to the CPX autoclicker project - adjustable click speed and an LED readout.
Saving and Loading Your MakeCode Programs
But first I wanted to talk about how to save and load your MakeCode programs. When you press the pink "Download" button, MakeCode generates a file that ends in ".UF2" which has the program source code as well as the compiled code that the microcontroller on the CPX board runs. This file gets saved in your Download folder on your ChromeBook and then gets copied to the CPLAYBOOT drive on the CPX board. If you need to recover the program, you can load it back into the MakeCode editor from either source. But since the CPX boards are getting shuffled between the classes, your Chromebook download directory is the best place to look.
Naming Your Program
When you start with a New Project, your program is called "Untitled" and the file that's generated is "circuitplayground-Untitled.uf2". It can be difficult to keep all the different Untitled versions straight, so before you click Download, give your program a name by typing it into the blue box to the right of the Download button. If you call your program "Autoclicker" the saved file will be called "circuitplayground-Autoclicker.uf2" to help you remember what it does.
Loading MakeCode Program From a File
To load an existing file in the MakeCode editor, click on "Home" in the upper left of the editor window to return to the list of projects. Then click on the "Import" button at the right of the "My Projects>" section. Click on "Import File" and then the "Browse" button. Navigate to the .uf2 file you want to load from your Downloads directory or the "Recent" file list in ChromeOS and then click "Go Ahead". MakeCode should open with the program you saved.
Here is copy of the autoclicker program from last week if you can't find your own.
Adding a Variable Speed Function
We can always change the click speed by changing the program code and downloading the new version, but it would be nice to be able to adjust it without needing a computer. We will add code to let the click speed be changed by pressing the right push button on the board (Button B).
Since our speed setting can be changed by the user as the program runs, we can't use a constant delay time like we had before. We will create a MakeCode variable to keep track of the current speed and update it when Button B is pressed.
Variables in MakeCode
In computer programming, a variable is a box with a name that is used to store information for use in the program. The value of the information can be entered by the user, calculated by the program, or read from sensors or another computer system, and it can change many times as the program runs. Variables can store numbers, groups of characters (called strings), or other more complicated collections of data.
In our autoclicker program, we will make a variable called ClickSpeed and use it to store the random value of the dice roll we want to display. Click on the MakeCode [VARIABLES] menu and then click on "Make a Variable...". Type ClickSpeed or whatever you would like for the name. It is helpful to give variables a name that describes the information they hold, to help other people (or even yourself) understand how the program works. MakeCode creates an oval labelled ClickSpeed that lets you access the variable in your program, and a couple of blocks to set or change the value.
It is good practice to always set the starting value of a variable before you use it for the first time. But we don't want to set it in the "forever" structure, or it will continually be reset as the autoclicker runs! MakeCode has an "On Start" structure in the [LOOPS] menu. Everything inside is run only once when the program starts, so it is a good place to initialize our ClickSpeed variable. Drag "On Start" from [LOOPS] onto your code window, and inside put the "Set ClickSpeed to 0" block from the [VARIABLES] menu. You can change the starting speed value to something else later on.
Changing the Click Speed Setting
MakeCode has a "On Button X" structure for code that will be run when an event like a button click happens. From the [INPUT] menu, insert an "On Button A click" structure into your workspace and change the button to "Button B" (the right push button). When the button is pressed we will increase the speed value, but once it reaches the maximum speed we should reset it to the minimum so there is a loop.
Speed values can be 0 through 9 (since there are 10 colored LEDs on the CPX board), so if ClickSpeed = 9 we will set ClickSpeed = 0. Insert an "if/then/else" structure from the [LOGIC] menu and add a <ClickSpeed < 9> comparison. If ClickSpeed<9, add one to ClickSpeed using [VARIABLES]->"Change ClickSpeed by 1", else set ClickSpeed back to 0 with [VARIABLES]->"Set ClickSpeed to 0".
Calculating the Delay
Now clicking Button B on the CPX board should cycle through the different speed settings, but we still need to change the delay in the click loop based on ClickSpeed. We will start with a long delay for ClickSpeed=0, and double the speed (halve the delay) for every increase in ClickSpeed. So a math formula for the delay value is: delay = longdelay / (2 ** ClickSpeed) where "**" is the exponentiation operator ( 2^ClickSpeed ).
Build up this formula from the outside in by starting with a [MATH]-> divide block. Enter 4000 (4 seconds) as the longest delay value. Add another divide block to the right side and change the function to "**" (exponentiation). Then enter a 2 on the left side and drop [VARIABLES]->ClickSpeed into the right side.
Also, move the pause block inside the if/then structure in the forever loop to avoid a long delay when changing ClickSpeed .
You should now have a working adjustible autoclicker! Watch the red LED blink to see the click rate change as you press the right push button. Your MakeCode program should look something like this:
Adding Speed Indicator Lights
With 10 different speeds it is hard to keep track of where you are in the sequence. We can use the 10 "Neopixel" LEDs on the CPX board to show us what speed we are on.
The ClickSpeed variable ranges in value from 0 to 9, so we can use that as the index for which Neopixel LED to light up after ClickSpeed is changed. But first we need to turn off all the LEDs by inserting a [LIGHT]->clear block after the "on button B click" if/then/else structure.
After the "clear" block, insert a [LIGHT]->"set pixel color at 0" block. Drop a ClickSpeed variable into the index slot and pick your favorite color.
That will update the lights after a button press, but we also need to set the starting state of the LEDs. Copy the clear and set pixel color blocks to the end of the "on start" structure. Those structures should wind up looking like this:
Test out your code in the simulator and download it to your CPX board!
For extra credit, use an array of color values to change the color of the LED indicator as the ClickSpeed increases.
Functions in MakeCode
The color LEDs are updated in two different places in the code - on start and when Button B is pushed. As the LED code gets more complicated, it is better to have that code in only one place. We can do that by defining a MakeCode function with the LED update code, and calling that function where ever it is needed in the main program.
Arrays in MakeCode
An array is a list of related variables that can be addressed using an index value. We can index an array of different color values using the ClickSpeed variable to change the color of the indicator LED. Create the color array using the [ADVANCED]->[ARRAYS] and populate it with color values from [LIGHT]->Color in the On Start structure. Then use the [ARRAYS] (Get value at) block to pick a color based on ClickSpeed.