Skip to content

Commit 7f5fcca

Browse files
supports internal pull-up/pull-down, external pull-up/pull-down
1 parent 2b2d120 commit 7f5fcca

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
## ezButton Library for Arduino
1+
## ezButton Library for Arduino, ESP32, ESP8266...
22
This library is designed to make it easy to use push button, momentary switches, toggle switch, magnetic contact switch (door sensor)..​. It is easy to use for not only beginners but also experienced users.
33

44
__ezButton__ stands for __Easy Button__
55

66
Features
77
----------------------------
8-
* Uses the internal pull-up resistor to avoid the floating value
8+
* Uses the internal pull-up resistor by default to avoid the floating value
99
* Supports debounce to eliminate the chattering phenomenon
1010
* Supports the pressed and released events
1111
* Supports the counting (for FALLING, RISING and BOTH)
1212
* Easy to use with multiple buttons
1313
* All functions are non-blocking
14+
* Support internal pull-up/pull-down, external pull-up/pull-down
1415

1516
Available Examples
1617
----------------------------

keywords.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@ resetCount KEYWORD2
2828

2929
COUNT_FALLING LITERAL1
3030
COUNT_RISING LITERAL1
31-
COUNT_BOTH LITERAL1
31+
COUNT_BOTH LITERAL1
32+
INTERNAL_PULLUP LITERAL1
33+
INTERNAL_PULLDOWN LITERAL1
34+
EXTERNAL_PULLUP LITERAL1
35+
EXTERNAL_PULLDOWN LITERAL1

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name=ezButton
2-
version=1.0.4
2+
version=1.0.5
33
author=ArduinoGetStarted.com
44
maintainer=ArduinoGetStarted.com (ArduinoGetStarted@gmail.com)
5-
sentence=Button library for Arduino
5+
sentence=Button library for Arduino, ESP32, ESP8266...
66
paragraph=Button library supports debounce, pressed/released events and the press counting. It is easy to use with multiple buttons. The library can be used for push-button, momentary switches, toggle switch, magnetic contact switch (door sensor)... It is designed for not only beginners but also experienced users.
77
category=Signal Input/Output
88
url=https://arduinogetstarted.com/tutorials/arduino-button-library

src/ezButton.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,28 @@
3131

3232
#include <ezButton.h>
3333

34-
ezButton::ezButton(int pin): ezButton(pin, INPUT_PULLUP) {};
34+
ezButton::ezButton(int pin): ezButton(pin, INTERNAL_PULLUP) {};
3535

3636
ezButton::ezButton(int pin, int mode) {
3737
btnPin = pin;
3838
debounceTime = 0;
3939
count = 0;
4040
countMode = COUNT_FALLING;
41+
42+
if (mode == INTERNAL_PULLUP || mode == INTERNAL_PULLDOWN) {
43+
pinMode(btnPin, mode);
44+
} else if (mode == EXTERNAL_PULLUP || mode == EXTERNAL_PULLDOWN) {
45+
pinMode(btnPin, INPUT); // External pull-up/pull-down, set as INPUT
46+
}
4147

42-
pinMode(btnPin, mode);
48+
// Set the pressed and unpressed states based on the mode
49+
if (mode == INTERNAL_PULLDOWN || mode == EXTERNAL_PULLDOWN) {
50+
pressedState = HIGH;
51+
unpressedState = LOW;
52+
} else {
53+
pressedState = LOW;
54+
unpressedState = HIGH;
55+
}
4356

4457
previousSteadyState = digitalRead(btnPin);
4558
lastSteadyState = previousSteadyState;
@@ -61,14 +74,14 @@ int ezButton::getStateRaw(void) {
6174
}
6275

6376
bool ezButton::isPressed(void) {
64-
if(previousSteadyState == HIGH && lastSteadyState == LOW)
77+
if(previousSteadyState == unpressedState && lastSteadyState == pressedState)
6578
return true;
6679
else
6780
return false;
6881
}
6982

7083
bool ezButton::isReleased(void) {
71-
if(previousSteadyState == LOW && lastSteadyState == HIGH)
84+
if(previousSteadyState == pressedState && lastSteadyState == unpressedState)
7285
return true;
7386
else
7487
return false;

src/ezButton.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@
3838
#define COUNT_RISING 1
3939
#define COUNT_BOTH 2
4040

41+
// Constants for button modes
42+
#define INTERNAL_PULLUP INPUT_PULLUP
43+
#define INTERNAL_PULLDOWN INPUT_PULLDOWN
44+
#define EXTERNAL_PULLUP 2
45+
#define EXTERNAL_PULLDOWN 3
46+
4147
class ezButton
4248
{
4349
private:
4450
int btnPin;
4551
unsigned long debounceTime;
4652
unsigned long count;
4753
int countMode;
54+
int pressedState; // the state when the button is considered pressed
55+
int unpressedState; // the state when the button is considered unpressed
4856

4957
int previousSteadyState; // the previous steady state from the input pin, used to detect pressed and released event
5058
int lastSteadyState; // the last steady state from the input pin

0 commit comments

Comments
 (0)