Moving to version 2 of the library
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user