From a522c2142c75fa8d627440c7da2484e46afe0934 Mon Sep 17 00:00:00 2001 From: StandBY76 Date: Fri, 25 Feb 2022 13:48:43 +0100 Subject: [PATCH 1/2] TouchPin added Touch pin of ESP32 added to library --- ClickButton.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++- ClickButton.h | 23 +++++++++++++++ keywords.txt | 4 ++- 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/ClickButton.cpp b/ClickButton.cpp index 7a1e1fe..65f8311 100644 --- a/ClickButton.cpp +++ b/ClickButton.cpp @@ -45,6 +45,7 @@ NOTE! History: + 2022.02.23 - Touch pin button for ESP32 added 2013.08.29 - Some small clean-up of code, more sensible variable names etc. Added another example code for multiple buttons in an object array 2013.04.23 - A "minor" debugging: active-high buttons now work (wops)! @@ -57,6 +58,7 @@ NOTE! #include "ClickButton.h" + ClickButton::ClickButton(uint8_t buttonPin) { _pin = buttonPin; @@ -92,6 +94,7 @@ ClickButton::ClickButton(uint8_t buttonPin, boolean activeType) pinMode(_pin, INPUT); } + ClickButton::ClickButton(uint8_t buttonPin, boolean activeType, boolean internalPullup) { _pin = buttonPin; @@ -117,7 +120,6 @@ ClickButton::ClickButton(uint8_t buttonPin, boolean activeType, boolean internal } - void ClickButton::Update() { long now = (long)millis(); // get current time @@ -159,3 +161,73 @@ void ClickButton::Update() } } + +TouchButton::TouchButton(uint8_t buttonPin, uint8_t thresholdValue) +{ + _pin = buttonPin; + _activeHigh = LOW; // Assume active-low button + _btnState = !_activeHigh; // initial button state in active-high logic + _lastState = _btnState; + _clickCount = 0; + clicks = 0; + depressed = false; + _lastBounceTime= 0; + debounceTime = 20; // Debounce timer in ms + multiclickTime = 250; // Time limit for multi clicks + longClickTime = 500; // time until long clicks register + changed = false; + threshold = thresholdValue; +} + + +void TouchButton::Update() +{ + long now = (long)millis(); // get current time + +#if defined(ESP32) + if(touchRead(_pin) < threshold) // current appearant button state + { + _btnState = LOW; + } + else + { + _btnState = HIGH; + } +#endif + + // Make the button logic active-high in code + if (!_activeHigh) _btnState = !_btnState; + + // If the switch changed, due to noise or a button press, reset the debounce timer + if (_btnState != _lastState) _lastBounceTime = now; + + + // debounce the button (Check if a stable, changed state has occured) + if (now - _lastBounceTime > debounceTime && _btnState != depressed) + { + depressed = _btnState; + if (depressed) _clickCount++; + } + + if(_lastState == _btnState) changed = false; + _lastState = _btnState; + + // If the button released state is stable, report nr of clicks and start new cycle + if (!depressed && (now - _lastBounceTime) > multiclickTime) + { + // positive count for released buttons + clicks = _clickCount; + _clickCount = 0; + if(clicks != 0) changed = true; + } + + // Check for "long click" + if (depressed && (now - _lastBounceTime > longClickTime)) + { + // negative count for long clicks + clicks = 0 - _clickCount; + _clickCount = 0; + if(clicks != 0) changed = true; + } +} + diff --git a/ClickButton.h b/ClickButton.h index 9037272..2f0f399 100644 --- a/ClickButton.h +++ b/ClickButton.h @@ -33,5 +33,28 @@ class ClickButton long _lastBounceTime; // the last time the button input pin was toggled, due to noise or a press }; + +class TouchButton +{ + public: + TouchButton(uint8_t buttonPin, uint8_t thresholdValue); + void Update(); + int clicks; // button click counts to return + boolean depressed; // the currently debounced button (press) state (presumably it is not sad :) + long debounceTime; + long multiclickTime; + long longClickTime; + boolean changed; + int threshold; // threshold for touch button + private: + uint8_t _pin; // Arduino pin connected to the button + boolean _activeHigh; // Type of button: Active-low = 0 or active-high = 1 + boolean _btnState; // Current appearant button state + boolean _lastState; // previous button reading + int _clickCount; // Number of button clicks within multiclickTime milliseconds + long _lastBounceTime; // the last time the button input pin was toggled, due to noise or a press +}; + + #endif diff --git a/keywords.txt b/keywords.txt index c3ff45a..d32221a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -7,6 +7,7 @@ ####################################### ClickButton KEYWORD1 +TouchButton KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -30,5 +31,6 @@ maxPresses LITERAL2 debounceTime LITERAL2 multiclickTime LITERAL2 longClickTime LITERAL2 -changed LITERAL2 +changed LITERAL2 +threshold LITERAL2 From 6be4471e79f09fda90c82df36cea13ca9be9fe17 Mon Sep 17 00:00:00 2001 From: Petr Date: Sun, 20 Mar 2022 21:11:36 +0100 Subject: [PATCH 2/2] ... --- ClickButton.cpp | 39 ++++- ClickButton.h | 7 +- examples/MultiButtons/MultiButtons.ino | 230 ++++++++++++------------- 3 files changed, 158 insertions(+), 118 deletions(-) diff --git a/ClickButton.cpp b/ClickButton.cpp index 65f8311..310724a 100644 --- a/ClickButton.cpp +++ b/ClickButton.cpp @@ -69,10 +69,12 @@ ClickButton::ClickButton(uint8_t buttonPin) clicks = 0; depressed = false; _lastBounceTime= 0; + _longStartTime = 0; debounceTime = 20; // Debounce timer in ms multiclickTime = 250; // Time limit for multi clicks longClickTime = 1000; // time until long clicks register changed = false; + holdTime = 0; pinMode(_pin, INPUT); } @@ -87,10 +89,12 @@ ClickButton::ClickButton(uint8_t buttonPin, boolean activeType) clicks = 0; depressed = 0; _lastBounceTime= 0; + _longStartTime = 0; debounceTime = 20; // Debounce timer in ms multiclickTime = 250; // Time limit for multi clicks longClickTime = 1000; // time until long clicks register changed = false; + holdTime = 0; pinMode(_pin, INPUT); } @@ -105,10 +109,12 @@ ClickButton::ClickButton(uint8_t buttonPin, boolean activeType, boolean internal clicks = 0; depressed = 0; _lastBounceTime= 0; + _longStartTime = 0; debounceTime = 20; // Debounce timer in ms multiclickTime = 250; // Time limit for multi clicks longClickTime = 1000; // time until "long" click register changed = false; + holdTime = 0; //pinMode(_pin, INPUT); // Turn on internal pullup resistor if applicable //if (_activeHigh == LOW && internalPullup == CLICKBTN_PULLUP) digitalWrite(_pin,HIGH); @@ -162,6 +168,8 @@ void ClickButton::Update() } + + TouchButton::TouchButton(uint8_t buttonPin, uint8_t thresholdValue) { _pin = buttonPin; @@ -172,20 +180,47 @@ TouchButton::TouchButton(uint8_t buttonPin, uint8_t thresholdValue) clicks = 0; depressed = false; _lastBounceTime= 0; + _longStartTime = 0; + debounceTime = 20; // Debounce timer in ms + multiclickTime = 250; // Time limit for multi clicks + longClickTime = 500; // time until long clicks register + changed = false; + holdTime = 0; + thresholdHigh = thresholdValue; + thresholdLow = 0; +} + + +TouchButton::TouchButton(uint8_t buttonPin, uint8_t thresholdValueHigh, uint8_t thresholdValueLow) +{ + _pin = buttonPin; + _activeHigh = LOW; // Assume active-low button + _btnState = !_activeHigh; // initial button state in active-high logic + _lastState = _btnState; + _clickCount = 0; + clicks = 0; + depressed = false; + _lastBounceTime= 0; + _longStartTime = 0; debounceTime = 20; // Debounce timer in ms multiclickTime = 250; // Time limit for multi clicks longClickTime = 500; // time until long clicks register changed = false; - threshold = thresholdValue; + holdTime = 0; + thresholdHigh = thresholdValueHigh; + thresholdLow = thresholdValueLow; } + + void TouchButton::Update() { long now = (long)millis(); // get current time #if defined(ESP32) - if(touchRead(_pin) < threshold) // current appearant button state + int touchValue = touchRead(_pin); // read touch pin value + if((touchValue < thresholdHigh) && (touchValue > thresholdLow)) // is a touch value in the range? { _btnState = LOW; } diff --git a/ClickButton.h b/ClickButton.h index 2f0f399..2596c16 100644 --- a/ClickButton.h +++ b/ClickButton.h @@ -24,6 +24,7 @@ class ClickButton long multiclickTime; long longClickTime; boolean changed; + long holdTime; // How long has the button been held private: uint8_t _pin; // Arduino pin connected to the button boolean _activeHigh; // Type of button: Active-low = 0 or active-high = 1 @@ -31,6 +32,7 @@ class ClickButton boolean _lastState; // previous button reading int _clickCount; // Number of button clicks within multiclickTime milliseconds long _lastBounceTime; // the last time the button input pin was toggled, due to noise or a press + long _longStartTime; // When long click started }; @@ -45,7 +47,9 @@ class TouchButton long multiclickTime; long longClickTime; boolean changed; - int threshold; // threshold for touch button + long holdTime; // How long has the button been held + int thresholdHigh; // high threshold for touch button + int thresholdLow; // low threshold for touch button private: uint8_t _pin; // Arduino pin connected to the button boolean _activeHigh; // Type of button: Active-low = 0 or active-high = 1 @@ -53,6 +57,7 @@ class TouchButton boolean _lastState; // previous button reading int _clickCount; // Number of button clicks within multiclickTime milliseconds long _lastBounceTime; // the last time the button input pin was toggled, due to noise or a press + long _longStartTime; // When long click started }; diff --git a/examples/MultiButtons/MultiButtons.ino b/examples/MultiButtons/MultiButtons.ino index 0abf105..ae8f87a 100644 --- a/examples/MultiButtons/MultiButtons.ino +++ b/examples/MultiButtons/MultiButtons.ino @@ -1,115 +1,115 @@ -/* ClickButton library demo - - Demo of multiple ClickButton objects in an array. - Same as MultiClicks example, but with 3 buttons and 3 LED's. - - Short clicks: - - Single click - Toggle LED on/off - Double click - Blink (Toggles LED 2 times/second) - Triple click - Fast blink (Toggles LED 5 times/second) - - Long clicks (hold button for one second or longer on last click): - - Single-click - Slow blink (Toggles LED every second) - Double-click - Sloow blink (Toggles LED every other second) - Triple-click - Slooow blink (Toggles LED every three seconds) - - - The circuit: - - LEDs attached from pin 10,11 and 12 to resistors (say 220-ish ohms), other side of resistors to GND (ground) - - Pushbuttons attached from pin 4,5 and 6 to GND - No pullup resistor needed, using the Arduino's (Atmega's) internal pullup resistor in this example. - - Based on the Arduino Debounce example. - - 2010, 2013 raron - - GNU GPLv3 license -*/ - -#include "ClickButton.h" - -// Nr. of buttons in the array -const int buttons = 3; - -// the LED -const int ledPin[buttons] = { 10, 11, 12 }; // Arduino pins to the LEDs -int ledState[buttons] = { 0, 0, 0 }; -int LEDfunction[buttons] = { 0, 0, 0 }; - -// Arduino input pins from the buttons (these are not in an array for simplicity just now) -const int buttonPin1 = 4; -const int buttonPin2 = 5; -const int buttonPin3 = 6; - -// Instantiate ClickButton objects in an array -ClickButton button[3] = { - ClickButton (buttonPin1, LOW, CLICKBTN_PULLUP), - ClickButton (buttonPin2, LOW, CLICKBTN_PULLUP), - ClickButton (buttonPin3, LOW, CLICKBTN_PULLUP), -}; - - - - -void setup() -{ - for (int i=0; i