diff --git a/ClickButton.cpp b/ClickButton.cpp index 7a1e1fe..310724a 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; @@ -67,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); } @@ -85,13 +89,16 @@ 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); } + ClickButton::ClickButton(uint8_t buttonPin, boolean activeType, boolean internalPullup) { _pin = buttonPin; @@ -102,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); @@ -117,7 +126,6 @@ ClickButton::ClickButton(uint8_t buttonPin, boolean activeType, boolean internal } - void ClickButton::Update() { long now = (long)millis(); // get current time @@ -159,3 +167,102 @@ 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; + _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; + holdTime = 0; + thresholdHigh = thresholdValueHigh; + thresholdLow = thresholdValueLow; +} + + + + +void TouchButton::Update() +{ + long now = (long)millis(); // get current time + +#if defined(ESP32) + int touchValue = touchRead(_pin); // read touch pin value + if((touchValue < thresholdHigh) && (touchValue > thresholdLow)) // is a touch value in the range? + { + _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..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,7 +32,34 @@ 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 }; + +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; + 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 + 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 + long _longStartTime; // When long click started +}; + + #endif 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