Compare commits

...
Author SHA1 Message Date
Thomas O Fredericks 371e2cff35 Update bounce_m5stack_atom_key-unit.ino 2024-01-11 12:24:10 -05:00
Thomas O Fredericks 9276539335 Added an example for the m5stack atom 2024-01-11 12:10:11 -05:00
Thomas O Fredericks cda0200633 Update README.md 2024-01-11 11:57:37 -05:00
Thomas O Fredericks ff821cb300 Removed old documentation and updated version 2024-01-11 11:53:14 -05:00
Thomas O Fredericks b7cd534fb7 Update README.md 2024-01-11 11:50:52 -05:00
Thomas O Fredericks 699b0e429c Update README.md 2024-01-11 11:47:09 -05:00
Thomas O Fredericks 71568702f2 Update README.md 2024-01-11 11:43:46 -05:00
Thomas O Fredericks d2c9f1673c Updated the documentation 2024-01-11 11:40:42 -05:00
Thomas O Fredericks ee74d73978 Merge pull request #90 from WAMaker/fix/typo
fix typo
2022-09-16 10:31:33 -04:00
WAMaker d744e0690f chore: fix typo 2022-09-14 23:16:39 +08:00
Thomas O Fredericks 152a64231a Changed documention on where to put defines 2022-03-28 19:56:01 -04:00
Thomas O Fredericks 79db9f0016 Added back duration but as deprecated and updated documentation 2022-03-21 10:04:38 -04:00
Thomas O Fredericks 5e100280c5 Updated Doxygen docs 2021-08-03 11:01:07 -04:00
Thomas O Fredericks 2ba0be8f83 Changed duration() to currentDuration() and added examples 2021-08-03 10:59:00 -04:00
Thomas O Fredericks 28afcafa6a Updated doc to 2.60 2021-06-29 01:30:28 -04:00
Thomas O Fredericks 1645b897c8 Added getPin 2021-06-28 16:41:16 -04:00
Thomas O Fredericks 68dc6f7830 Adding a little documentation 2021-06-28 16:33:17 -04:00
Thomas O Fredericks f87ba5ba9f Update library.properties 2021-03-04 15:53:16 -05:00
Thomas O Fredericks 246096ae96 Update library.json 2021-03-04 15:53:02 -05:00
Thomas O Fredericks 1ce882f949 Merge pull request #78 from DanForever/master
Made the bounce2 library const correct
2021-03-04 15:49:53 -05:00
Dan Neve a132f4ab43 Made the bounce2 library const correct 2021-03-04 19:14:11 +01:00
Thomas O Fredericks e89801c2dd Merge pull request #77 from michaelcress/master
Correcting spelling error in README file
2021-03-04 00:18:06 -05:00
Michael Cress 744a957b76 Correcting spelling error 2021-03-03 05:59:07 -05:00
Thomas O Fredericks cd87a100be Delete Bounce2NameSpace.h 2021-01-29 18:45:51 -05:00
Thomas O Fredericks 6f244834a4 Merge pull request #74 from mkfrey/master
Add a component.mk file to allow using this project as a component of esp-idf (< 4.0).
2021-01-29 13:21:46 -05:00
Thomas O Fredericks 5a481c6922 Moving Button to Bounce2 namespace 2021-01-23 18:05:58 -05:00
Markus Frey 4207a8dd17 Add component.mk 2021-01-19 17:35:14 +01:00
Thomas O Fredericks eb5ab9fad8 Update README.md 2020-09-29 10:33:17 -04:00
Thomas O Fredericks c445bbc7a7 Update README.md 2020-09-29 10:32:59 -04:00
Thomas O Fredericks c012c6c042 Merge pull request #67 from jamesmyatt/better_button
Methods to determine whether Button is currently pressed
2020-09-03 09:28:58 -04:00
James Myatt 83bc18e245 Revert "Reuse state attribute for pressed state"
This reverts commit 9babb4fc38.
2020-08-26 13:53:35 +01:00
Thomas O Fredericks 3a88a924f2 Fixed more "sloppy programming" 2020-08-25 14:24:38 -04:00
Thomas O Fredericks fd99238112 Fixed "sloppy programming" in main examples 2020-08-25 14:23:11 -04:00
James Myatt 9babb4fc38 Reuse state attribute for pressed state 2020-08-25 13:59:55 +01:00
James Myatt 878f86bf1c Add isPressed and getPressedState methods 2020-08-25 13:54:33 +01:00
Thomas O Fredericks 59d3c66c56 Removed Todo 2020-08-08 10:57:24 -04:00
Thomas O Fredericks 08392e0f01 Removed Todo 2020-08-08 10:56:06 -04:00
Thomas O Fredericks 3ddefb4ba4 Updated documentation 2020-08-08 10:21:57 -04:00
Thomas O Fredericks 172e3e0835 Merge pull request #63 from thomasfredericks/Button
Button
2020-08-06 17:42:30 -04:00
Thomas O Fredericks f51b22b708 Updated documentation 2020-08-06 17:37:25 -04:00
Thomas O Fredericks 1a6552ffe5 New Debouncer class split from the hardware layer 2020-08-06 13:34:56 -04:00
Thomas O Fredericks 2b6be81dc1 Updated docs and version number 2020-05-21 16:03:08 -04:00
Thomas O Fredericks 6147a34058 Changed the default stateForPressed and fixed example 2020-05-21 15:45:52 -04:00
Thomas O Fredericks 1af864b0bd Added a Button class 2020-05-19 14:06:37 -04:00
Thomas O Fredericks cf32eda85e v2.53 2020-02-26 14:42:35 -05:00
Thomas O Fredericks a57ba4c66a Update library.properties 2020-02-26 14:41:36 -05:00
Thomas O Fredericks 85d80fe400 Merge pull request #58 from fpistm/patch-1
Generic Arduino_STM32 support
2020-02-20 18:48:35 -05:00
Frederic Pillon 14f9c5c4a6 Generic Arduino_STM32 support
When BluePill support have been introduced, it was not enough generic
https://github.com/thomasfredericks/Bounce2/pull/35

Board specific definition have been used instead of the generic core one:
`ARDUINO_ARCH_STM32F1`
2020-02-14 08:05:43 +01:00
Thomas O Fredericks 9335056c46 Merge pull request #57 from dwrobel/dw-fix-compilation-error
Fix compilation error
2019-11-26 08:08:29 -05:00
Damian Wrobel 26e216c98d Fix compilation error
libraries/Bounce2/src/Bounce2.h:209:10: error: extra qualification 'Bounce::' on member 'changed' [-fpermissive]
  209 |     bool Bounce::changed( ) { return getStateFlag(CHANGED_STATE); }
      |          ^~~~~~

