Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TouchPin added #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion ClickButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)!
Expand All @@ -57,6 +58,7 @@ NOTE!

#include "ClickButton.h"


ClickButton::ClickButton(uint8_t buttonPin)
{
_pin = buttonPin;
Expand All @@ -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);
}

Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -117,7 +126,6 @@ ClickButton::ClickButton(uint8_t buttonPin, boolean activeType, boolean internal
}



void ClickButton::Update()
{
long now = (long)millis(); // get current time
Expand Down Expand Up @@ -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;
}
}

28 changes: 28 additions & 0 deletions ClickButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,42 @@ 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
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
};


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

Loading