Skip to content

Commit 5b58654

Browse files
committed
New status added
1 parent a0cdb6b commit 5b58654

File tree

6 files changed

+59
-21
lines changed

6 files changed

+59
-21
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ be pressed for at least 0.5 seconds).
99

1010
ButtonSL is extended so that a distinction can be made between a short and long button press.
1111
A time (in ms) can be specified after which a button press is considered "long".
12-
Correspondingly, the tick() method returns the status notPressed, shortPressed or longPressed.
12+
Correspondingly, the tick() method returns the status notPressed, shortPressed or longPressed/pressed.
1313

1414
## Examples
1515

@@ -103,6 +103,15 @@ You can test this library with the online simulator wokwi:: [Button Example](htt
103103
* Debounce time: 30ms (max. 65535 ms).
104104
* For ButtonSL releaseOff()
105105

106+
## Notes
107+
108+
Version 1.1.6:
109+
The ButtonState::pressed status has been added.
110+
For a long button press with automatic release enabled, the ButtonState::longPressed status is returned once
111+
when the defined time for a long button press is reached and then the ButtonState::pressed status is returned
112+
as long as the button is held down. Previously (version 1.1.6), ButtonState::nopressed was returned even
113+
if the button had not yet been released.
114+
106115
## Methods
107116

108117
The methods of the objects are named so that their function should be self-explanatory:
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
#include <Button_SL.hpp>
2+
#include "Button_SL.hpp"
3+
4+
//////////////////////////////////////////////////////////////////////////////
5+
/// @brief Auxiliary class for time-controlled functions.
6+
///
7+
//////////////////////////////////////////////////////////////////////////////
8+
class Timer {
9+
public:
10+
void start() { timeStamp = millis(); }
11+
bool operator()(const unsigned long duration) const { return millis() - timeStamp >= duration; }
12+
13+
private:
14+
unsigned long timeStamp {0};
15+
};
216

317
//////////////////////////////////////////////////
418
// Global constants and variables
519
//////////////////////////////////////////////////
6-
constexpr uint8_t BUTTON_PIN1{2};
7-
constexpr uint8_t BUTTON_PIN2{3};
8-
constexpr uint8_t BUTTON_PIN3{4};
20+
constexpr uint8_t btn_pin1 {2};
21+
constexpr uint8_t btn_pin2 {3};
22+
constexpr uint8_t btn_pin3 {4};
23+
constexpr uint8_t led_short {6};
24+
constexpr uint8_t led_long {7};
25+
constexpr unsigned long duration {500};
26+
constexpr unsigned long blink_duration {200};
927

1028
using namespace Btn;
1129

12-
Button btn{BUTTON_PIN1};
30+
Button btn{btn_pin1};
1331
ButtonSL bArray[]{
14-
{BUTTON_PIN2},
15-
{BUTTON_PIN3, 1500, HIGH}
32+
{btn_pin2},
33+
{btn_pin3, 1500, HIGH}
1634
};
17-
//////////////////////////////////////////////////
18-
// Function forward declaration
19-
//////////////////////////////////////////////////
35+
Timer wait;
2036

2137
//////////////////////////////////////////////////////////////////////////////
2238
/// @brief Initialize the program.
@@ -25,14 +41,16 @@ ButtonSL bArray[]{
2541
void setup() {
2642
Serial.begin(115200);
2743
Serial.println("Start");
44+
pinMode(led_short, OUTPUT);
45+
pinMode(led_long, OUTPUT);
46+
2847
// Initialize Buttons
2948
for (auto &buttons : bArray) {
3049
buttons.begin();
3150
}
3251
btn.begin();
3352
btn.setDebounceTime_ms(20);
3453
bArray[1].releaseOn();
35-
3654
}
3755

3856
//////////////////////////////////////////////////////////////////////////////
@@ -45,12 +63,12 @@ void loop() {
4563

4664
switch (bArray[0].tick()) {
4765
case ButtonState::shortPressed:
48-
Serial.print("B1 . ");
66+
Serial.print("B1 (short) ");
4967
Serial.print(bArray[0].getDuration_ms());
5068
Serial.println(" ms");
5169
break;
5270
case ButtonState::longPressed:
53-
Serial.print("B1 + ");
71+
Serial.print("B1 (long) ");
5472
Serial.print(bArray[0].getDuration_ms());
5573
Serial.println(" ms");
5674
break;
@@ -59,16 +77,27 @@ void loop() {
5977

6078
switch (bArray[1].tick()) {
6179
case ButtonState::shortPressed:
62-
Serial.print("B2 . ");
80+
wait.start();
81+
digitalWrite(led_short, HIGH);
82+
Serial.print("B2 (short) ");
6383
Serial.print(bArray[1].getDuration_ms());
6484
Serial.println(" ms");
6585
break;
66-
case ButtonState::longPressed:
67-
Serial.print("B2 + ");
86+
case ButtonState::longPressed:
87+
Serial.print("B2 (long) ");
6888
Serial.print(bArray[1].getDuration_ms());
6989
Serial.println(" ms");
7090
Serial.println("Auto released");
7191
break;
72-
default: break;
92+
case ButtonState::pressed:
93+
if (wait(blink_duration)) {
94+
digitalWrite(led_long, !digitalRead(led_long));
95+
wait.start();
96+
}
97+
break;
98+
default:
99+
if (digitalRead(led_short) && wait(duration)) { digitalWrite(led_short, LOW); }
100+
if (digitalRead(led_long)) { digitalWrite(led_long, LOW); }
101+
break;
73102
}
74103
}

library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Button_SL",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"keywords": "button, debounce, signal, input, ouput",
55
"description": "Button_SL enables the query of buttons. The query is debounced. A query can be made as to whether the button was pressed for a short or long time.",
66
"repository": {

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Button_SL
2-
version=1.1.5
2+
version=1.1.6
33
author=Kai R.
44
maintainer=Kai R.
55
sentence=Button query

src/Button_SL.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ ButtonState ButtonSL::tick() {
107107
return ButtonState::longPressed;
108108
}
109109
}
110-
return ButtonState::notPressed;
110+
return hasReleased ? ButtonState::pressed : ButtonState::notPressed;
111111
}
112112

113113
//////////////////////////////////////////////////////////////////////////////

src/Button_SL.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ constexpr uint8_t DEBOUNCE_TIME{30}; // The value can be reduced for buttons t
3030
//////////////////////////////////////////////////
3131
// Variables and Classdefinitions
3232
//////////////////////////////////////////////////
33-
enum class ButtonState : uint8_t { notPressed, shortPressed, longPressed };
33+
enum class ButtonState : uint8_t { notPressed = false, pressed = true, shortPressed = 2, longPressed };
3434

3535
//////////////////////////////////////////////////////////////////////////////
3636
/// @brief Button object

0 commit comments

Comments
 (0)