Regression introduced in 01e6fc9398

Signed-off-by: Damian Wrobel <dwrobel@ertelnet.rybnik.pl>
2019-11-25 14:42:05 +01:00
Thomas O Fredericks 6228e09451 Moved literals to header as suggested by MartinNel 2019-11-21 22:09:13 -05:00
Thomas O Fredericks 51032991ac Update Bounce2.h 2019-11-07 09:33:43 -05:00
Thomas O Fredericks de69c15225 Merge pull request #55 from thomasfredericks/Testing
Added a changed() function
2019-11-07 09:30:31 -05:00
Thomas O Fredericks 220a287467 Made changed() public instead of protected 2019-10-06 13:01:12 -04:00
Thomas O Fredericks 01e6fc9398 Added a protected changed() function 2019-10-05 16:14:33 -04:00
Thomas O Fredericks 3e4738b4ef Added Bounce2 as a KEYWORD1 2019-05-23 10:47:16 -04:00
Thomas O Fredericks 3706e16e4b Merge pull request #50 from trapper-/master
Fix extraneous @return in function comment
2019-02-06 19:22:17 -05:00
trapper- 9a275c4f96 Update Bounce2.h
Fix extraneous @return line in function comment
2019-02-06 14:30:05 +08:00
Thomas O Fredericks d422d7c054 Added previous duration 2018-09-11 10:09:57 -04:00
Thomas O Fredericks d3ef0365eb Version 2.52 2018-08-03 15:18:51 -04:00
Thomas O Fredericks f5203d0fb8 Merge pull request #48 from per1234/keywords-separator
Remove leading space from keywords.txt identifier
2018-08-03 15:14:50 -04:00
per1234 c2e3cd5a2d Remove leading space from keywords.txt identifier
Each field of keywords.txt is separated by a single true tab. Leading spaces on a keyword identifier causes it to not be recognized by the Arduino IDE. On Arduino IDE 1.6.5 and newer an unrecognized keyword identifier causes the default editor.function.style highlighting to be used (as with KEYWORD2, KEYWORD3).

