Compare commits
80
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
371e2cff35 | ||
|
|
9276539335 | ||
|
|
cda0200633 | ||
|
|
ff821cb300 | ||
|
|
b7cd534fb7 | ||
|
|
699b0e429c | ||
|
|
71568702f2 | ||
|
|
d2c9f1673c | ||
|
|
ee74d73978 | ||
|
|
d744e0690f | ||
|
|
152a64231a | ||
|
|
79db9f0016 | ||
|
|
5e100280c5 | ||
|
|
2ba0be8f83 | ||
|
|
28afcafa6a | ||
|
|
1645b897c8 | ||
|
|
68dc6f7830 | ||
|
|
f87ba5ba9f | ||
|
|
246096ae96 | ||
|
|
1ce882f949 | ||
|
|
a132f4ab43 | ||
|
|
e89801c2dd | ||
|
|
744a957b76 | ||
|
|
cd87a100be | ||
|
|
6f244834a4 | ||
|
|
5a481c6922 | ||
|
|
4207a8dd17 | ||
|
|
eb5ab9fad8 | ||
|
|
c445bbc7a7 | ||
|
|
c012c6c042 | ||
|
|
83bc18e245 | ||
|
|
3a88a924f2 | ||
|
|
fd99238112 | ||
|
|
9babb4fc38 | ||
|
|
878f86bf1c | ||
|
|
59d3c66c56 | ||
|
|
08392e0f01 | ||
|
|
3ddefb4ba4 | ||
|
|
172e3e0835 | ||
|
|
f51b22b708 | ||
|
|
1a6552ffe5 | ||
|
|
2b6be81dc1 | ||
|
|
6147a34058 | ||
|
|
1af864b0bd | ||
|
|
cf32eda85e | ||
|
|
a57ba4c66a | ||
|
|
85d80fe400 | ||
|
|
14f9c5c4a6 | ||
|
|
9335056c46 | ||
|
|
26e216c98d | ||
|
|
6228e09451 | ||
|
|
51032991ac | ||
|
|
de69c15225 | ||
|
|
220a287467 | ||
|
|
01e6fc9398 | ||
|
|
3e4738b4ef | ||
|
|
3706e16e4b | ||
|
|
9a275c4f96 | ||
|
|
d422d7c054 | ||
|
|
d3ef0365eb | ||
|
|
f5203d0fb8 | ||
|
|
c2e3cd5a2d | ||
|
|
32c1c48b26 | ||
|
|
5a132024fc | ||
|
|
cbe0b980b7 | ||
|
|
a1c83e9d7d | ||
|
|
f19d27689f | ||
|
|
045183b598 | ||
|
|
2ba409cd4a | ||
|
|
47d7fab6ac | ||
|
|
d0443e3f3d | ||
|
|
0f5acfd2c4 | ||
|
|
cbb12331c9 | ||
|
|
2c83e7f19c | ||
|
|
a5ee5f9e79 | ||
|
|
9d91d62d1e | ||
|
|
c0be1e178a | ||
|
|
742dfe9668 | ||
|
|
527524112d | ||
|
|
99e9334994 |
+2
-1
@@ -1 +1,2 @@
|
||||
*.db
|
||||
*.db
|
||||
*.yml
|
||||
@@ -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.
|
||||
|
||||

|
||||
|
||||
## 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
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 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.
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+88
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
@@ -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
@@ -6,7 +6,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/thomasfredericks/Bounce2.git"
|
||||
},
|
||||
"version": "2.41",
|
||||
"version": "2.72",
|
||||
"exclude": [
|
||||
"*.png",
|
||||
"*.zip"
|
||||
|
||||
+4
-3
@@ -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
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user