Initial commit
This commit is contained in:
+92
@@ -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; }
|
||||
|
||||
@@ -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 <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
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,32 @@
|
||||
#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 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,31 @@
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,59 @@
|
||||
#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...
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <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);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,48 @@
|
||||
#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 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user