CapTouchBasic.ino
1 // SPDX-FileCopyrightText: 2020 Carter Nelson for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 //////////////////////////////////////////////////////////////////////////// 6 // Circuit Playground Capacitive Touch Basic 7 // 8 // Print text messages for each touch pad. 9 // 10 // Author: Carter Nelson 11 // MIT License (https://opensource.org/licenses/MIT) 12 13 #include <Adafruit_CircuitPlayground.h> 14 15 #define CAP_THRESHOLD 50 16 #define DEBOUNCE 250 17 18 uint8_t pads[] = {3, 2, 0, 1, 12, 6, 9, 10}; 19 uint8_t numberOfPads = sizeof(pads)/sizeof(uint8_t); 20 21 //////////////////////////////////////////////////////////////////////////// 22 void takeAction(uint8_t pad) { 23 Serial.print("PAD "); Serial.print(pad); Serial.print(" says: "); 24 switch (pad) { 25 case 3: 26 Serial.println("The goggles! They do nothing!"); 27 break; 28 case 2: 29 Serial.println("Dental plan!"); 30 break; 31 case 0: 32 Serial.println("Lisa needs braces."); 33 break; 34 case 1: 35 Serial.println("I call the large one Bitey."); 36 break; 37 case 12: 38 Serial.println("I'm Idaho!"); 39 break; 40 case 6: 41 Serial.println("Monorail!"); 42 break; 43 case 9: 44 Serial.println("I choo-choo choose you."); 45 break; 46 case 10: 47 Serial.println("Maaaattlllock!"); 48 break; 49 default: 50 Serial.println("THIS SHOULD NEVER HAPPEN."); 51 } 52 } 53 54 //////////////////////////////////////////////////////////////////////////// 55 boolean capButton(uint8_t pad) { 56 // Check if capacitive touch exceeds threshold. 57 if (CircuitPlayground.readCap(pad) > CAP_THRESHOLD) { 58 return true; 59 } else { 60 return false; 61 } 62 } 63 64 //////////////////////////////////////////////////////////////////////////// 65 void setup() { 66 // Initialize serial. 67 Serial.begin(9600); 68 69 // Initialize Circuit Playground library. 70 CircuitPlayground.begin(); 71 } 72 73 //////////////////////////////////////////////////////////////////////////// 74 void loop() { 75 // Loop over every pad. 76 for (int i=0; i<numberOfPads; i++) { 77 78 // Check if pad is touched. 79 if (capButton(pads[i])) { 80 81 // Do something. 82 takeAction(pads[i]); 83 84 // But not too often. 85 delay(DEBOUNCE); 86 } 87 } 88 }