Compare commits

...
119 Commits
Author SHA1 Message Date
Thomas O Fredericks 804c4c06d2 Update README.md 2024-01-11 15:00:06 -05:00
Thomas O Fredericks f362cf32e7 Update README.md 2024-01-11 14:54:25 -05:00
Thomas O Fredericks 1badf95e3b Create _config.yml 2024-01-11 14:44:06 -05:00
Thomas O Fredericks ed79ad5b79 Merge branch 'master' of https://github.com/thomasfredericks/Bounce2 2024-01-11 13:15:49 -05:00
Thomas O Fredericks 91fea0682d Update README.md 2024-01-11 13:15:30 -05:00
Thomas O Fredericks e1c506bb48 Create jekyll-gh-pages.yml 2024-01-11 13:09:59 -05:00
Thomas O Fredericks e68f6110ee Merge pull request #94 from thomasfredericks/simpler_documentation
Moving all documentation into the README
2024-01-11 12:25:19 -05:00
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
Thomas O Fredericks 01a2a947ed Updated version number 2018-05-22 10:34:19 -04:00
Thomas O Fredericks 8d5b3756b0 Merge pull request #45 from T81/Arduino-IDE-1.5-Library-specification
Folder structure according Arduino IDE 1.5 Library specification
2018-05-22 10:31:08 -04:00
T81 b061fcf83e Folder structure according Arduino IDE 1.5 Library specification 2018-05-22 15:48:01 +03:00
Thomas O Fredericks 7091ec8bf9 Fixed a missing include for Platform.io 2018-02-06 09:46:54 -05:00
Thomas O Fredericks 149abc4921 Update library.properties 2018-02-05 11:24:22 -05:00
Thomas O Fredericks 716c423922 Update library.json 2018-02-05 11:24:07 -05:00
Thomas O Fredericks 9901bf1f23 Merge pull request #42 from nemoo2580/master
Made "setPinMode" method extendable
2018-02-05 11:20:40 -05:00
Michał Rybicki 80d29125d6 Made "pinMode" method extendable 2018-02-04 17:03:57 +01:00
Thomas O Fredericks 3f9594d49c Added a help file and updated links 2018-01-10 12:20:31 -05:00
Thomas O Fredericks 5a53e1b757 Update README.md 2018-01-10 12:17:45 -05:00
Thomas O Fredericks 391f7aaf7f Cleaned up the bit operators and made the class extendable 2018-01-10 12:16:27 -05:00
Thomas O Fredericks 06beac6d75 Update library.json 2017-11-09 08:51:03 -05:00
Thomas O Fredericks 43b373c836 Update library.properties 2017-11-09 08:50:49 -05:00
Thomas O Fredericks eea5da2ba2 Merge pull request #35 from LieBtrau/master
Blue Pill port
2017-10-30 10:26:00 -04:00
Christoph Tack 2d586f5b50 Blue Pill port
Signed-off-by: Christoph Tack <prog.send@gmail.com>
2017-10-29 21:26:06 +01:00
Thomas O Fredericks 8b60f9f057 Added a fix for Due 2016-06-28 11:54:47 -04:00
Thomas O Fredericks ed75053c9e Ignoring .db files 2016-06-28 11:50:18 -04:00
Thomas O Fredericks 60edc5701e Updated version number 2016-05-14 02:53:55 -04:00
Thomas O Fredericks ffe7633042 Update library.properties 2016-05-14 02:45:41 -04:00
Thomas O Fredericks c6d1aeb7eb Merge pull request #25 from MrGradgrind/prompt_detection_option
Add compile option: BOUNCE_WITH_PROMPT_DETECTION
2016-04-28 09:01:28 -04:00
ChrisGallagher 0a30a709f0 Add compile option: BOUNCE_WITH_PROMPT_DETECTION
New compile time opiton for alternative 'prompt' debounce algorithm.
Button state changes are avaliable imideatley, so long as the previous
state has been stable for the timeout period. Otherwise the state will
be updated when the timeout period allows.

