From 7ca879a7ff1b75c3018e242aa375d74d0ff78acd Mon Sep 17 00:00:00 2001 From: thomasfredericks Date: Sat, 21 Dec 2013 13:34:31 -0500 Subject: [PATCH] Initial commit --- Bounce.cpp | 92 +++++++++++++++++++++++ Bounce.h | 70 ++++++++++++++++++ examples/._bounce | Bin 0 -> 4096 bytes examples/._change | Bin 0 -> 4096 bytes examples/._duration | Bin 0 -> 4096 bytes examples/._rebounce | Bin 0 -> 4096 bytes examples/bounce/._bounce.pde | Bin 0 -> 4096 bytes examples/bounce/bounce.pde | 32 ++++++++ examples/change/._change.pde | Bin 0 -> 4096 bytes examples/change/change.pde | 31 ++++++++ examples/duration/._duration.pde | Bin 0 -> 4096 bytes examples/duration/duration.pde | 59 +++++++++++++++ examples/edge/edge.pde | 121 +++++++++++++++++++++++++++++++ examples/rebounce/._rebounce.pde | Bin 0 -> 4096 bytes examples/rebounce/rebounce.pde | 48 ++++++++++++ 15 files changed, 453 insertions(+) create mode 100644 Bounce.cpp create mode 100644 Bounce.h create mode 100644 examples/._bounce create mode 100644 examples/._change create mode 100644 examples/._duration create mode 100644 examples/._rebounce create mode 100644 examples/bounce/._bounce.pde create mode 100644 examples/bounce/bounce.pde create mode 100644 examples/change/._change.pde create mode 100644 examples/change/change.pde create mode 100644 examples/duration/._duration.pde create mode 100644 examples/duration/duration.pde create mode 100644 examples/edge/edge.pde create mode 100644 examples/rebounce/._rebounce.pde create mode 100644 examples/rebounce/rebounce.pde diff --git a/Bounce.cpp b/Bounce.cpp new file mode 100644 index 0000000..ec6c37c --- /dev/null +++ b/Bounce.cpp @@ -0,0 +1,92 @@ + +// 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(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; } + diff --git a/Bounce.h b/Bounce.h new file mode 100644 index 0000000..7be294c --- /dev/null +++ b/Bounce.h @@ -0,0 +1,70 @@ + +/* + * 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 + +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 + + diff --git a/examples/._bounce b/examples/._bounce new file mode 100644 index 0000000000000000000000000000000000000000..3e6bd4e69b9fb2b86c349cac522133e544e25075 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^44`s1G%bukK2%&ZIX_n~v7jI)Rj;r#u_!UGBr`9S!N9=4(Ap$9#URDlB+=S8 zF*C>7%+bxv$jH@P*TTrrP}juR#7)=2z}ZmO$;s5k)zs45($d1Y24Pq(P!F1|qukLD z7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Eu=CVI2aX&L#+hfm}#NX0bw1YH@yPQF5w6 yT7FTsLS|k`DzMv`pO=`EQ>l=XnpUEal#`g34eSd;bq#3>)&Fp>$S}zL{|^AkfjVvg literal 0 HcmV?d00001 diff --git a/examples/._change b/examples/._change new file mode 100644 index 0000000000000000000000000000000000000000..3e6bd4e69b9fb2b86c349cac522133e544e25075 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^44`s1G%bukK2%&ZIX_n~v7jI)Rj;r#u_!UGBr`9S!N9=4(Ap$9#URDlB+=S8 zF*C>7%+bxv$jH@P*TTrrP}juR#7)=2z}ZmO$;s5k)zs45($d1Y24Pq(P!F1|qukLD z7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Eu=CVI2aX&L#+hfm}#NX0bw1YH@yPQF5w6 yT7FTsLS|k`DzMv`pO=`EQ>l=XnpUEal#`g34eSd;bq#3>)&Fp>$S}zL{|^AkfjVvg literal 0 HcmV?d00001 diff --git a/examples/._duration b/examples/._duration new file mode 100644 index 0000000000000000000000000000000000000000..3e6bd4e69b9fb2b86c349cac522133e544e25075 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^44`s1G%bukK2%&ZIX_n~v7jI)Rj;r#u_!UGBr`9S!N9=4(Ap$9#URDlB+=S8 zF*C>7%+bxv$jH@P*TTrrP}juR#7)=2z}ZmO$;s5k)zs45($d1Y24Pq(P!F1|qukLD z7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Eu=CVI2aX&L#+hfm}#NX0bw1YH@yPQF5w6 yT7FTsLS|k`DzMv`pO=`EQ>l=XnpUEal#`g34eSd;bq#3>)&Fp>$S}zL{|^AkfjVvg literal 0 HcmV?d00001 diff --git a/examples/._rebounce b/examples/._rebounce new file mode 100644 index 0000000000000000000000000000000000000000..3e6bd4e69b9fb2b86c349cac522133e544e25075 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^44`s1G%bukK2%&ZIX_n~v7jI)Rj;r#u_!UGBr`9S!N9=4(Ap$9#URDlB+=S8 zF*C>7%+bxv$jH@P*TTrrP}juR#7)=2z}ZmO$;s5k)zs45($d1Y24Pq(P!F1|qukLD z7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Eu=CVI2aX&L#+hfm}#NX0bw1YH@yPQF5w6 yT7FTsLS|k`DzMv`pO=`EQ>l=XnpUEal#`g34eSd;bq#3>)&Fp>$S}zL{|^AkfjVvg literal 0 HcmV?d00001 diff --git a/examples/bounce/._bounce.pde b/examples/bounce/._bounce.pde new file mode 100644 index 0000000000000000000000000000000000000000..a725af206b68aeaec354d5f1b604822578c68c11 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^44`s1G%bukK2%&ZIX_n~v7jI)Rj;r#u_!UGBr`9S!N9=8(Ap$9#URDlB+=S8 zF*C>7%+bxv$jH@P*TTrrP}juR#7)=2z}ZmO$;s5k)zs45($d1Y24Pq(P!F1|qukLD z7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Eu=CVI2aX&L#+hfm}#NX0bw1YH@yPQF5w6 yT7FTsLS|k`DzMv`pO=`EQ>l=XnpUEal#`g34eSd;bq#3>)&Fp>$S}zL{|^8MC^~Wg literal 0 HcmV?d00001 diff --git a/examples/bounce/bounce.pde b/examples/bounce/bounce.pde new file mode 100644 index 0000000..d706884 --- /dev/null +++ b/examples/bounce/bounce.pde @@ -0,0 +1,32 @@ +#include + +// 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 ); + } + +} + diff --git a/examples/change/._change.pde b/examples/change/._change.pde new file mode 100644 index 0000000000000000000000000000000000000000..3e6bd4e69b9fb2b86c349cac522133e544e25075 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^44`s1G%bukK2%&ZIX_n~v7jI)Rj;r#u_!UGBr`9S!N9=4(Ap$9#URDlB+=S8 zF*C>7%+bxv$jH@P*TTrrP}juR#7)=2z}ZmO$;s5k)zs45($d1Y24Pq(P!F1|qukLD z7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Eu=CVI2aX&L#+hfm}#NX0bw1YH@yPQF5w6 yT7FTsLS|k`DzMv`pO=`EQ>l=XnpUEal#`g34eSd;bq#3>)&Fp>$S}zL{|^AkfjVvg literal 0 HcmV?d00001 diff --git a/examples/change/change.pde b/examples/change/change.pde new file mode 100644 index 0000000..63074e0 --- /dev/null +++ b/examples/change/change.pde @@ -0,0 +1,31 @@ +#include +#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); + } + } +} + diff --git a/examples/duration/._duration.pde b/examples/duration/._duration.pde new file mode 100644 index 0000000000000000000000000000000000000000..3e6bd4e69b9fb2b86c349cac522133e544e25075 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^44`s1G%bukK2%&ZIX_n~v7jI)Rj;r#u_!UGBr`9S!N9=4(Ap$9#URDlB+=S8 zF*C>7%+bxv$jH@P*TTrrP}juR#7)=2z}ZmO$;s5k)zs45($d1Y24Pq(P!F1|qukLD z7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Eu=CVI2aX&L#+hfm}#NX0bw1YH@yPQF5w6 yT7FTsLS|k`DzMv`pO=`EQ>l=XnpUEal#`g34eSd;bq#3>)&Fp>$S}zL{|^AkfjVvg literal 0 HcmV?d00001 diff --git a/examples/duration/duration.pde b/examples/duration/duration.pde new file mode 100644 index 0000000..3a6faed --- /dev/null +++ b/examples/duration/duration.pde @@ -0,0 +1,59 @@ +#include + +/* +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... + + } + } + +} diff --git a/examples/edge/edge.pde b/examples/edge/edge.pde new file mode 100644 index 0000000..b38f29b --- /dev/null +++ b/examples/edge/edge.pde @@ -0,0 +1,121 @@ + +// 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 + +// 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); +} diff --git a/examples/rebounce/._rebounce.pde b/examples/rebounce/._rebounce.pde new file mode 100644 index 0000000000000000000000000000000000000000..3e6bd4e69b9fb2b86c349cac522133e544e25075 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIUt(=a103vvYvJF zKST$^44`s1G%bukK2%&ZIX_n~v7jI)Rj;r#u_!UGBr`9S!N9=4(Ap$9#URDlB+=S8 zF*C>7%+bxv$jH@P*TTrrP}juR#7)=2z}ZmO$;s5k)zs45($d1Y24Pq(P!F1|qukLD z7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Eu=CVI2aX&L#+hfm}#NX0bw1YH@yPQF5w6 yT7FTsLS|k`DzMv`pO=`EQ>l=XnpUEal#`g34eSd;bq#3>)&Fp>$S}zL{|^AkfjVvg literal 0 HcmV?d00001 diff --git a/examples/rebounce/rebounce.pde b/examples/rebounce/rebounce.pde new file mode 100644 index 0000000..a312bdc --- /dev/null +++ b/examples/rebounce/rebounce.pde @@ -0,0 +1,48 @@ +#include + +// 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 ); + + } + + + +} +