Let's start simple. This sketch prints a unique message for each touch pad. Download it and load it to the Circuit Playground.

// SPDX-FileCopyrightText: 2020 Carter Nelson for Adafruit Industries
//
// SPDX-License-Identifier: MIT

////////////////////////////////////////////////////////////////////////////
// Circuit Playground Capacitive Touch Basic
//
// Print text messages for each touch pad.
//
// Author: Carter Nelson
// MIT License (https://opensource.org/licenses/MIT)

#include <Adafruit_CircuitPlayground.h>

#define CAP_THRESHOLD   50
#define DEBOUNCE        250

uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10};
uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t);

////////////////////////////////////////////////////////////////////////////
void takeAction(uint8_t pad) {
  Serial.print("PAD "); Serial.print(pad); Serial.print(" says: ");
  switch (pad) {
    case 3:
      Serial.println("The goggles! They do nothing!");
      break;
    case 2:
      Serial.println("Dental plan!");
      break;
    case 0:
      Serial.println("Lisa needs braces.");
      break;
    case 1:
      Serial.println("I call the large one Bitey.");
      break;
    case 12:
      Serial.println("I'm Idaho!");
      break;
    case 6:
      Serial.println("Monorail!");
      break;
    case 9:
      Serial.println("I choo-choo choose you.");
      break;
    case 10:
      Serial.println("Maaaattlllock!");
      break;
    default:
      Serial.println("THIS SHOULD NEVER HAPPEN.");
  }
}

////////////////////////////////////////////////////////////////////////////
boolean capButton(uint8_t pad) {
  // Check if capacitive touch exceeds threshold.
  if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) {
    return true;  
  } else {
    return false;
  }
}

////////////////////////////////////////////////////////////////////////////
void setup() {
  // Initialize serial.
  Serial.begin(9600); 
  
  // Initialize Circuit Playground library.
  CircuitPlayground.begin();
}

////////////////////////////////////////////////////////////////////////////
void loop() {
  // Loop over every pad.
  for (int i=0; i<numberOfPads; i++) {
    
    // Check if pad is touched.
    if (capButton(pads[i])) {
      
      // Do something.
      takeAction(pads[i]);
      
      // But not too often.
      delay(DEBOUNCE);
    }
  }
}

With this sketch loaded and running on the Circuit Playground, open the Serial Monitor.

Tools -> Serial Monitor

Now press any of the 8 touch pads. You should see a message printed out for each one.

Take a look at the loop() function in this sketch. The main change from the previous sketch is the addition of an outer for loop which scans over each of the touch pads on the Circuit Playground.

void loop() {
  // Loop over every pad.
  for (int i=0; i<numberOfPads; i++) {
    
    // Check if pad is touched.
    if (capButton(pads[i])) {
      
      // Do something.
      takeAction(pads[i]);
      
      // But not too often.
      delay(DEBOUNCE);
    }
  }
}

The variable numberOfPads and the array pads[] have been defined globally near the top of the sketch. The sequence of the numbers in pads[] is such that they are scanned in a counter-clockwise fashion starting with pad #3.

uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10};
uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t);

Also, a new function called takeAction() has been created. This allows us to use this same loop() setup for all of the sketches. All we have to do is change the code in takeAction() to change the behavior of the Circuit Playground.

If you look at the code in takeAction() you will see it contains a switch statement. This is where the main plumbing between Circuit Playground touch pads and actual code takes place. The case statements correspond to pad numbers and the code inside the case statement defines what is executed for each pad.

For example, for pad #12, the following lines of code are executed:

      Serial.println("I'm Idaho!");
      break;

The break is needed to exit out of the switch statement.

Keen. Now let's do something a little more exciting than just printing out Simpsons quotes.

This guide was first published on Nov 15, 2016. It was last updated on Mar 28, 2024.

This page (Basic Print) was last updated on Mar 28, 2024.

Text editor powered by tinymce.