In this section, we will go through each code specific for the sensor card provided in the kit. All of the codes have been separated for each card and will run independently. You will be able to find each code inside the forever loop in each mode block. The mode blocks are inside the “if counter == number” blocks in the Combined Code.
The contact code uses the ability of the CPX to detect the voltage connected to a pin and interpret that as a digital value, a 1 or a 0. Digital values are sometimes referred to as ON or OFF, HIGH or LOW, and TRUE or FALSE. For the CPX a voltage of 3.3 volts is digitally read as a 1 and a voltage of 0 volts is digitally read as a 0. Going forward we will refer to these states as HIGH and LOW
In the contact mode a special command set pull pin to up is used to set pin A6 to 3.3 volts unless it is connected to GND (which is at 0 volts). This means that until we connect pin A6 to GND it will always digital read a HIGH.
Using a digital read, the If statement checks to see if the pin A6 is LOW, which means it is connected to GND. THEN it will do what is inside of it. If A6 is HIGH, meaning it is not connected to GND then the code in the ELSE section will run.
Digital read = LOW = Contact has been made
Digital read = HIGH = No contact has been made
Potentiometer, Pressure, and Flex
These three sensor cards require analog input, unlike digital read which is only HIGH or LOW, analog read turns a range of voltages (from 0 to 3.3 Volts) into a range of numbers (0 to 1023) which we can then do things with.
Reading and outputting the values are simple here. You read the corresponding analog pin (in this case it is A7) and graph it.
Graph is a special function that as the values of the input changes, the lights will also move with it.
For the capacitive sensor, before we start the code, we need to set our threshold. This determines the sensitivity of the input sensor. This needs to be done only once, so you can put it in the “on start” block.
Now, in an If statement, you can simply check if each capacitive sensor has been triggered by checking if the pin has been pressed. Depending on the threshold you can even tigger a capacitive sensor by being close to it and without having to actually touch it. You can also sense touch through thin materials like paper. Capacitive sensing works best to detect fingers.
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.