Inhaltsverzeichnis

Touch

Übersicht

Mit einer kleinen Änderung in Bounce2.h der Bounce2 Bibiliothek klappt das Debouncen auch mit dem ESP32.

aenderung.txt
#ifdef ESP32
      virtual bool readCurrentState() { return (touchRead(pin) < 40); }
#else
      virtual bool readCurrentState() { return digitalRead(pin); }
#endif

anstatt

original.txt
virtual bool readCurrentState() { return digitalRead(pin); }

https://github.com/thomasfredericks/Bounce2

Code Beispiel

sketch.cpp
#include <Arduino.h>
 
 
/*
 DESCRIPTION
 ====================
 Simple example of the Bounce library that switches the debug LED when a button is pressed.
 */
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
 
 
#define LED_PIN 21
 
// Instantiate a Bounce object
Bounce debouncer = Bounce();
 
void setup() {
  Serial.begin(115200);
 
 
  // After setting up the button, setup the Bounce instance :
  debouncer.attach(T7);
  debouncer.interval(5); // interval in ms
 
  //Setup the LED :
  pinMode(21,OUTPUT);
 
}
int ledState = LOW;
void loop() {
  // Update the Bounce instance :
  debouncer.update();
 
  // Get the updated value :
  int value = debouncer.read();
 
  // Turn on or off the LED as determined by the state :
  if ( debouncer.fell() ) {
 
    // Toggle LED state :
    ledState = !ledState;
    digitalWrite(LED_PIN,ledState);
 
  }
}