Files
JCButton/examples/SimpleOnOff/SimpleOnOff.ino
2018-05-10 17:37:47 -04:00

44 lines
2.1 KiB
C++

/*----------------------------------------------------------------------*
* Example sketch for Arduino Button Library by Jack Christensen *
* *
* The simplest example. Using a tactile button switch to turn *
* the Arduino's pin 13 LED on and off. Wire a switch from Arduino *
* pin 2 to ground. *
* *
* This work is licensed under the Creative Commons Attribution- *
* ShareAlike 3.0 Unported License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
* letter to Creative Commons, 171 Second Street, Suite 300, *
* San Francisco, California, 94105, USA. *
*----------------------------------------------------------------------*/
#include <JC_Button.h> //https://github.com/JChristensen/JC_Button
#define BUTTON_PIN 7 //Connect a tactile button switch (or something similar) from this pin to ground.
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
#define INVERT true //Since the pullup resistor will keep the pin high unless the
//switch is closed, this is negative logic, i.e. a high state
//means the button is NOT pressed. (Assuming a normally open switch.)
#define DEBOUNCE_MS 25 //A debounce time of 25 milliseconds usually works well for tactile button switches.
#define LED_PIN 13 //The standard Arduino "Pin 13" LED
Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Define the button
boolean ledState; //A variable that keeps the current LED status
void setup()
{
pinMode(LED_PIN, OUTPUT); //Set the LED pin as an output
}
void loop()
{
myBtn.read(); //Read the button
if (myBtn.wasReleased()) { //If the button was released, change the LED state
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
}
}