The Circuit Playground Express has a slide switch on it, above the battery connector.
The slide switch doesn’t control the power of the board. It is a switch that returns True
or False
depending on whether it’s left or right. So, we can use it as a toggle switch in our code!
Let’s take a look:
We can access the status of the switch using cpx.switch
:
- When it’s left:
True
- When it’s right:
False
To run code when the switch is True
, we would do something like:
from adafruit_circuitplayground.express import cpx while True: if cpx.switch == True: # Stuff happens
Instructions
In the last exercises, we learned how to light up the NeoPixels. There is another LED light at the top right of the board that’s known as the red “D13” LED light.
Let’s use the slide switch to control it.
Inside the while
loop, type the following code:
if cpx.switch: cpx.red_led = True else: cpx.red_led = False
cpx.red_led
is how we control the little red “D13” LED light.
Click the Download button to download the code.py file.
Drag and drop the downloaded file into the CIRCUITPY volume on your computer.
Slide the switch back and forth. Is the LED switching on and off?
Don’t forget to press Run to move on.