Circuit Playground Express and Bluefruit have an accelerometer built in which opens up all kinds of opportunities for inputs. One of those inputs is tap. You have the ability to tap your board to tell it to do something. There are two options: single tap and double tap. Single tap looks for one tap before reacting. Double tap looks for two taps before reacting. Let's take a look!

Add the following code to your code.py. Remember, if you need help with this, check here.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""This example prints to the serial console when the board is double-tapped."""
import time
from adafruit_circuitplayground import cp

# Change to 1 for single-tap detection.
cp.detect_taps = 2

while True:
    if cp.tapped:
        print("Tapped!")
    time.sleep(0.05)

Open the serial console to see when the board is double tapped. Try tapping the board twice to see it printed out!

First we import time and cp.

Then we set cp.detect_taps = 2. This tells the code to look for a double tap, or two taps, before responding.

Inside our loop, we have if cp.tapped:. The code tells cp.tapped that we're looking for 2 taps before responding. So, if the board is tapped twice, the response is to print Tapped! to the serial output. To see this, open the serial console, and tap your board twice. Tap twice. Tapped! We include a time.sleep(0.05) to prevent mistakenly detecting multiple double-taps at once.

Try changing cp.detect_taps to 1. Tap the board once to see the same response!

Now, let's do something with it! Add the following code to your code.py:

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""This example turns on the little red LED and prints to the serial console when you double-tap
the Circuit Playground!"""
import time
from adafruit_circuitplayground import cp

# Change to 1 for detecting a single-tap!
cp.detect_taps = 2

while True:
    if cp.tapped:
        print("Tapped!")
        cp.red_led = True
        time.sleep(0.1)
    else:
        cp.red_led = False

Try tapping twice. Red LED!

Let's look at the code. First we import time and cp.

We'll keep cp.detect_taps = 2 to tell the code to look for two taps.

Inside our loop, we are checking to see if the board has been tapped twice. We still print to the serial output, so we can see if we've successfully tapped. But now, we've added turning on the red LED. Since the tap event is extremely quick, we've also included a time.sleep(0.1) so the red LED stays on long enough for us to see. Without it, it's a super quick flash. And we have our else to turn off the red LED when not tapping the board - otherwise it would turn on and never turn off.

Single Double

You can't detect a single tap and a double tap at the same time - it's a limitation of the hardware. You can include both single tap and double tap detection in one piece of code if you separate them with a delay of some sort. Let's take a look. Add the following code to your code.py.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""This example shows how you can use single-tap and double-tap together with a delay between.
Single-tap the board twice and then double-tap the board twice to complete the program."""
from adafruit_circuitplayground import cp

# Set to check for single-taps.
cp.detect_taps = 1
tap_count = 0

# We're looking for 2 single-taps before moving on.
while tap_count < 2:
    if cp.tapped:
        tap_count += 1
print("Reached 2 single-taps!")

# Now switch to checking for double-taps
tap_count = 0
cp.detect_taps = 2

# We're looking for 2 double-taps before moving on.
while tap_count < 2:
    if cp.tapped:
        tap_count += 1
print("Reached 2 double-taps!")
print("Done.")
while True:
    cp.red_led = True

This code looks for two single-taps and then two double-taps to complete the sequence. So, if you single-tap the board twice, and then double-tap the board twice, you'll work though this code and see the messages printed out as you go!

Let's take a look at the code. First we import cp and then set it to look for single taps with cp.detect_taps = 1.

Then we create the variable tap_count and assign it to 0 with tap_count = 0. We're going to use this to keep track of how many times we've tapped the board. This is how we know when to move on to the next part of the code.

Our loop is different from our previous loops. This loop begins with while tap_count < 2:. It says, "keep looping through the following indented code until tap_count is greater than 2." Since tap_count is currently 0, we'll begin the loop. The code inside the loop says, "If the board has been tapped, increase tap_count by 1." Each time you tap the board, it prints to the serial console, Single-tap! The first time you tap the board, tap_count = 1. The second time, tap_count = 2. 2 is not less than 2, so after the second tap, the code stops working through this loop and moves on to the next section. The last thing we do before moving on is print to the serial console, Reached 2 single-taps! so we know we've reached the end of this section.

Next, we set tap_count = 0 again since we're going to start looking for a new type of tap. Then we set the code to look for double taps with cp.detect_taps = 2.

Our next loop is the same as the first. While tap_count is greater than 2, check to see if the board is double tapped, and if it is, print Double tapped! and increase tap_count by 1. Once it reaches 2, the code moves on. Then we print to the serial console, Reached 2 double-taps!.

The last thing we do is print Done, and turn on the red LED so we know our code is finished.

This type of code could be used to create a Circuit Playground Express controlled combination lock where the combination is a series of taps. Get creative with it and see what you can come up with!

This guide was first published on Jun 05, 2018. It was last updated on Apr 17, 2024.

This page (Tap) was last updated on Apr 17, 2024.

Text editor powered by tinymce.