It turns out, when you program the Circuit Playground Express to utilise the capacitive touch for sound, it makes sound pretty much every time you touch it. This is great for the project! However, it can be incredibly frustrating if you're simply trying to move the board or unplug it. To alleviate this issue, we're going to use the slide switch to have the option to mute the tones.

To begin, move the slide switch is to the left. This is the off or mute position, or as the board and code reads it, "True".

Rename your current if you made any changes you want to keep. Download the following file. Rename it to and copy it to your CPX.

# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

from adafruit_circuitplayground import cp

while True:
    if cp.switch:
        print("Slide switch off!")
        print("Slide switch on!")

We've again imported the module from the library file on the first line. Then we have something new: a while statement. while True: essentially means, "Forever do:".

while True: creates a loop. When there is a loop, the code will forever go through the code inside the loop. All code that is indented under while True: is "inside" the loop.

Inside our loop, we have an if statement. An if statement says, "if this event is happening, do the following." Our code says, if the switch is to the left, or True, print "Slide switch off!"

This is followed by an else statement. And else statement says, "Otherwise, do the following." An else typically follows an if. Together they say, "If this is happening, do this first thing, otherwise, do the second thing." Our code says, when the switch is to the right, or False, print "Slide switch on!"

The True and False are how the slide switch knows it's location, and do not necessarily reflect the purpose you may assign it.

Next, we're going to learn about the capacitive touch pads and adding sound.

This guide was first published on Oct 20, 2017. It was last updated on Apr 16, 2024.

This page (Now Slide To The Left) was last updated on Apr 16, 2024.

Text editor powered by tinymce.