acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
CapacitiveButton.ino (2903B)
1 /* 2 * A demo sketch that uses the AceButton Library 3 * (https://github.com/bxparks/AceButton) and the CapitiveSense Library 4 * (https://github.com/PaulStoffregen/CapacitiveSensor) to detect touches and 5 * clicks of a capacitive switch. 6 * 7 * Prompted by questions from Gaston Loos. 8 * 9 * Brian T. Park 2018 10 */ 11 12 #if defined(ESP32) || defined(ESP8266) 13 #error ESP32 or ESP8266 not supported 14 #endif 15 16 #include <CapacitiveSensor.h> 17 #include <AceButton.h> 18 using namespace ace_button; 19 20 /** 21 * A subclass of ButtonConfig that emulates a mechanical switch connected to a 22 * pull-up resistor on the input pin. A "touch" sends a LOW signal, just like a 23 * mechnical switch. 24 */ 25 class CapacitiveConfig: public ButtonConfig { 26 public: 27 CapacitiveConfig(CapacitiveSensor& sensor): 28 mSensor(sensor) {} 29 30 protected: 31 // Number of iterations to sample the capacitive switch. Higher number 32 // provides better smoothing but increases the time taken for a single read. 33 static const uint8_t kSamples = 30; 34 35 // The threshold value which is considered to be a "touch" on the switch. 36 static const long kTouchThreshold = 100; 37 38 int readButton(uint8_t /*pin*/) override { 39 long total = mSensor.capacitiveSensor(kSamples); 40 return (total > kTouchThreshold) ? LOW : HIGH; 41 } 42 43 private: 44 CapacitiveSensor& mSensor; 45 }; 46 47 // Timeout for a single read of the capacitive switch. 48 static const unsigned long TIMEOUT_MILLIS = 10; 49 50 // I used a 1M resistor between pins 4 (send) & metal plate, and a 1K resistor 51 // between the plate and pin 6 (receive). Try adjusting the 52 // CapacitiveConfig::kTouchThreshold value for other resistor values. 53 CapacitiveSensor capSensor(4, 6); 54 55 CapacitiveConfig buttonConfig(capSensor); 56 AceButton button(&buttonConfig); 57 58 void handleEvent(AceButton* /* button */, uint8_t eventType, 59 uint8_t /* buttonState */) { 60 switch (eventType) { 61 case AceButton::kEventPressed: 62 Serial.println(F("Pressed")); 63 break; 64 case AceButton::kEventClicked: 65 Serial.println(F("Clicked")); 66 break; 67 case AceButton::kEventDoubleClicked: 68 Serial.println(F("DoubleClicked")); 69 break; 70 } 71 } 72 73 void setup() { 74 Serial.begin(115200); 75 while (!Serial); // Leonardo/Micro 76 77 // Set the timeout to 10 millisecond so that AceButton::check() 78 // can have about 4-5 iterations during the 50 millisecond debouncing time. 79 capSensor.set_CS_Timeout_Millis(TIMEOUT_MILLIS); 80 81 // Configure the button using CapacitiveConfig. 82 buttonConfig.setFeature(ButtonConfig::kFeatureClick); 83 buttonConfig.setFeature(ButtonConfig::kFeatureDoubleClick); 84 buttonConfig.setEventHandler(handleEvent); 85 } 86 87 void loop() { 88 unsigned long start = millis(); 89 button.check(); 90 91 // check on performance in milliseconds 92 unsigned long duration = millis() - start; 93 if (duration > TIMEOUT_MILLIS) { 94 Serial.print(F("duration: ")); 95 Serial.println(duration); 96 } 97 }