- Able to report acurate switch times normally with no delay.
- Compatable with exising usage.
- Use when prompt switch transition timing is important.
2016-04-26 15:48:26 +01:00
Thomas O Fredericks 3aaca27601 Merge pull request #23 from PaulStoffregen/master
Partial compatibility with programs written using Bounce 1
2016-03-12 20:24:01 -05:00
PaulStoffregen 0016920498 Partial compatibility with programs written using Bounce 1 2016-03-07 09:29:37 -08:00
Thomas O Fredericks f7f48e1760 Merge pull request #22 from ivankravets/patch-1
@PlatformIO Library Registry manifest file
2016-02-25 12:30:21 -05:00
Ivan Kravets 56c943c894 @PlatformIO Library Registry manifest file
* This library in Web Registry: http://platformio.org/#!/lib/show/1106/Bounce2
* Specification: [library.json](http://docs.platformio.org/en/latest/librarymanager/config.html)
* Library Manager:  http://docs.platformio.org/en/latest/librarymanager/index.html
2016-02-25 18:29:20 +02:00
Thomas O Fredericks bba2a0aa75 Merge pull request #21 from sseptillion/master
Added pinMode setting to attach

I will later document this, as I will change the documentation to an HTML file eventually as I did with my Chrono library for example.
2015-09-08 13:55:15 -04:00
sseptillion 2d6327e91c Added pinMode setting to attach
Added possibility to attach the pin and auto set the pinMode. Attach and
pinMode can now be done in 1 step. just call Bounce.attach(pin,
INPUT_PULLUP) etc Fixed Header file and made all mode possible.
2015-09-08 12:29:48 +02:00
sseptillion 50b796ccfc Added pinMode setting to attach
Added possibility to attach the pin and auto set the pinMode. Attach and
pinMode can now be done in 1 step. just call Bounce.attach(pin,
INPUT_PULLUP) etc
2015-09-08 11:34:38 +02:00
thomasfredericks ea7731102f Added library properties 2015-07-24 08:43:05 -04:00
thomasfredericks 7ed666b7b1 Added a 2 button example 2015-05-06 11:05:29 -04:00
thomasfredericks a2ec7acd6c Update README.md 2015-01-06 12:23:49 -05:00
thomasfredericks 987d751bfa Changed folder structure as requested 2015-01-06 12:22:41 -05:00
40 changed files with 1403 additions and 866 deletions
+51
View File
@@ -0,0 +1,51 @@
# Sample workflow for building and deploying a Jekyll site to GitHub Pages
name: Deploy Jekyll with GitHub Pages dependencies preinstalled
on:
# Runs on pushes targeting the default branch
push:
branches: ["master"]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
# Build job
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Build with Jekyll
uses: actions/jekyll-build-pages@v1
with:
source: ./
destination: ./_site
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
+2
View File
@@ -0,0 +1,2 @@
*.db
*.yml
-88
View File
@@ -1,88 +0,0 @@
// Please read Bounce.h for information about the liscence and authors
#include "WProgram.h"
#include "Bounce.h"
Bounce::Bounce(uint8_t pin,unsigned long interval_millis)
{
interval(interval_millis);
previous_millis = millis();
state = digitalRead(pin);
this->pin = pin;
}
void Bounce::write(int new_state)
{
this->state = new_state;
digitalWrite(pin,state);
}
void Bounce::interval(unsigned long interval_millis)
{
this->interval_millis = interval_millis;
this->rebounce_millis = 0;
}
void Bounce::rebounce(unsigned long interval)
{
this->rebounce_millis = interval;
}
int Bounce::update()
{
if ( debounce() ) {
rebounce(0);
return stateChanged = 1;
}
// We need to rebounce, so simulate a state change
if ( rebounce_millis && (millis() - previous_millis >= rebounce_millis) ) {
previous_millis = millis();
rebounce(0);
return stateChanged = 1;
}
return stateChanged = 0;
}
unsigned long Bounce::duration()
{
return millis() - previous_millis;
}
int Bounce::read()
{
return (int)state;
}
// Protected: debounces the pin
int Bounce::debounce() {
uint8_t newState = digitalRead(pin);
if (state != newState ) {
if (millis() - previous_millis >= interval_millis) {
previous_millis = millis();
state = newState;
return 1;
}
}
return 0;
}
// The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.
bool Bounce::risingEdge() { return stateChanged && state; }
// The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off.
bool Bounce::fallingEdge() { return stateChanged && !state; }
-70
View File
@@ -1,70 +0,0 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Main code by Thomas O Fredericks
Rebounce and duration functions contributed by Eric Lowry
Write function contributed by Jim Schimpf
risingEdge and fallingEdge contributed by Tom Harkaway
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef Bounce_h
#define Bounce_h
#include <inttypes.h>
class Bounce
{
public:
// Initialize
Bounce(uint8_t pin, unsigned long interval_millis );
// Sets the debounce interval
void interval(unsigned long interval_millis);
// Updates the pin
// Returns 1 if the state changed
// Returns 0 if the state did not change
int update();
// Forces the pin to signal a change (through update()) in X milliseconds
// even if the state does not actually change
// Example: press and hold a button and have it repeat every X milliseconds
void rebounce(unsigned long interval);
// Returns the updated pin state
int read();
// Sets the stored pin state
void write(int new_state);
// Returns the number of milliseconds the pin has been in the current state
unsigned long duration();
// The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.
bool risingEdge();
// The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off.
bool fallingEdge();
protected:
int debounce();
unsigned long previous_millis, interval_millis, rebounce_millis;
uint8_t state;
uint8_t pin;
uint8_t stateChanged;
};
#endif
-13
View File
@@ -1,13 +0,0 @@
Bounce library for Arduino
Version 1.5
by Thomas Ouellet Fredericks
with contributions from:
Eric Lowry
Jim Schimpf
Tom Harkaway
contact: mrtoftrash@gmail.com
See the online documentation here: http://www.arduino.cc/playground/Code/Bounce
-32
View File
@@ -1,32 +0,0 @@
#include <Bounce.h>
// This code turns a led on/off through a debounced button
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button
#define BUTTON 2
#define LED 13
// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer = Bounce( BUTTON,5 );
void setup() {
pinMode(BUTTON,INPUT);
pinMode(LED,OUTPUT);
}
void loop() {
// Update the debouncer
bouncer.update ( );
// Get the update value
int value = bouncer.read();
// Turn on or off the LED
if ( value == HIGH) {
digitalWrite(LED, HIGH );
} else {
digitalWrite(LED, LOW );
}
}
-31
View File
@@ -1,31 +0,0 @@
#include <Bounce.h>
#define BUTTON 2
#define LED 13
int ledValue = LOW;
// This example changes the state of the LED everytime the button is pushed
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button
Bounce bouncer = Bounce( BUTTON, 5 );
void setup() {
pinMode(BUTTON,INPUT);
pinMode(LED,OUTPUT);
}
void loop() {
if ( bouncer.update() ) {
if ( bouncer.read() == HIGH) {
if ( ledValue == LOW ) {
ledValue = HIGH;
} else {
ledValue = LOW;
}
digitalWrite(LED,ledValue);
}
}
}
-59
View File
@@ -1,59 +0,0 @@
#include <Bounce.h>
/*
This example uses a mercury tilt switch to detect the orientation
of a robot. If you turn the bot over, it falls asleep to conserve
battery life. If you have ever used a tilt switch before,
you know they can be very "noisy" - they jump around a lot!
The bounce library can handle sensing when a noisy switch has
"settled down" with the "duration" method that tells you how long
the pin has been in the current state.
*/
const uint8_t ORIENTATION_PIN = 16;
Bounce orientation = Bounce( ORIENTATION_PIN, 50 );
int awake; // Are we awake based on our current orientation?
void setup() {
pinMode( ORIENTATION_PIN, INPUT );
digitalWrite( ORIENTATION_PIN, HIGH ); // Activate Pull-Up
Serial.begin(19200);
Serial.println( "Orientation Test " );
Serial.println();
awake = orientation.read();
Serial.println( awake ? "Awake!" : "Asleep!" );
}
unsigned long lastKnock = 0;
void loop() {
orientation.update();
// has our orientation changed for more the 1/2 a second?
if ( orientation.read() != awake && orientation.duration() > 500 ) {
awake = orientation.read();
if ( awake ) {
Serial.println( "Waking Up!" );
// Do Something Here...
}
else {
Serial.println( "Falling Asleep" );
// Do Something Here...
}
}
}
-121
View File
@@ -1,121 +0,0 @@
// Edge Test For use with the DANGER SHIELD
// This code turns a LED on and incraments number displayed on the 7-segment display
// whenever either PB1 or PB2 is pressed. PB1 used the debounce library and PB2 does
// not. PB1 will cleanly increment the displayed value. PB2 will not alway increment
// the display because the switch is not debounced.
//
// The bounce library also detects the rising or falling edge of the input.
// This is often called a one-shot and is usefull if you want something to only
// happen once when a button is pressed or released. The risingEdge method is true
// for only one scan when the input goes from off-to-on. The fallingEdge method is
// true for only one scan when the input goes from on-to-off. Even if you are not
// using the debounce feature the the library you need to call the update method
// every scan for the edge detection to work.
//
// Notes:
// The buttons on the Danger Shield pull the input to ground when they are pressed,
// so they act like normally closed switches. If order to make them work like one
// would expect, he tests for the button states are reversed. This also reversesd
// the rising edge test on button1 to a falling edge test.
//
// Since PB2 is not debounced, the edge detection is done directly in the sketch.
//
#include <Bounce.h>
// This code turns a led on/off through a debounced button
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button
#define BUTTON1 10
#define BUTTON2 11
#define BUTTON3 12
#define BUZZER 3
#define LED1 5
#define LED2 6
#define LATCH 7
#define CLOCK 8
#define DATA 4
// Shift register bit values to display 0-9 on the seven-segment display
const byte ledCharSet[16] = {
B00111111, B00000110, B01011011, B01001111, B01100110,
B01101101, B01111101, B00000111, B01111111, B01101111,
B01110111, // a
B01111100, // b
B00111001, // c
B01011110, // d
B01111001, // e
B01110001 // f
};
byte pressCount= 0;
bool lastButton2;
// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer1 = Bounce(BUTTON1, 5);
void setup() {
pinMode(BUTTON1, INPUT);
pinMode(LED1, OUTPUT);
pinMode(BUTTON2, INPUT);
pinMode(LED2, OUTPUT);
pinMode(BUTTON3, INPUT);
pinMode(BUZZER, OUTPUT);
pinMode(LATCH, OUTPUT);
pinMode(CLOCK, OUTPUT);
pinMode(DATA, OUTPUT);
lastButton2 = digitalRead(BUTTON2);
displayDigit(255, true);
}
void loop() {
// Update debouncer
boolean button1Changed = bouncer1.update();
// Turn on or off the LED
if ( !bouncer1.read()) {
digitalWrite(LED1, HIGH);
} else {
digitalWrite(LED1, LOW);
}
int buttonValue2 = digitalRead(BUTTON2);
if (buttonValue2 == LOW) {
digitalWrite(LED2, HIGH);
} else {
digitalWrite(LED2, LOW);
}
if ( bouncer1.fallingEdge() || // rising edge of button 1
(lastButton2 && !buttonValue2)) { // rising edge of button 2
displayDigit(pressCount, false);
pressCount++;
if (pressCount > 15) { pressCount = 0; }
}
lastButton2 = digitalRead(BUTTON2);
}
void displayDigit(byte value, boolean dp) {
byte shiftData = 0;
if (value <= 15) { shiftData = ledCharSet[value]; }
if (dp) { shiftData |= B10000000; }
digitalWrite(LATCH,LOW);
shiftOut(DATA, CLOCK, MSBFIRST, ~shiftData);
digitalWrite(LATCH,HIGH);
}
-48
View File
@@ -1,48 +0,0 @@
#include <Bounce.h>
// As long as the button is held down, the LED will blink
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button
#define BUTTON 2
#define LED 13
// A variable to store the current LED state
int ledState = LOW;
// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer = Bounce( BUTTON,5 );
void setup() {
pinMode(BUTTON,INPUT);
pinMode(LED,OUTPUT);
}
void loop() {
// Update and monitor a change of input
if ( bouncer.update() ) {
// Get the state of the button
int value = bouncer.read();
// Toggle the LED if the button is held
if ( value == HIGH) {
// Make the button retrigger in 500 milliseconds
bouncer.rebounce(500);
if ( ledState == LOW ) {
ledState = HIGH;
} else {
ledState = LOW;
}
} else {
ledState = LOW;
}
digitalWrite(LED, ledState );
}
}
-30
View File
@@ -1,30 +0,0 @@
#######################################
# Syntax Coloring Map For Debounce
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Bounce KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
update KEYWORD2
interval KEYWORD2
read KEYWORD2
write KEYWORD2
rebounce KEYWORD2
risingEdge KEYWORD2
fallingEdge KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################
#######################################
# Constants (LITERAL1)
#######################################
-92
View File
@@ -1,92 +0,0 @@
// Please read Bounce2.h for information about the liscence and authors
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Bounce2.h"
#define DEBOUNCED_STATE 0
#define UNSTABLE_STATE 1
#define STATE_CHANGED 3
Bounce::Bounce()
: previous_millis(0)
, interval_millis(10)
, state(0)
, pin(0)
{}
void Bounce::attach(int pin) {
this->pin = pin;
bool read = digitalRead(pin);
state = 0;
if (digitalRead(pin)) {
state = _BV(DEBOUNCED_STATE) | _BV(UNSTABLE_STATE);
}
#ifdef BOUNCE_LOCK_OUT
previous_millis = 0;
#else
previous_millis = millis();
#endif
}
void Bounce::interval(uint16_t interval_millis)
{
this->interval_millis = interval_millis;
}
bool Bounce::update()
{
#ifdef BOUNCE_LOCK_OUT
state &= ~_BV(STATE_CHANGED);
// Ignore everything if we are locked out
if (millis() - previous_millis >= interval_millis) {
bool currentState = digitalRead(pin);
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
previous_millis = millis();
state ^= _BV(DEBOUNCED_STATE);
state |= _BV(STATE_CHANGED);
}
}
return state & _BV(STATE_CHANGED);
#else
// Read the state of the switch in a temporary variable.
bool currentState = digitalRead(pin);
state &= ~_BV(STATE_CHANGED);
// If the reading is different from last reading, reset the debounce counter
if ( currentState != (bool)(state & _BV(UNSTABLE_STATE)) ) {
previous_millis = millis();
state ^= _BV(UNSTABLE_STATE);
} else
if ( millis() - previous_millis >= interval_millis ) {
// We have passed the threshold time, so the input is now stable
// If it is different from last state, set the STATE_CHANGED flag
if ((bool)(state & _BV(DEBOUNCED_STATE)) != currentState) {
previous_millis = millis();
state ^= _BV(DEBOUNCED_STATE);
state |= _BV(STATE_CHANGED);
}
}
return state & _BV(STATE_CHANGED);
#endif
}
bool Bounce::read()
{
return state & _BV(DEBOUNCED_STATE);
}
bool Bounce::rose()
{
return ( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
}
bool Bounce::fell()
{
return !( state & _BV(DEBOUNCED_STATE) ) && ( state & _BV(STATE_CHANGED));
}
-75
View File
@@ -1,75 +0,0 @@
/*
The MIT License (MIT)
Copyright (c) 2013 thomasfredericks
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Main code by Thomas O Fredericks (tof@t-o-f.info)
Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifdef BOUNCE_LOCK
#error You are using the invalid BOUNCE_LOCK-OUT define. Please update your sources to use BOUNCE_LOCK_OUT
#endif
// Uncomment the following line for "LOCK-OUT" debounce method
//#define BOUNCE_LOCK_OUT
#ifndef Bounce2_h
#define Bounce2_h
#include <inttypes.h>
class Bounce
{
public:
// Create an instance of the bounce library
Bounce();
// Attach to a pin (and also sets initial state)
void attach(int pin);
// Sets the debounce interval
void interval(uint16_t interval_millis);
// 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();
// Returns the falling pin state
bool fell();
// Returns the rising pin state
bool rose();
protected:
unsigned long previous_millis;
uint16_t interval_millis;
uint8_t state;
uint8_t pin;
};
#endif
-20
View File
@@ -1,20 +0,0 @@
The MIT License (MIT)
Copyright (c) 2013 thomasfredericks
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-9
View File
@@ -1,9 +0,0 @@
DOCUMENTATION
=====================
https://github.com/thomasfredericks/Bounce-Arduino-Wiring/wiki
DOWNLOAD
=====================
https://github.com/thomasfredericks/Bounce-Arduino-Wiring/archive/master.zip
-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/Bounce-Arduino-Wiring
#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 );
}
}
-48
View File
@@ -1,48 +0,0 @@
// Detect the falling edge
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce-Arduino-Wiring
#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/Bounce-Arduino-Wiring
#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();
}
}
-1
View File
@@ -1 +0,0 @@
Put the "Bounce2" folder in your Arduino or Wiring "libraries" folder.
+216 -21
View File
@@ -1,37 +1,232 @@
BOUNCE 2
=====================
# Bounce2
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 on contact Debouncing](http://en.wikipedia.org/wiki/Debounce#Contact_bounce)
with contributions from:
The library is composed of three classes:
* `Bounce2::Button` : The class that most people will use. It is the most feature rich class for deboucing hardware buttons.
* `Debouncer` : The code that does the actual debouncing. Only advanced users should use this class for creating their own implememtations.
* `Bounce` : This class links the `Debouncer` class 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 and Tom Harkaway
# Need Help?
Please post your usage questions on the [Arduino Forums](https://forum.arduino.cc/latest).
# Installation
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
## Global Space
```cpp
#include <Bounce2.h>
Bounce2::Button button = Bounce2::Button(); // INSTANTIATE A Bounce2::Button OBJECT
```
## Configuration 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 :
// IF THE BUTTON WAS PRESSED THIS LOOP:
if ( button.pressed() ) {
// DO SOMETHING IF THE BUTTON WAS PRESSED THIS LOOP...
}
```
https://github.com/thomasfredericks/Bounce-Arduino-Wiring/archive/master.zip
# Examples
## A Button Toggling 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);
}
}
```
## More Examples
You can find more examples in the **examples** folder.
# 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/Bounce-Arduino-Wiring/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 hardware.
| 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. |
ABOUT BOUNCE 1 (ORIGINAL VERSION)
=====================
# Alternate Algorithms
The following alternate debouncing algorithms are for **advanced** users or 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)
## 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.
+5
View File
@@ -0,0 +1,5 @@
title: Bounce2 Arduino & Wiring Library
description: Debouncing library for Arduino and Wiring.
show_downloads: true
google_analytics:
theme: jekyll-theme-leap-day
+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
}
}
+56
View File
@@ -0,0 +1,56 @@
// Detect the falling edge of multiple buttons.
// Eight buttons with internal pullups.
// Toggles a LED when any button is pressed.
// Buttons on pins 2,3,4,5,6,7,8,9
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define LED_PIN 13
#define NUM_BUTTONS 8
const uint8_t BUTTON_PINS[NUM_BUTTONS] = {2, 3, 4, 5, 6, 7, 8, 9};
int ledState = LOW;
Bounce * buttons = new Bounce[NUM_BUTTONS];
void setup() {
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i].attach( BUTTON_PINS[i] , INPUT_PULLUP ); //setup the bounce instance for the current button
buttons[i].interval(25); // interval in ms
}
// Setup the LED :
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, ledState);
}
void loop() {
bool needToToggleLed = false;
for (int i = 0; i < NUM_BUTTONS; i++) {
// Update the Bounce instance :
buttons[i].update();
// If it fell, flag the need to toggle the LED
if ( buttons[i].fell() ) {
needToToggleLed = true;
}
}
// if a LED toggle has been flagged :
if ( needToToggleLed ) {
// Toggle LED state :
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
}
}
+64
View File
@@ -0,0 +1,64 @@
/*
DESCRIPTION
====================
Simple example of the Bounce library that switches the debug LED when
either of 2 buttons are pressed.
*/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 3
#define LED_PIN 13
// Instantiate a Bounce object
Bounce debouncer1 = Bounce();
// Instantiate another Bounce object
Bounce debouncer2 = Bounce();
void setup() {
// Setup the first button with an internal pull-up :
pinMode(BUTTON_PIN_1,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer1.attach(BUTTON_PIN_1);
debouncer1.interval(5); // interval in ms
// Setup the second button with an internal pull-up :
pinMode(BUTTON_PIN_2,INPUT_PULLUP);
// After setting up the button, setup the Bounce instance :
debouncer2.attach(BUTTON_PIN_2);
debouncer2.interval(5); // interval in ms
//Setup the LED :
pinMode(LED_PIN,OUTPUT);
}
void loop() {
// Update the Bounce instances :
debouncer1.update();
debouncer2.update();
// Get the updated value :
int value1 = debouncer1.read();
int value2 = debouncer2.read();
// Turn on the LED if either button is pressed :
if ( value1 == LOW || value2 == LOW ) {
digitalWrite(LED_PIN, HIGH );
}
else {
digitalWrite(LED_PIN, LOW );
}
}
@@ -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
}
}
@@ -9,7 +9,7 @@ Open the Serial Monitor (57600 baud) for debug messages.
*/
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce-Arduino-Wiring
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Before