Reference:
https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification#keywords
2018-08-03 10:20:43 -07:00
Thomas O Fredericks 32c1c48b26 Moved documentation and fixed version number 2018-07-19 08:38:38 -04:00
Thomas O Fredericks 5a132024fc Updating version to 2.5 2018-07-18 12:45:54 -04:00
Thomas O Fredericks cbe0b980b7 Updaing documentation 2018-07-18 12:44:12 -04:00
Thomas O Fredericks a1c83e9d7d Updating documentation 2018-07-18 12:34:50 -04:00
Thomas O Fredericks f19d27689f Updating documentation 2018-07-18 12:24:20 -04:00
Thomas O Fredericks 045183b598 Updating documentation 2018-07-18 12:09:10 -04:00
Thomas O Fredericks 2ba409cd4a Where did the docs go? 2018-07-16 19:10:25 -04:00
Thomas O Fredericks 47d7fab6ac No idea 2018-07-16 19:09:09 -04:00
Thomas O Fredericks d0443e3f3d resolved merge conflict 2018-07-16 19:08:38 -04:00
Thomas O Fredericks 0f5acfd2c4 First test with doxygen 2018-07-16 19:05:12 -04:00
Thomas O Fredericks cbb12331c9 Set theme jekyll-theme-dinky 2018-07-16 18:39:06 -04:00
Thomas O Fredericks 2c83e7f19c Testing Jekyll theme 2018-07-16 18:32:30 -04:00
Thomas O Fredericks a5ee5f9e79 Set theme jekyll-theme-minimal 2018-07-16 18:28:53 -04:00
Thomas O Fredericks 9d91d62d1e New documentation integration 2018-07-16 18:28:03 -04:00
Thomas O Fredericks c0be1e178a Removing readthecods integration 2018-07-16 18:25:04 -04:00
Thomas O Fredericks 742dfe9668 Making compatible with arduinodocs 2018-07-16 17:15:10 -04:00
Thomas O Fredericks 527524112d Added Doxygon type documentation 2018-07-16 17:05:53 -04:00
Thomas O Fredericks 99e9334994 Added duration() 2018-07-16 16:37:41 -04:00
20 changed files with 1053 additions and 266 deletions
+2 -1
View File
@@ -1 +1,2 @@
*.db
*.db
*.yml
+218 -25
View File
@@ -1,42 +1,235 @@
BOUNCE 2
=====================
# BOUNCE 2
Debouncing library for Arduino or Wiring
Debouncing library for Arduino and Wiring by Thomas Ouellet Fredericks and many [contributors](https://github.com/thomasfredericks/Bounce2/graphs/contributors).
by Thomas Ouellet Fredericks
The mechanical part of buttons and switches vibrate slightly when closed or opened causing multiple undesired false states (similar to noise). This library filters out these undesired state changes. You can learn more about debouncing here:
* John Errington's Experiments with an Arduino : [Using digital inputs: Switch bounce and solutions to it](http://www.skillbank.co.uk/arduino/switchbounce.htm)
* Wikipedia article : http://en.wikipedia.org/wiki/Debounce#Contact_bounce
with contributions from:
The library is composed of three classes:
* Bounce2::Button : The most feature rich class for deboucing hardware buttons. The one that most people will use.
* Debouncer : The code that does the actual debouncing. Only advanced users should play with this class.
* Bounce : This class links the Debouncer to a hardware pin on your board. It is odly named because it needs to be backward compatible to previous versions of this library.
Eric Lowry, Jim Schimpf, Tom Harkaway and Joachim Krüger.
# HAVE A QUESTION?
Please post your usage questions on the [Arduino Forums](https://forum.arduino.cc/latest).
# INSTALLATION & DOWNLOAD
Install through your software's Library Manager or download the latest version [here](https://github.com/thomasfredericks/Bounce2/archive/master.zip) and put the "Bounce2" folder in your "libraries" folder.
Please note that the original version of this library (Bounce 1) is included in the "extras" folder of the download but not supported anymore.
# BASIC USE
## ADD TO THE GLOBAL SPACE
```cpp
#include <Bounce2.h>
Bounce2::Button button = Bounce2::Button(); // INSTANTIATE A Bounce2::Button OBJECT
```
## CONFIGURE IN SETUP()
In the code sample below :
* Change *PIN* to the hardware pin of the button.
* Change *PIN_MODE* to `INPUT_PULLUP` if using an internal pullup, or `INPUT` if using an external pullup.
* Change *INTERVAL_IN_MS* to the debounce interval in millisecons. `5` is a good value.
* Change *PRESSED_STATE* to `LOW` if the button outputs a `LOW` when pressed, or to `HIGH` if the button outputs a `HIGH` when pressed.
```cpp
button.attach ( PIN , PIN_MODE );
button.interval( INTERVAL_IN_MS );
button.setPressedState( PRESSED_STATE );
```
HAVE A QUESTION?
=====================
Please post your questions here :
http://forum.arduino.cc/index.php?topic=266132.0
## USE IN LOOP()
DOWNLOAD
=====================
```cpp
// UPDATE THE BUTTON BY CALLING .update() AT THE BEGINNING OF THE LOOP:
button.update();
Download the latest version (version 2) here :
https://github.com/thomasfredericks/Bounce2/archive/master.zip
// IF THE BUTTON WAS PRESSED THIS LOOP:
if ( button.pressed() ) {
// DO SOMETHING IF THE BUTTON WAS PRESSED THIS LOOP...
}
```
INSTALLATION
=====================
Put the "Bounce2" folder in your Arduino or Wiring "libraries" folder.
# EXAMPLES
You can find many examples in the **examples** folder.
## EXAMPLE OF Bounce2::Button TOGGLING THE STATE OF A LED
```cpp
/*
DESCRIPTION
====================
This is an example of the Bounce2::Button class.
When the user presses a physical button, it toggles a LED on or off.
The Button class matches an electrical state to a physical action.
Use .setPressedState(LOW or HIGH) to set the detection state for when the button is pressed.
INSTRUCTIONS
====================
Set BUTTON_PIN to the pin attached to the button.
Set LED_PIN to the pin attached to a LED.
*/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
// INSTANTIATE A Button OBJECT
Bounce2::Button button = Bounce2::Button();
// WE WILL attach() THE BUTTON TO THE FOLLOWING PIN IN setup()
#define BUTTON_PIN 39
// DEFINE THE PIN FOR THE LED :
// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)
//#define LED_PIN LED_BUILTIN
// 2) OTHERWISE SET YOUR OWN PIN
#define LED_PIN 13
// SET A VARIABLE TO STORE THE LED STATE
bool ledState = LOW;
void setup() {
// BUTTON SETUP
// SELECT ONE OF THE FOLLOWING :
// 1) IF YOUR BUTTON HAS AN INTERNAL PULL-UP
// button.attach( BUTTON_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP
// 2) IF YOUR BUTTON USES AN EXTERNAL PULL-UP
button.attach( BUTTON_PIN, INPUT ); // USE EXTERNAL PULL-UP
// DEBOUNCE INTERVAL IN MILLISECONDS
button.interval(5);
// INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON
button.setPressedState(LOW);
// LED SETUP
pinMode(LED_PIN,OUTPUT);
digitalWrite(LED_PIN,ledState);
}
void loop() {
// UPDATE THE BUTTON
// YOU MUST CALL THIS EVERY LOOP
button.update();
if ( button.pressed() ) {
// TOGGLE THE LED STATE :
ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
digitalWrite(LED_PIN,ledState);
}
}
```
# DOCUMENTATION
## Bounce2::Button
The `Bounce2::Button` class extends `Bounce` and adds `pressed()` and `released()` methods. It is the most feature rich class of the library.
| Method | Description |
| --------------- | --------------- |
| `Button()` | Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical level. |
| `void` `update()` | Updates the pin's state. Because Bounce does not use interrupts, you have to "update" the object before reading its value and it has to be done as often as possible (that means to include it in your `loop()`). Only call `update()` for each Bounce instance once per `loop()`. |
| `void` `setPressedState(bool state)` | Set the electrical state (HIGH/LOW) that corresponds to a physical press. By default, the pressed state is matched to a HIGH electrical level. |
| `bool` `getPressedState()` | Get the electrical state (HIGH/LOW) that corresponds to a physical press. |
| `bool ` `isPressed() ` | Returns true if the button **is** currently pressed. |
| `bool ` `pressed()` | Returns true if the button **was** pressed |
| `bool ` `released()` | Returns true if the button **was** released |
| `void` `attach(int pin, int mode)` | Attach to a pin and sets that pin's mode (INPUT, INPUT_PULLUP or OUTPUT). |
| `int` `getPin()` | Return pin that this instance is attached to. |
| `void` `interval ( uint16_t interval_millis )` | Sets the debounce interval in milliseconds. |
| `unsigned long` `previousDuration()` | Returns the duration in milliseconds of the previous state. |
| `unsigned long` `currentDuration()` | Returns the duration in milliseconds of the current state. Is reset to 0 when the state changes. |
| `bool` `changed()` | Returns true if the state changed on last update. |
| `bool` `read()` | Returns the pin's state (HIGH or LOW). |
| `bool` `fell()` | Returns true if pin signal transitions from high to low since the last update. |
| `bool` `rose()` | Returns true if pin signal transitions from low to high since the last update. |
DOCUMENTATION
=====================
## Bounce
The latest version (version 2) documentation can be found here :
The `Bounce` class extends `Debouncer` and links it to a hardware pin. This class is odly named, but it will be kept that so it stays compatible with previous code.
https://github.com/thomasfredericks/Bounce2/wiki
| Method | Description |
| --------------- | --------------- |
| `Bounce()` | Create an instance of the Bounce class. |
| `void` `update()` | Updates the pin's state. Because Bounce does not use interrupts, you have to "update" the object before reading its value and it has to be done as often as possible (that means to include it in your `loop()`). Only call `update()` for each Bounce instance once per `loop()`. |
| `void` `attach(int pin, int mode)` | Attach to a pin and sets that pin's mode (INPUT, INPUT_PULLUP or OUTPUT). |
| `int` `getPin()` | Return pin that this instance is attached to. |
| `void` `interval ( uint16_t interval_millis )` | Sets the debounce interval in milliseconds. |
| `unsigned long` `previousDuration()` | Returns the duration in milliseconds of the previous state. |
| `unsigned long` `currentDuration()` | Returns the duration in milliseconds of the current state. Is reset to 0 when the state changes. |
| `bool` `changed()` | Returns true if the state changed on last update. |
| `bool` `read()` | Returns the pin's state (HIGH or LOW). |
| `bool` `fell()` | Returns true if pin signal transitions from high to low since the last update. |
| `bool` `rose()` | Returns true if pin signal transitions from low to high since the last update. |
## Debouncer
The `Debouncer` class. Just the debouncing code separated from all harware.
| Method | Description |
| --------------- | --------------- |
| `Debouncer()` | Create an instance of the Debouncer class.|
| `void` `update()` | Updates the pin's state. Because Bounce does not use interrupts, you have to "update" the object before reading its value and it has to be done as often as possible (that means to include it in your `loop()`). Only call `update()` for each Bounce instance once per `loop()`. |
| `void` `interval ( uint16_t interval_millis )` | Sets the debounce interval in milliseconds. |
| `unsigned long` `previousDuration()` | Returns the duration in milliseconds of the previous state. |
| `unsigned long` `currentDuration()` | Returns the duration in milliseconds of the current state. Is reset to 0 when the state changes. |
| `bool` `changed()` | Returns true if the state changed on last update. |
| `bool` `read()` | Returns the pin's state (HIGH or LOW). |
| `bool` `fell()` | Returns true if pin signal transitions from high to low since the last update. |
| `bool` `rose()` | Returns true if pin signal transitions from low to high since the last update. |
# GITHUB PAGE (SOURCE CODE)
https://github.com/thomasfredericks/Bounce2
ABOUT BOUNCE 1 (ORIGINAL VERSION)
=====================
# ALTERNATE DEBOUNCE ALGORITHMS FOR **ADVANCED** USERS AND SPECIFIC CASES
## STABLE INTERVAL
By default, the Bounce library uses a stable interval to process the debouncing. This is simpler to understand and can cancel unwanted noise.
![](https://raw.github.com/thomasfredericks/Bounce-Arduino-Wiring/master/extras/BouncySwitch_stable.png)
## LOCK-OUT INTERVAL
By defining "#define BOUNCE_LOCK_OUT" in "Bounce.h" you can activate an alternative debouncing method. This method is a lot more responsive, but does not cancel noise.
```
#define BOUNCE_LOCK_OUT
```
![](https://raw.github.com/thomasfredericks/Bounce-Arduino-Wiring/master/extras/BouncySwitch_lockout.png)
## WITH PROMPT DETECTION
By defining "#define BOUNCE_WITH_PROMPT_DETECTION" in "Bounce.h" you can activate an alternative debouncing method. Button state changes are available immediately so long as the previous state has been stable for the timeout period. Otherwise the state will be updated as soon as the timeout period allows.
* Able to report acurate switch time normally with no delay.
* Use when accurate switch transition timing is important.
```
#define BOUNCE_WITH_PROMPT_DETECTION
```
The original version of Bounce (Bounce 1) is included but not supported anymore.
-48
View File
@@ -1,48 +0,0 @@
/*
DESCRIPTION
====================
Simple example of the Bounce library that switches the debug LED when a button is pressed.
*/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define BUTTON_PIN 2
#define LED_PIN 13
// Instantiate a Bounce object
Bounce debouncer = Bounce();
void setup() {
// Setup the button with an internal pull-up :
pinMode(BUTTON_PIN,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer.attach(BUTTON_PIN);
debouncer.interval(5); // interval in ms
//Setup the LED :
pinMode(LED_PIN,OUTPUT);
}
void loop() {
// Update the Bounce instance :
debouncer.update();
// Get the updated value :
int value = debouncer.read();
// Turn on or off the LED as determined by the state :
if ( value == LOW ) {
digitalWrite(LED_PIN, HIGH );
}
else {
digitalWrite(LED_PIN, LOW );
}
}
+70
View File
@@ -0,0 +1,70 @@
/*
DESCRIPTION
====================
Simple example of the Bounce library that switches a LED when
a state change (from HIGH to LOW) is triggered (for example when a button is pressed).
Set BOUNCE_PIN to the pin attached to the input (a button for example).
Set LED_PIN to the pin attached to a LED.
*/
// WE WILL attach() THE Bounce INSTANCE TO THE FOLLOWING PIN IN setup()
#define BOUNCE_PIN 2
// DEFINE THE PIN FOR THE LED :
// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)
#define LED_PIN LED_BUILTIN
// 2) OTHERWISE SET YOUR OWN PIN
// #define LED_PIN 13
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
// INSTANTIATE A Bounce OBJECT
Bounce bounce = Bounce();
// SET A VARIABLE TO STORE THE LED STATE
int ledState = LOW;
void setup() {
// BOUNCE SETUP
// SELECT ONE OF THE FOLLOWING :
// 1) IF YOUR INPUT HAS AN INTERNAL PULL-UP
bounce.attach( BOUNCE_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP
// 2) IF YOUR INPUT USES AN EXTERNAL PULL-UP
//bounce.attach( BOUNCE_PIN, INPUT ); // USE EXTERNAL PULL-UP
// DEBOUNCE INTERVAL IN MILLISECONDS
bounce.interval(5); // interval in ms
// LED SETUP
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, ledState);
}
void loop() {
// Update the Bounce instance (YOU MUST DO THIS EVERY LOOP)
bounce.update();
// <Bounce>.changed() RETURNS true IF THE STATE CHANGED (FROM HIGH TO LOW OR LOW TO HIGH)
if ( bounce.changed() ) {
// THE STATE OF THE INPUT CHANGED
// GET THE STATE
int deboucedInput = bounce.read();
// IF THE CHANGED VALUE IS LOW
if ( deboucedInput == LOW ) {
ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
digitalWrite(LED_PIN,ledState); // WRITE THE NEW ledState
}
}
}
+77
View File
@@ -0,0 +1,77 @@
/*
DESCRIPTION
====================
This is an example of the Bounce2::Button class.
When the user presses a physical button, it toggles a LED on or off.
The Button class matches an electrical state to a physical action.
Use .setPressedState(LOW or HIGH) to set the detection state for when the button is pressed.
INSCRUCTIONS
====================
Set BUTTON_PIN to the pin attached to the button.
Set LED_PIN to the pin attached to a LED.
*/
// WE WILL attach() THE BUTTON TO THE FOLLOWING PIN IN setup()
#define BUTTON_PIN 2
// DEFINE THE PIN FOR THE LED :
// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)
//#define LED_PIN LED_BUILTIN
// 2) OTHERWISE SET YOUR OWN PIN
#define LED_PIN 13
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
// INSTANTIATE A Button OBJECT FROM THE Bounce2 NAMESPACE
Bounce2::Button button = Bounce2::Button();
// SET A VARIABLE TO STORE THE LED STATE
int ledState = LOW;
void setup() {
// BUTTON SETUP
// SELECT ONE OF THE FOLLOWING :
// 1) IF YOUR BUTTON HAS AN INTERNAL PULL-UP
// button.attach( BUTTON_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP
// 2) IF YOUR BUTTON USES AN EXTERNAL PULL-UP
button.attach( BUTTON_PIN, INPUT ); // USE EXTERNAL PULL-UP
// DEBOUNCE INTERVAL IN MILLISECONDS
button.interval(5);
// INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON
button.setPressedState(LOW);
// LED SETUP
pinMode(LED_PIN,OUTPUT);
digitalWrite(LED_PIN,ledState);
}
void loop() {
// UPDATE THE BUTTON
// YOU MUST CALL THIS EVERY LOOP
button.update();
// <Button>.pressed() RETURNS true IF THE STATE CHANGED
// AND THE CURRENT STATE MATCHES <Button>.setPressedState(<HIGH or LOW>);
// WHICH IS LOW IN THIS EXAMPLE AS SET WITH button.setPressedState(LOW); IN setup()
if ( button.pressed() ) {
// TOGGLE THE LED STATE :
ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
digitalWrite(LED_PIN,ledState); // WRITE THE NEW ledState
}
}
-48
View File
@@ -1,48 +0,0 @@
// Detect the falling edge
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define BUTTON_PIN 2
#define LED_PIN 13
int ledState = LOW;
// Instantiate a Bounce object :
Bounce debouncer = Bounce();
void setup() {
// Setup the button with an internal pull-up :
pinMode(BUTTON_PIN,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer.attach(BUTTON_PIN);
debouncer.interval(500);
// Setup the LED :
pinMode(LED_PIN,OUTPUT);
digitalWrite(LED_PIN,ledState);
}
void loop() {
// Update the Bounce instance :
debouncer.update();
// Call code if Bounce fell (transition from HIGH to LOW) :
if ( debouncer.fell() ) {
// Toggle LED state :
ledState = !ledState;
digitalWrite(LED_PIN,ledState);
}
}
-53
View File
@@ -1,53 +0,0 @@
/*
DESCRIPTION
====================
Reports through serial (57600 baud) the time since
a button press (transition from HIGH to LOW).
*/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define BUTTON_PIN 2
#define LED_PIN 13
// Instantiate a Bounce object :
Bounce debouncer = Bounce();
unsigned long buttonPressTimeStamp;
void setup() {
Serial.begin(57600);
// Setup the button with an internal pull-up :
pinMode(BUTTON_PIN,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer.attach(BUTTON_PIN);
debouncer.interval(5);
// Setup the LED :
pinMode(LED_PIN,OUTPUT);
}
void loop() {
// Update the Bounce instance :
debouncer.update();
// Call code if Bounce fell (transition from HIGH to LOW) :
if ( debouncer.fell() ) {;
Serial.println( millis()-buttonPressTimeStamp );
buttonPressTimeStamp = millis();
}
}
@@ -0,0 +1,67 @@
/*
DESCRIPTION
====================
Simple example of the Bounce library that switches on a LED when
the decounced input is held for more than 1000 milliseconds.
Set BOUNCE_PIN to the pin attached to the input (a button for example).
Set LED_PIN to the pin attached to a LED.
*/
// WE WILL attach() THE Bounce INSTANCE TO THE FOLLOWING PIN IN setup()
#define BOUNCE_PIN 2
// DEFINE THE PIN FOR THE LED :
// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)
#define LED_PIN LED_BUILTIN
// 2) OTHERWISE SET YOUR OWN PIN
// #define LED_PIN 13
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
// INSTANTIATE A Bounce OBJECT.
Bounce bounce = Bounce();
// SET A VARIABLE TO STORE THE LED STATE
int ledState = LOW;
void setup() {
// BOUNCE SETUP
// SELECT ONE OF THE FOLLOWING :
// 1) IF YOUR INPUT HAS AN INTERNAL PULL-UP
bounce.attach( BOUNCE_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP
// 2) IF YOUR INPUT USES AN EXTERNAL PULL-UP
//bounce.attach( BOUNCE_PIN, INPUT ); // USE EXTERNAL PULL-UP
// DEBOUNCE INTERVAL IN MILLISECONDS
bounce.interval(5); // interval in ms
// LED SETUP
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, ledState);
}
void loop() {
// Update the Bounce instance (YOU MUST DO THIS EVERY LOOP)
bounce.update();
// GET THE STATE WITH <Bounce.read()>
int debouncedState = bounce.read();
// <Bounce>.duration() RETURNS THE TIME IN MILLISECONDS THE CURRENT STATE HAS BEEN HELD.
// SO WE CHECK IF THE STATE IS LOW AND IF IT HAS BEEN LOW FOR MORE THAN 1 SECOND.
if ( debouncedState == LOW && bounce.currentDuration() > 1000 ) {
digitalWrite(LED_PIN,HIGH); // TURN THE LED ON
} else {
digitalWrite(LED_PIN,LOW); // TURN THE LED OFF
}
}
@@ -0,0 +1,67 @@
// Le code de base pour le M5Stack Atom
// Inclure la librairie M5 (version pour M5Atom) :
// https://github.com/m5stack/M5Atom
#include <M5Atom.h>
// Inclure la librairie FastLED qui va gérer le pixel :
// https://github.com/FastLED/FastLED
#include <FastLED.h>
// Un tableau qui contient une variable de type CRGB.
// Il y a un seul pixel, mais il doit être dans un tableau.
// CRGB est un type de couleur défini par la lirairie FastLed :
// https://github.com/FastLED/FastLED/wiki/Pixel-reference#crgb-reference
CRGB mesPixels[1];
#include <Bounce2.h>
Bounce2::Button button = Bounce2::Button(); // INSTANTIATE A Bounce2::Button OBJECT
bool ledToggle = false;
void setup() {
// Démarrer la libraire M5 avec toutes les options de pré-configuration désactivées :
M5.begin(false, false, false);
// Démarrer la connexion sérielle :
Serial.begin(115200);
// Ajouter le pixel (il y en a un seul) du M5Atom à la librairie FastLED :
FastLED.addLeds<WS2812, DATA_PIN, GRB>(mesPixels, 1);
// Animation de démarrage
while (millis() < 5000) {
mesPixels[0] = CHSV((millis() / 5) % 255, 255, 255 - (millis() * 255 / 5000));
FastLED.show();
delay(50);
}
mesPixels[0] = CRGB(0, 0, 0);
FastLED.show();
button.attach(32, INPUT_PULLUP);
button.interval(5);
button.setPressedState(LOW);
}
void loop() {
// Toujours inclure M5.update() au début de loop() :
M5.update();
// UPDATE THE BUTTON BY CALLING .update() AT THE BEGINNING OF THE LOOP:
button.update();
// IF THE BUTTON WAS PRESSED THIS LOOP:
if (button.pressed()) {
// DO SOMETHING IF THE BUTTON WAS PRESSED THIS LOOP...
if (ledToggle) {
ledToggle = false;
mesPixels[0] = CRGB(0, 0, 0);
} else {
ledToggle = true;
mesPixels[0] = CRGB(255, 255, 255);
}
FastLED.show();
}
}
@@ -0,0 +1,73 @@
/*
DESCRIPTION
====================
When the debounced input goes from LOW to HIGH, the LED will turn on for the same time
the debounced input was held LOW.
Set BOUNCE_PIN to the pin attached to the input (a button for example).
Set LED_PIN to the pin attached to a LED.
*/
// WE WILL attach() THE Bounce INSTANCE TO THE FOLLOWING PIN IN setup()
#define BOUNCE_PIN 2
// DEFINE THE PIN FOR THE LED :
// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)
#define LED_PIN LED_BUILTIN
// 2) OTHERWISE SET YOUR OWN PIN
// #define LED_PIN 13
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
// INSTANTIATE A Bounce OBJECT.
Bounce bounce = Bounce();
// SET A VARIABLE TO STORE THE INTERVAL FOR HOW LONG TO KEEP THE LED HIGH
unsigned long ledHighInterval;
// SET A VARIABLE TO STORE THE START TIME WHEN THE LED WAS TURNED HIGH
unsigned long ledHighLastTime;
void setup() {
// BOUNCE SETUP
// SELECT ONE OF THE FOLLOWING :
// 1) IF YOUR INPUT HAS AN INTERNAL PULL-UP
bounce.attach( BOUNCE_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP
// 2) IF YOUR INPUT USES AN EXTERNAL PULL-UP
//bounce.attach( BOUNCE_PIN, INPUT ); // USE EXTERNAL PULL-UP
// DEBOUNCE INTERVAL IN MILLISECONDS
bounce.interval(5); // interval in ms
// LED SETUP
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void loop() {
// Update the Bounce instance (YOU MUST DO THIS EVERY LOOP)
bounce.update();
// <Bounce>.rose() RETURNS true IF THE STATE JUST WENT FROM LOW TO HIGH
if ( bounce.rose() ) {
digitalWrite(LED_PIN, HIGH); // TURN ON THE LED
ledHighLastTime = millis();
ledHighInterval = bounce.previousDuration();
}
// IF WE HAVE TO BLINK THE LED
if ( millis() - ledHighLastTime > ledHighInterval ) {
digitalWrite(LED_PIN, LOW);
} else {
digitalWrite(LED_PIN, HIGH);
}
}
@@ -0,0 +1,88 @@
/*
DESCRIPTION
====================
Simple example of the Bounce library that switches the state of a LED
when the debounced input goes from LOW to HIGH.
Also if the debounced input was previously held LOW for more than 1000 milliseconds,
the LED will blink.
Set BOUNCE_PIN to the pin attached to the input (a button for example).
Set LED_PIN to the pin attached to a LED.
*/
// WE WILL attach() THE Bounce INSTANCE TO THE FOLLOWING PIN IN setup()
#define BOUNCE_PIN 2
// DEFINE THE PIN FOR THE LED :
// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN)
#define LED_PIN LED_BUILTIN
// 2) OTHERWISE SET YOUR OWN PIN
// #define LED_PIN 13
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
// INSTANTIATE A Bounce OBJECT.
Bounce bounce = Bounce();
// SET A VARIABLE TO STORE THE LED STATE
int ledState = LOW;
// SET A VARIABLE TO STORE THE BLINKING STATE
bool blinkLed = false;
// SET A VARIABLE TO STORE THE LAST TIME THE LED BLINKED
unsigned long blinkLedLastTime;
void setup() {
// BOUNCE SETUP
// SELECT ONE OF THE FOLLOWING :
// 1) IF YOUR INPUT HAS AN INTERNAL PULL-UP
bounce.attach( BOUNCE_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP
// 2) IF YOUR INPUT USES AN EXTERNAL PULL-UP
//bounce.attach( BOUNCE_PIN, INPUT ); // USE EXTERNAL PULL-UP
// DEBOUNCE INTERVAL IN MILLISECONDS
bounce.interval(5); // interval in ms
// LED SETUP
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, ledState);
}
void loop() {
// Update the Bounce instance (YOU MUST DO THIS EVERY LOOP)
bounce.update();
// <Bounce>.changed() RETURNS true IF THE STATE CHANGED (FROM HIGH TO LOW OR LOW TO HIGH)
if ( bounce.changed() ) {
// THE STATE OF THE INPUT CHANGED
// GET THE STATE
int deboucedInput = bounce.read();
// IF THE CHANGED VALUE IS HIGH
if ( deboucedInput == HIGH ) {
ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
digitalWrite(LED_PIN,ledState); // WRITE THE NEW ledState
// IF THE DURATION OF THE PREVIOUS STATE (LOW IN THIS CASE) WAS HELD LONGER THAN 1000 MILLISECONDS:
if ( bounce.previousDuration() > 1000 ) blinkLed = true;
else blinkLed = false;
}
}
// IF WE HAVE TO BLINK THE LED
if ( blinkLed ) {
if ( millis() - blinkLedLastTime > 500 ) {
blinkLedLastTime = millis();
ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
digitalWrite(LED_PIN,ledState); // WRITE THE NEW ledState
}
}
}
+37
View File
@@ -0,0 +1,37 @@
// This example toggles the debug LED (pin 13) on or off
// when a button on pin 2 is pressed.
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define BUTTON_PIN 2
#define LED_PIN 13
int ledState = LOW;
Bounce debouncer = Bounce(); // Instantiate a Bounce object
void setup() {
debouncer.attach(BUTTON_PIN,INPUT_PULLUP); // Attach the debouncer to a pin with INPUT_PULLUP mode
debouncer.interval(25); // Use a debounce interval of 25 milliseconds
pinMode(LED_PIN,OUTPUT); // Setup the LED
digitalWrite(LED_PIN,ledState);
}
void loop() {
debouncer.update(); // Update the Bounce instance
if ( debouncer.fell() ) { // Call code if button transitions from HIGH to LOW
ledState = !ledState; // Toggle LED state
digitalWrite(LED_PIN,ledState); // Apply new LED state
}
}
+14 -6
View File
@@ -6,18 +6,26 @@
# Datatypes (KEYWORD1)
#######################################
Bounce KEYWORD1
Bounce KEYWORD1
Bounce2 KEYWORD1
Button KEYWORD1
Bounce2NameSpace KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
update KEYWORD2
interval KEYWORD2
read KEYWORD2
attach KEYWORD2
update KEYWORD2
interval KEYWORD2
read KEYWORD2
attach KEYWORD2
rose KEYWORD2
fell KEYWORD2
duration KEYWORD2
pressed KEYWORD2
released KEYWORD2
setPressedState KEYWORD2
currentDuration KEYWORD2
previousDuration KEYWORD2
#######################################
# Instances (KEYWORD2)
+1 -1
View File
@@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/thomasfredericks/Bounce2.git"
},
"version": "2.41",
"version": "2.72",
"exclude": [
"*.png",
"*.zip"
+4 -3
View File
@@ -1,9 +1,10 @@
name=Bounce2
version=2.42
author=Thomas O Fredericks <tof@t-o-f.info> with contributions fromEric Lowry, Jim Schimpf and Tom Harkaway
version=2.72
author=Thomas O Fredericks <tof@t-o-f.info> with contributions from Eric Lowry, Jim Schimpf, Tom Harkaway, Joachim Krüger and MrGradgrind.
maintainer=Thomas O Fredericks <tof@t-o-f.info>
sentence=Debouncing library for Arduino and Wiring.
paragraph=Deboucing switches and toggles is important.
paragraph=Debouncing switches and toggles is important.
category=Signal Input/Output
includes=Bounce2.h
url=https://github.com/thomasfredericks/Bounce2
architectures=*
+64 -34
View File
@@ -3,42 +3,33 @@
#include "Bounce2.h"
static const uint8_t DEBOUNCED_STATE = 0b00000001;
static const uint8_t UNSTABLE_STATE = 0b00000010;
static const uint8_t CHANGED_STATE = 0b00000100;
//////////////
// DEBOUNCE //
//////////////
Bounce::Bounce()
: previous_millis(0)
Debouncer::Debouncer():previous_millis(0)
, interval_millis(10)
, state(0)
, pin(0)
{}
, state(0) {}
void Bounce::attach(int pin) {
this->pin = pin;
state = 0;
void Debouncer::interval(uint16_t interval_millis)
{
this->interval_millis = interval_millis;
}
void Debouncer::begin() {
state = 0;
if (readCurrentState()) {
setStateFlag(DEBOUNCED_STATE | UNSTABLE_STATE);
}
#ifdef BOUNCE_LOCK_OUT
#ifdef BOUNCE_LOCK_OUT
previous_millis = 0;
#else
previous_millis = millis();
#endif
}
void Bounce::attach(int pin, int mode){
setPinMode(pin, mode);
this->attach(pin);
}
void Bounce::interval(uint16_t interval_millis)
{
this->interval_millis = interval_millis;
}
bool Bounce::update()
bool Debouncer::update()
{
unsetStateFlag(CHANGED_STATE);
@@ -49,8 +40,7 @@ bool Bounce::update()
bool currentState = readCurrentState();
if ( currentState != getStateFlag(DEBOUNCED_STATE) ) {
previous_millis = millis();
toggleStateFlag(DEBOUNCED_STATE);
setStateFlag(CHANGED_STATE);
changeState();
}
}
@@ -68,8 +58,7 @@ bool Bounce::update()
// set the STATE_CHANGED flag and the new DEBOUNCED_STATE.
// This will be prompt as long as there has been greater than interval_misllis ms since last change of input.
// Otherwise debounced state will not change again until bouncing is stable for the timeout period.
toggleStateFlag(DEBOUNCED_STATE);
setStateFlag(CHANGED_STATE );
changeState();
}
}
@@ -97,29 +86,70 @@ bool Bounce::update()
// If it is different from last state, set the STATE_CHANGED flag
if (currentState != getStateFlag(DEBOUNCED_STATE) ) {
previous_millis = millis();
toggleStateFlag(DEBOUNCED_STATE);
setStateFlag(CHANGED_STATE) ;
changeState();
}
}
#endif
return getStateFlag(CHANGED_STATE);
return changed();
}
bool Bounce::read()
// WIP HELD
unsigned long Debouncer::previousDuration() const {
return durationOfPreviousState;
}
unsigned long Debouncer::currentDuration() const {
return (millis() - stateChangeLastTime);
}
inline void Debouncer::changeState() {
toggleStateFlag(DEBOUNCED_STATE);
setStateFlag(CHANGED_STATE) ;
durationOfPreviousState = millis() - stateChangeLastTime;
stateChangeLastTime = millis();
}
bool Debouncer::read() const
{
return getStateFlag(DEBOUNCED_STATE);
}
bool Bounce::rose()
bool Debouncer::rose() const
{
return getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE);
}
bool Bounce::fell()
bool Debouncer::fell() const
{
return !getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE);
}
////////////
// BOUNCE //
////////////
Bounce::Bounce()
: pin(0)
{}
void Bounce::attach(int pin) {
this->pin = pin;
// SET INITIAL STATE
begin();
}
void Bounce::attach(int pin, int mode){
setPinMode(pin, mode);
this->attach(pin);
}
+271 -47
View File
@@ -26,6 +26,7 @@
Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef Bounce2_h
#define Bounce2_h
@@ -43,68 +44,291 @@
#include <inttypes.h>
/*
#ifndef _BV
#define _BV(n) (1<<(n))
#endif
/**
@example bounce_basic.ino
Basic example of the Bounce class.
*/
class Bounce
/**
@example button_basic.ino
Basic example of the Button class.
*/
/**
@brief The Debouce class. Just the deboucing code separated from all harware.
*/
class Debouncer
{
public:
// Create an instance of the bounce library
Bounce();
// Note : this is private as it migh change in the future
private:
static const uint8_t DEBOUNCED_STATE = 0b00000001; // Final returned calculated debounced state
static const uint8_t UNSTABLE_STATE = 0b00000010; // Actual last state value behind the scene
static const uint8_t CHANGED_STATE = 0b00000100; // The DEBOUNCED_STATE has changed since last update()
// Attach to a pin (and also sets initial state)
void attach(int pin);
// Note : this is private as it migh change in the future
private:
inline void changeState();
inline void setStateFlag(const uint8_t flag) {state |= flag;}
inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
inline bool getStateFlag(const uint8_t flag) const {return((state & flag) != 0);}
public:
/*!
@brief Create an instance of the Debounce class.
@endcode
*/
Debouncer();
/**
@brief Sets the debounce interval in milliseconds.
@param interval_millis
The interval time in milliseconds.
*/
void interval(uint16_t interval_millis);
/*!
@brief Updates the pin's state.
Because Bounce does not use interrupts, you have to "update" the object before reading its value and it has to be done as often as possible (that means to include it in your loop()). Only call update() once per loop().
@return True if the pin changed state.
*/
bool update();
/**
@brief Returns the pin's state (HIGH or LOW).
@return HIGH or LOW.
*/
bool read() const;
/**
@brief Returns true if pin signal transitions from high to low.
*/
bool fell() const;
/**
@brief Returns true if pin signal transitions from low to high.
*/
bool rose() const;
public:
/**
@brief Returns true if the state changed on last update.
@return True if the state changed on last update. Otherwise, returns false.
*/
bool changed( ) const { return getStateFlag(CHANGED_STATE); }
/**
@brief Returns the duration in milliseconds of the current state.
Is reset to 0 once the pin rises ( rose() ) or falls ( fell() ).
// Attach to a pin (and also sets initial state) and sets pin to mode (INPUT/INPUT_PULLUP/OUTPUT)
void attach(int pin, int mode);
@return The duration in milliseconds (unsigned long) of the current state.
*/
// Sets the debounce interval
void interval(uint16_t interval_millis);
unsigned long currentDuration() const;
// Updates the pin
// Returns 1 if the state changed
// Returns 0 if the state did not change
bool update();
// Returns the updated pin state
bool read();
/**
@brief Returns the duration in milliseconds of the previous state.
// Returns the falling pin state
bool fell();
Takes the values of duration() once the pin changes state.
@return The duration in milliseconds (unsigned long) of the previous state.
*/
unsigned long previousDuration() const;
/**
@brief DEPRECATED (i.e. do not use). Renamed currentDuration().
This member function is DEPRECATED will be removed next major version. DO NOT USE IT. USE currentDuration() INSTEAD.
It was renamed to avoid confusion with previousDuration().
@return The duration in milliseconds (unsigned long) of the current state.
*/
[[deprecated]]
unsigned long duration() {
return currentDuration();
};
// Returns the rising pin state
bool rose();
protected:
void begin();
virtual bool readCurrentState() =0;
unsigned long previous_millis;
uint16_t interval_millis;
uint8_t state;
unsigned long stateChangeLastTime;
unsigned long durationOfPreviousState;
// Partial compatibility for programs written with Bounce version 1
bool risingEdge() { return rose(); }
bool fallingEdge() { return fell(); }
Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
attach(pin);
interval(interval_millis);
}
};
protected:
unsigned long previous_millis;
uint16_t interval_millis;
uint8_t state;
uint8_t pin;
virtual bool readCurrentState() { return digitalRead(pin); }
virtual void setPinMode(int pin, int mode) {
#if defined(ARDUINO_STM_NUCLEO_F103RB) || defined(ARDUINO_GENERIC_STM32F103C)
pinMode(pin, (WiringPinMode)mode);
/**
@brief The Debouncer:Bounce class. Links the Deboucing class to a hardware pin. This class is odly named, but it will be kept that so it stays compatible with previous code.
*/
class Bounce : public Debouncer
{
public:
/*!
@brief Create an instance of the Bounce class.
@code
// Create an instance of the Bounce class.
Bounce() button;
@endcode
*/
Bounce();
/*!
@brief Attach to a pin and sets that pin's mode (INPUT, INPUT_PULLUP or OUTPUT).
@param pin
The pin that is to be debounced.
@param mode
A valid Arduino pin mode (INPUT, INPUT_PULLUP or OUTPUT).
*/
void attach(int pin, int mode);
/**
Attach to a pin for advanced users. Only attach the pin this way once you have previously set it up. Otherwise use attach(int pin, int mode).
*/
void attach(int pin);
Bounce(uint8_t pin, unsigned long interval_millis ) : Bounce() {
attach(pin);
interval(interval_millis);
}
/**
@brief Return pin that this Bounce is attached to
@return integer identifier of the coupled pin
*/
inline int getPin() const {
return this->pin;
};
////////////////
// Deprecated //
////////////////
/**
@brief Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce version 1
*/
[[deprecated]]
bool risingEdge() const { return rose(); }
/**
@brief Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce version 1
*/
[[deprecated]]
bool fallingEdge() const { return fell(); }
/**
@brief Deprecated (i.e. do not use). Included for partial compatibility for programs written with Bounce version 1
*/
protected:
uint8_t pin;
virtual bool readCurrentState() { return digitalRead(pin); }
virtual void setPinMode(int pin, int mode) {
#if defined(ARDUINO_ARCH_STM32F1)
pinMode(pin, (WiringPinMode)mode);
#else
pinMode(pin, mode);
pinMode(pin, mode);
#endif
}
}
private:
inline void setStateFlag(const uint8_t flag) {state |= flag;}
inline void unsetStateFlag(const uint8_t flag) {state &= ~flag;}
inline void toggleStateFlag(const uint8_t flag) {state ^= flag;}
inline bool getStateFlag(const uint8_t flag) {return((state & flag) != 0);}
};
/**
@brief The Debouncer:Bounce:Button class. The Button class matches an electrical state to a physical action.
*/
namespace Bounce2 {
// code declarations
class Button : public Bounce{
protected:
bool stateForPressed = 1; //
public:
/*!
@brief Create an instance of the Button class. By default, the pressed state is matched to a HIGH electrical level.
@code
// Create an instance of the Button class.
Button() button;
@endcode
*/
Button(){ }
/*!
@brief Set the electrical state (HIGH/LOW) that corresponds to a physical press. By default, the pressed state is matched to a HIGH electrical level.
@param state
The electrical state (HIGH/LOW) that corresponds to a physical press.
*/
void setPressedState(bool state){
stateForPressed = state;
}
/*!
@brief Get the electrical state (HIGH/LOW) that corresponds to a physical press.
*/
inline bool getPressedState() const {
return stateForPressed;
};
/*!
@brief Returns true if the button is currently physically pressed.
*/
inline bool isPressed() const {
return read() == getPressedState();
};
/*!
@brief Returns true if the button was physically pressed
*/
inline bool pressed() const {
return changed() && isPressed();
};
/*!
@brief Returns true if the button was physically released
*/
inline bool released() const {
return changed() && !isPressed();
};
};
};
#endif