Updated documentation
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
|
||||
/*
|
||||
DESCRIPTION
|
||||
====================
|
||||
Pressing a physical button switches a LED on or off.
|
||||
Simple example of the Button class from the Bounce library.
|
||||
The Button class matches an electrical state to a physical action
|
||||
with <Button>.setPressedState(LOW or HIGH).
|
||||
|
||||
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
|
||||
Button button = Button();
|
||||
|
||||
|
||||
|
||||
// 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();
|
||||
|
||||
// <Button>.pressed() RETURNS true IF THE STATE CHANGED
|
||||
// AND THE CURRENT STATE MATCHES <Button>.setPressedState(<HIGH or LOW>);
|
||||
if ( button.pressed() ) {
|
||||
|
||||
// TOGGLE THE LED STATE :
|
||||
ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
|
||||
digitalWrite(LED_PIN,ledState); // WRITE THE NEW ledState
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user