Width:  |  Height:  |  Size: 9.1 KiB

After

Width:  |  Height:  |  Size: 9.1 KiB

Before

Width:  |  Height:  |  Size: 47 KiB

After

Width:  |  Height:  |  Size: 47 KiB

+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)
+16
View File
@@ -0,0 +1,16 @@
{
"name": "Bounce2",
"keywords": "bounce, signal, input, ouput",
"description": "Debouncing library for Arduino or Wiring",
"repository": {
"type": "git",
"url": "https://github.com/thomasfredericks/Bounce2.git"
},
"version": "2.72",
"exclude": [
"*.png",
"*.zip"
],
"frameworks": "arduino",
"platforms": "*"
}
+10
View File
@@ -0,0 +1,10 @@
name=Bounce2
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=Debouncing switches and toggles is important.
category=Signal Input/Output
includes=Bounce2.h
url=https://github.com/thomasfredericks/Bounce2
architectures=*
+155
View File
@@ -0,0 +1,155 @@
// Please read Bounce2.h for information about the liscence and authors
#include "Bounce2.h"
//////////////
// DEBOUNCE //
//////////////
Debouncer::Debouncer():previous_millis(0)
, interval_millis(10)
, 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
previous_millis = 0;
#else
previous_millis = millis();
#endif
}
bool Debouncer::update()
{
unsetStateFlag(CHANGED_STATE);
#ifdef BOUNCE_LOCK_OUT
// Ignore everything if we are locked out
if (millis() - previous_millis >= interval_millis) {
bool currentState = readCurrentState();
if ( currentState != getStateFlag(DEBOUNCED_STATE) ) {
previous_millis = millis();
changeState();
}
}
#elif defined BOUNCE_WITH_PROMPT_DETECTION
// Read the state of the switch port into a temporary variable.
bool readState = readCurrentState();
if ( readState != getStateFlag(DEBOUNCED_STATE) ) {
// We have seen a change from the current button state.
if ( millis() - previous_millis >= interval_millis ) {
// We have passed the time threshold, so a new change of state is allowed.
// 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.
changeState();
}
}
// If the readState is different from previous readState, reset the debounce timer - as input is still unstable
// and we want to prevent new button state changes until the previous one has remained stable for the timeout.
if ( readState != getStateFlag(UNSTABLE_STATE) ) {
// Update Unstable Bit to macth readState
toggleStateFlag(UNSTABLE_STATE);
previous_millis = millis();
}
#else
// Read the state of the switch in a temporary variable.
bool currentState = readCurrentState();
// If the reading is different from last reading, reset the debounce counter
if ( currentState != getStateFlag(UNSTABLE_STATE) ) {
previous_millis = millis();
toggleStateFlag(UNSTABLE_STATE);
} else
if ( millis() - previous_millis >= interval_millis ) {
// We have passed the threshold time, so the input is now stable
// If it is different from last state, set the STATE_CHANGED flag
if (currentState != getStateFlag(DEBOUNCED_STATE) ) {
previous_millis = millis();
changeState();
}
}
#endif
return changed();
}
// 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 Debouncer::rose() const
{
return getStateFlag(DEBOUNCED_STATE) && getStateFlag(CHANGED_STATE);
}
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);
}
+334
View File
@@ -0,0 +1,334 @@
/*
The MIT License (MIT)
Copyright (c) 2013 thomasfredericks
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Main code by Thomas O Fredericks (tof@t-o-f.info)
Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef Bounce2_h
#define Bounce2_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// Uncomment the following line for "LOCK-OUT" debounce method
//#define BOUNCE_LOCK_OUT
// Uncomment the following line for "BOUNCE_WITH_PROMPT_DETECTION" debounce method
//#define BOUNCE_WITH_PROMPT_DETECTION
#include <inttypes.h>
/**
@example bounce_basic.ino
Basic example of the Bounce class.
*/
/**
@example button_basic.ino
Basic example of the Button class.
*/
/**
@brief The Debouce class. Just the deboucing code separated from all harware.
*/
class Debouncer
{
// 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()
// 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() ).
@return The duration in milliseconds (unsigned long) of the current state.
*/
unsigned long currentDuration() const;
/**
@brief Returns the duration in milliseconds of the previous state.
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();
};
protected:
void begin();
virtual bool readCurrentState() =0;
unsigned long previous_millis;
uint16_t interval_millis;
uint8_t state;
unsigned long stateChangeLastTime;
unsigned long durationOfPreviousState;
};
/**
@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);
#endif
}
};
/**
@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