Moving to version 2 of the library
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
|
||||
// Please read Bounce.h for information about the liscence and authors
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
#include "Bounce.h"
|
||||
|
||||
|
||||
|
||||
Bounce::Bounce() {
|
||||
this->interval_millis = 10;
|
||||
|
||||
}
|
||||
|
||||
void Bounce::attach(int pin) {
|
||||
this->pin = pin;
|
||||
debouncedState = unstableState = digitalRead(pin);
|
||||
#ifdef BOUNCE_RAPID_FIRE
|
||||
previous_millis = 0;
|
||||
#else
|
||||
previous_millis = millis();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Bounce::interval(unsigned long interval_millis)
|
||||
{
|
||||
this->interval_millis = interval_millis;
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool Bounce::update()
|
||||
{
|
||||
|
||||
#ifdef BOUNCE_RAPID_FIRE
|
||||
|
||||
// Ignore everything if we are locked out
|
||||
if (millis() - previous_millis >= interval_millis) {
|
||||
uint8_t currentState = digitalRead(pin);
|
||||
if (debouncedState != currentState ) {
|
||||
previous_millis = millis();
|
||||
debouncedState = currentState;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
#else
|
||||
// Lire l'etat de l'interrupteur dans une variable temporaire.
|
||||
uint8_t currentState = digitalRead(pin);
|
||||
stateChanged = false;
|
||||
|
||||
// Redemarrer le compteur timeStamp tant et aussi longtemps que
|
||||
// la lecture ne se stabilise pas.
|
||||
if ( currentState != unstableState ) {
|
||||
previous_millis = millis();
|
||||
} else if ( millis() - previous_millis >= interval_millis ) {
|
||||
// Rendu ici, la lecture est stable
|
||||
|
||||
// Est-ce que la lecture est différente de l'etat emmagasine de l'interrupteur?
|
||||
if ( currentState != debouncedState ) {
|
||||
debouncedState = currentState;
|
||||
stateChanged = true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
unstableState = currentState;
|
||||
return stateChanged;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
uint8_t Bounce::read()
|
||||
{
|
||||
return debouncedState;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
|
||||
/*
|
||||
* 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 (tof@t-o-f.info)
|
||||
Previous contributions by Eric Lowry, Jim Schimpf and Tom Harkaway
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
|
||||
// Uncomment the following line for "rapid fire" mode
|
||||
//#define BOUNCE_RAPID_FIRE
|
||||
|
||||
|
||||
#ifndef Bounce_h
|
||||
#define Bounce_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(unsigned long 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
|
||||
uint8_t read();
|
||||
|
||||
|
||||
protected:
|
||||
int debounce();
|
||||
unsigned long previous_millis, interval_millis;
|
||||
uint8_t debouncedState;
|
||||
uint8_t unstableState;
|
||||
uint8_t pin;
|
||||
uint8_t stateChanged;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
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.
|
||||
@@ -0,0 +1,93 @@
|
||||
Bounce-Arduino-Wiring
|
||||
=====================
|
||||
|
||||
Debouncing library for Arduino or Wiring
|
||||
by Thomas Ouellet Fredericks
|
||||
with contributions from:
|
||||
Eric Lowry
|
||||
Jim Schimpf
|
||||
Tom Harkaway
|
||||
|
||||
|
||||
Version
|
||||
====================
|
||||
|
||||
1.6: Fixed for Arduino 1.0
|
||||
|
||||
1.5: Added risingEdge and fallingEdge
|
||||
|
||||
1.3: Fixed a bug that was confusing a local variable and an argument
|
||||
|
||||
1.2: Added a duration method
|
||||
|
||||
1.1: Removed a badly formated comment that would halt compiling
|
||||
|
||||
1.0: Initial Release
|
||||
|
||||
Description
|
||||
====================
|
||||
|
||||
Bounce is a library for Arduino (arduino.cc). It debounces digital inputs and more (http://en.wikipedia.org/wiki/Debounce#Contact_bounce).
|
||||
|
||||
|
||||
License
|
||||
====================
|
||||
|
||||
MIT License (MIT)
|
||||
Copyright (c) 2012 Thomas O Fredericks
|
||||
|
||||
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.
|
||||
|
||||
|
||||
Download, install and import
|
||||
====================
|
||||
|
||||
• Put the Bounce folder in your Arduino "libraries". To identify this location open "menu -> File -> Preferences".
|
||||
|
||||
|
||||
In the Arduino IDE, select "menubar->Sketch->Import Library->Bounce" to import the library to your sketch. An "#include Bounce.h" line will appear at the top of your Sketch.
|
||||
|
||||
You can also find examples under "menubar->File->Sketchbook->libraries->Bounce"
|
||||
|
||||
|
||||
Methods
|
||||
====================
|
||||
|
||||
# Bounce(byte pin,unsigned long debounce_interval)
|
||||
|
||||
Instantiates a Debounce object for the specified pin with a debounce time.
|
||||
|
||||
Because Bounce does not use interrupts, you have to "update" the object before reading its value.
|
||||
|
||||
# int update()
|
||||
|
||||
Updates Bounce. Returns true if the pin state changed (HIGH to LOW or LOW to HIGH). False if not.
|
||||
|
||||
# void interval(unsigned long interval)
|
||||
|
||||
Changes the debounce time in milliseconds.
|
||||
|
||||
|
||||
# int read()
|
||||
Reads the updated pin state.
|
||||
|
||||
# void write(int state)
|
||||
Sets the stored pin state
|
||||
|
||||
# void rebounce(unsigned long interval)
|
||||
Forces the pin to signal a state change in X milliseconds even if the state does not actually change.
|
||||
Example: A button that repeats every X milliseconds as long as it is held down
|
||||
|
||||
# unsigned long duration(void)
|
||||
Returns the number of milliseconds the pin has been in the current state.
|
||||
|
||||
# bool risingEdge()
|
||||
// The risingEdge method is true for one scan after the de-bounced input goes from off-to-on.
|
||||
|
||||
# bool fallingEdge();
|
||||
The fallingEdge method it true for one scan after the de-bounced input goes from on-to-off.
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <Bounce.h>
|
||||
|
||||
#define BUTTON_PIN 2
|
||||
#define LED_PIN 13
|
||||
|
||||
|
||||
// Instantiate a Bounce object
|
||||
Bounce debouncer = Bounce();
|
||||
|
||||
void setup() {
|
||||
// Setup the button
|
||||
pinMode(BUTTON_PIN,INPUT);
|
||||
digitalWrite(BUTTON_PIN,HIGH);
|
||||
|
||||
// After setting up the button, setup debouncer
|
||||
debouncer.attach(BUTTON_PIN);
|
||||
debouncer.interval(5);
|
||||
|
||||
//Setup the LED
|
||||
pinMode(LED_PIN,OUTPUT);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Update the debouncer
|
||||
debouncer.update();
|
||||
|
||||
// Get the update value
|
||||
int value = debouncer.read();
|
||||
|
||||
// Turn on or off the LED
|
||||
if ( value == HIGH) {
|
||||
digitalWrite(LED_PIN, HIGH );
|
||||
} else {
|
||||
digitalWrite(LED_PIN, LOW );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <Bounce.h>
|
||||
#define BUTTON_PIN 2
|
||||
#define LED_PIN 13
|
||||
|
||||
int ledState = LOW;
|
||||
|
||||
|
||||
// Instantiate a Bounce object
|
||||
Bounce debouncer = Bounce();
|
||||
|
||||
void setup() {
|
||||
// Setup the button
|
||||
pinMode(BUTTON_PIN,INPUT);
|
||||
digitalWrite(BUTTON_PIN,HIGH);
|
||||
|
||||
// After setting up the button, setup Bounce object
|
||||
debouncer.attach(BUTTON_PIN);
|
||||
debouncer.interval(500);
|
||||
|
||||
//Setup the LED
|
||||
pinMode(LED_PIN,OUTPUT);
|
||||
digitalWrite(LED_PIN,ledState);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
|
||||
boolean stateChanged = debouncer.update();
|
||||
int state = debouncer.read();
|
||||
|
||||
// Detect the raising edge
|
||||
if ( stateChanged && state == LOW ) {
|
||||
|
||||
if ( ledState == LOW ) {
|
||||
ledState = HIGH;
|
||||
} else {
|
||||
ledState = LOW;
|
||||
}
|
||||
digitalWrite(LED_PIN,ledState);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 47 KiB |
@@ -0,0 +1,62 @@
|
||||
#include <Bounce.h>
|
||||
|
||||
#define BUTTON_PIN 2
|
||||
#define LED_PIN 13
|
||||
|
||||
// Instantiate a Bounce object
|
||||
Bounce debouncer = Bounce();
|
||||
|
||||
int buttonState;
|
||||
unsigned long buttonPressTimeStamp;
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(57600);
|
||||
|
||||
// Setup the button
|
||||
pinMode(BUTTON_PIN,INPUT);
|
||||
digitalWrite(BUTTON_PIN,HIGH);
|
||||
|
||||
// After setting up the button, setup debouncer
|
||||
debouncer.attach(BUTTON_PIN);
|
||||
debouncer.interval(5);
|
||||
|
||||
//Setup the LED
|
||||
pinMode(LED_PIN,OUTPUT);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Update the debouncer and get the changed state
|
||||
boolean changed = debouncer.update();
|
||||
|
||||
|
||||
|
||||
if ( changed ) {
|
||||
// Get the update value
|
||||
int value = debouncer.read();
|
||||
if ( value == HIGH) {
|
||||
digitalWrite(LED_PIN, HIGH );
|
||||
|
||||
buttonState = 0;
|
||||
Serial.println("Button released (state 0)");
|
||||
|
||||
} else {
|
||||
digitalWrite(LED_PIN, LOW );
|
||||
buttonState = 1;
|
||||
Serial.println("Button pressed (state 1)");
|
||||
buttonPressTimeStamp = millis();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ( buttonState == 1 ) {
|
||||
if ( millis() - buttonPressTimeStamp >= 2000 ) {
|
||||
buttonState = 2;
|
||||
Serial.println("Button held for two seconds (state 2)");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
#include <Bounce.h>
|
||||
|
||||
#define BUTTON_PIN 2
|
||||
#define LED_PIN 13
|
||||
|
||||
// Instantiate a Bounce object
|
||||
Bounce debouncer = Bounce();
|
||||
|
||||
int buttonState;
|
||||
unsigned long buttonPressTimeStamp;
|
||||
|
||||
int ledState;
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(57600);
|
||||
|
||||
// Setup the button
|
||||
pinMode(BUTTON_PIN,INPUT);
|
||||
digitalWrite(BUTTON_PIN,HIGH);
|
||||
|
||||
// After setting up the button, setup debouncer
|
||||
debouncer.attach(BUTTON_PIN);
|
||||
debouncer.interval(5);
|
||||
|
||||
//Setup the LED
|
||||
pinMode(LED_PIN,OUTPUT);
|
||||
digitalWrite(LED_PIN,ledState);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Update the debouncer and get the changed state
|
||||
boolean changed = debouncer.update();
|
||||
|
||||
|
||||
|
||||
if ( changed ) {
|
||||
// Get the update value
|
||||
int value = debouncer.read();
|
||||
if ( value == HIGH) {
|
||||
ledState = LOW;
|
||||
digitalWrite(LED_PIN, ledState );
|
||||
|
||||
buttonState = 0;
|
||||
Serial.println("Button released (state 0)");
|
||||
|
||||
} else {
|
||||
ledState = HIGH;
|
||||
digitalWrite(LED_PIN, ledState );
|
||||
|
||||
buttonState = 1;
|
||||
Serial.println("Button pressed (state 1)");
|
||||
buttonPressTimeStamp = millis();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ( buttonState == 1 ) {
|
||||
if ( millis() - buttonPressTimeStamp >= 500 ) {
|
||||
buttonPressTimeStamp = millis();
|
||||
if ( ledState == HIGH ) ledState = LOW;
|
||||
else if ( ledState == LOW ) ledState = HIGH;
|
||||
digitalWrite(LED_PIN, ledState );
|
||||
Serial.println("Retriggering button");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Put the "Bounce" folder in your Arduino "libraries" folder.
|
||||
Reference in New Issue
Block a user