Adding Touch

In this next step, we'll replace the buttons with capacitive touch. That way we don't have to click the button, just tap the pads with our fingertips

All that's needed is to replace the button checks with checks for the capacitive touch inputs. The code uses the top touch pad on each side as a replacement for the button on that side. 

Touch sensitivity can be adjusted to suit your need. See the code for the use of adjust_touch_threshold

As before, save the code below as code.py on your Circuit Playground Express.

"""
Circuit Playground Express GBoard: capacitive touch generating tones

Adafruit invests time and resources providing this open source code.
Please support Adafruit and open source hardware by purchasing
products from Adafruit!

Written by Dave Astels for Adafruit Industries
Copyright (c) 2018 Adafruit Industries
Licensed under the MIT license.

All text above must be included in any redistribution.
"""

import time
from adafruit_circuitplayground.express import cpx

DOT_DURATION = 0.20
DASH_DURATION = 0.5

# You can adjust this to get the level of sensitivity you want.
cpx.adjust_touch_threshold(100)

while True:
    if cpx.touch_A4:
        cpx.play_tone(4000, DOT_DURATION)
        time.sleep(0.1)
        while cpx.touch_A4:
            pass
    elif cpx.touch_A3:
        cpx.play_tone(4000, DASH_DURATION)
        time.sleep(0.1)
        while cpx.touch_A3:
            pass

Now, when you touch the A4 pad a short tone will sound, and when you touch the A3 pad a longer tone will sound.  As before, the while cpx.touch_X: pass code causes it to wait until you release the button before playing the tone again. This prevents you triggering it multiple times accidentally.

Be careful how you hold the board so that you don't accidentally touch the A3 and A4 touch pads.

This guide was first published on Jul 24, 2018. It was last updated on Jul 24, 2018. This page (Adding Touch) was last updated on Mar 26, 2019.