T1W2: CPX and MakeCode Autoclicker

For the second class we will continue programming the CPX boards - the project this week is a hardware autoclicker! Plug the CPX board into a computer or Chromebook and generate mouse clicks to bake cookies, mine resources, or let a program know you are still around. Simple program modifications allow you to send mouse movements or key presses to automate even more actions.

We will start with a basic mouse clicking program and then add features as time permits.

Bare-bones Autoclicker

Plug your CPX board into your Chromebook and load the MakeCode website home page. Click on "New Project" to start a new program.

Our first program will just generate mouse left-clicks at a fixed rate when the left button is held down. To get access to the mouse functions, click on the [ADVANCED] menu and select [EXTENSIONS], then click "mouse" in the window that opens up. This should add a [MOUSE] menu under [MATH].

For our first version, we will put our code in the "forever" loop structure so it will always be running on the CPX board. We will check to see if a push button is pressed and then output the mouse click if it is. Then we will delay for a little while (whether the button is pressed or not) and go back to the beginning of the loop.

We will use an "if/then" structure from the [LOGIC] menu to check on the button - drag it into the "forever" loop. The "if/then" structure checks a function which can be either TRUE or FALSE (this is called a Boolean function). In MakeCode, the Boolean function blocks have pointed ends.

The push button is an input on the CPX board, so look in the pink [INPUT] menu for the <button A is pressed> block and drag it onto the <true> block of the if/then statement. The statement blocks inside the if/then structure will be run if the button is pressed.

To send a mouse click, look in the [MOUSE] menu for the [mouse button right] block and drag it inside the if/then structure. Click on the pull-down menu and select the [left] mouse button, and click the status switch to change it to <DOWN>. This will send a "mouse button down" event over USB.

We want to delay a short time and then release the mouse. Find the [pause] block in the [LOOPS] menu and insert it after the mouse button block inside the if/then structure. Select a pause of 100 ms from the pull-down menu (numeric values are given in ovals in MakeCode.) This is 1/10 of a second.

Then add another [mouse button right] block after the pause and set it to the [left] mouse button and <UP> state. You can save time by right-clicking on the first [mouse button] block and choosing "Duplicate".

Finally, add another pause outside of the if/then structure at the bottom. Set it to 200 ms (1/5th of a second). This sets the delay between mouse clicks. When you are done the code blocks should look like this:

Download your program to your CPX board and test it out by making some cookies. Be careful with where the mouse pointer is while you click - lots of random mouse clicks can make computers act strangely.

Adding Features - Indicator Light

It would be nice to see when and how fast the mouse button is being clicked. We could use some of the "NeoPixel" color LEDs, but let's save them for another future function. There is also a small red LED marked "D13" near the USB connector that we can control by changing the output on pin 13 of the microcontroller chip. Under [ADVANCED] -> [PINS], find the [digital write pin] block and insert it inside the if/then structure before the first [mouse button] block. In the pull down menu, select "LED" as the pin to control, and set the state switch to <HIGH>. Running this statement will change the voltage on the digital output pin and turn the little red LED on when the mouse button is activated.

Duplicate that block and place it after the second [mouse button] block, and change the state value to <LOW>. This will turn the LED back off after the mouse button is released.

Now that you can see how often and for how long the mouse button is pressed, try changing the numerical value of the delays in the program to change the mouse click rate. If the pull-down menu doesn't have the value you want you can just click in the white oval and type your own value. The value is the number of milliseconds or 1/1000 of a second to pause. See how fast you can generate the clicks by reducing the duration and pause in between clicks.

Adding Features - On/Off Switch

It is a lot of work to hold the button on the CPX board down, and you'd like to be able to get something else done while you are baking virtual cookies. So let's use the slide switch on the CPX board to allow us to do unattended clicking!

We still would like to be able to use Button A for momentary clicks, so what we want is for the program to click if Button A is pressed *OR* if the switch is turned on. We can do this with a Boolean OR function from the [LOGIC] menu. Find the < OR > function and drop it on the background of the program area (not in the "forever" structure yet). Then grab the <button A is pressed> block from the if/then statement and put it in the first input to the < OR > function. For the second input, add the <switch right> function at the bottom of the [INPUT] menu. Then drag the whole < OR > function with the two inputs back into the if/then statement. Now the clicker should work two ways - momentary while Button A is pressed, or constantly on while the little slide switch is to the right.

Here is the clicker program with those features added:

More Features

Feel free to experiment with the program so far! Next week we will add more features like an adjustable click rate with LED indicators.