Combined Code

The Combined Code that runs on the CPX has the ability to work with all of the cards included in your kit. For this to work properly the code is broken up into sections that each work with a specific card. You can use these different sections by changing the mode of the CPX. To change the mode you will need to have the switch on the CPX flipped to the “right” (towards the B button) and then you can press the A button to move forward through the modes or the B button to move backwards through the modes. To help you tell which mode you are in the small red LED near the USB port will blink a specific number of times. Below you can find more details about each mode and how the code switches between them.


The Design

The Combined Code is set up as a state machine. This means that you will be able to navigate different modes in the CPX to activate different code. For example, in mode 1, there will be a set of code that will allow you to successfully run the contact sensor card and in mode 2, a potentiometer sensor code.

How it works

Flip the switch to the right to change to different modes. There will be a total of 6 modes in the code. By pressing the button A, you will be able to navigate forward in the modes i.e. 1 to 2, 2 to 3. Pressing the button B will allow you to navigate backwards, i.e. 3 to 2 and 2 to 1. the built-in LED next the USB port, labeled D13, will flash the number corresponding to which mode you are on. So if you just switched to mode 3, the light will blink 3 times. Once you have set your mode, then make sure you put the switch back to left so that you can use the buttons without changing the modes! Everytime you reboot the CPX it will start off in mode 1.

In the Code

On start: The mode starts at 1 every time.

Forever loop: Depending on the number of the counter, different parts of the code are run. If the counter is at 1, it means the state is at mode 1 which will set all pixels to orange and run the code inside the corresponding if statement.

Switch: Flipping the switch right will enable mode change. In this state, the user will not be able to program anything to both A and B buttons, since they are being used to switch modes. Left switch will allow the users to use both A and B buttons without changing the modes.

On button A click: Every time the button A is clicked, the counter increases 1, which means the mode increases as well. Once the mode reaches past mode 6, then it goes back to mode 1.

On button B click: Every time the button B is clicked, the counter decreases 1, which means the mode decreases as well. Once the mode reaches below 1, then it goes back to the mode 6.

Applying the code: If you would like to change what code goes into any of the modes, you can! Simply locate the corresponding if-statement block and switch out the current code with the new code you would like to use.

Adding new modes: To watch a video about adding a new mode, visit:

If you want to add a new mode to the code click the plus button below the empty else block near the bottom of the code. Then you will need to copy the block from one of the other else if statements that looks like what is circled in red and put it into the new else if statement.

You will also need to change the number to be 1 higher than the previous mode. Finally change the numOfModes over circled in red to be equal to the new number of modes. (numOfModes is a variable CodeCraft team created in this program)

You can then put new code into the mode block you created.

Accessing separated code

Below, you can find both sensor and actuator codes in blocks separated from the combined code. You can reference these blocks to create your own code.