Rename library & repository from Button to JC_Button.
This commit is contained in:
@@ -14,7 +14,7 @@
|
|||||||
* San Francisco, California, 94105, USA. *
|
* San Francisco, California, 94105, USA. *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include "Button.h"
|
#include "JC_Button.h"
|
||||||
|
|
||||||
/*----------------------------------------------------------------------*
|
/*----------------------------------------------------------------------*
|
||||||
* Button(pin, puEnable, invert, dbTime) instantiates a button object. *
|
* Button(pin, puEnable, invert, dbTime) instantiates a button object. *
|
||||||
@@ -39,4 +39,4 @@ class Button
|
|||||||
uint32_t _lastChange; //time of last state change
|
uint32_t _lastChange; //time of last state change
|
||||||
uint32_t _dbTime; //debounce time
|
uint32_t _dbTime; //debounce time
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
34
README.md
34
README.md
@@ -1,20 +1,12 @@
|
|||||||
# Arduino Button Library v1.0
|
# Arduino Button Library
|
||||||
https://github.com/JChristensen/Button
|
https://github.com/JChristensen/JC_Button
|
||||||
ReadMe file
|
README file
|
||||||
Jack Christensen Mar 2012
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
The Button library is for debouncing and reading momentary contact switches like tactile button switches. "Long presses" of arbitrary length can be detected. Works well in state machine constructs. Use the read() function to read each button in the main loop, which should execute as fast as possible.
|
The Button library is for debouncing and reading momentary contact switches like tactile button switches. "Long presses" of arbitrary length can be detected. Works well in state machine constructs. Use the read() function to read each button in the main loop, which should execute as fast as possible.
|
||||||
|
|
||||||
## Installation
|
|
||||||
To use the **Button** library:
|
|
||||||
- Go to https://github.com/JChristensen/Button, click the **Download ZIP** button and save the ZIP file to a convenient location on your PC.
|
|
||||||
- Uncompress the downloaded file. This will result in a folder containing all the files for the library, that has a name that includes the branch name, usually **Button-master**.
|
|
||||||
- Rename the folder to just **Button**.
|
|
||||||
- Copy the renamed folder to the Arduino sketchbook\libraries folder.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
The following example sketches are included with the **Button** library:
|
The following example sketches are included with the **Button** library:
|
||||||
|
|
||||||
@@ -22,7 +14,7 @@ The following example sketches are included with the **Button** library:
|
|||||||
- **LongPress**: Demonstrates detecting long and short button presses.
|
- **LongPress**: Demonstrates detecting long and short button presses.
|
||||||
- **UpDown**: Counts up or down, one number at a time or rapidly by holding the button down.
|
- **UpDown**: Counts up or down, one number at a time or rapidly by holding the button down.
|
||||||
|
|
||||||
## Button library methods
|
## Button library functions
|
||||||
|
|
||||||
### Button(pin, puEnable, invert, dbTime)
|
### Button(pin, puEnable, invert, dbTime)
|
||||||
##### Description
|
##### Description
|
||||||
@@ -41,7 +33,7 @@ None.
|
|||||||
Button myButton = Button(2, true, true, 25); //25 ms debounce
|
Button myButton = Button(2, true, true, 25); //25 ms debounce
|
||||||
```
|
```
|
||||||
|
|
||||||
### read(void)
|
### read()
|
||||||
##### Description
|
##### Description
|
||||||
Reads the button and returns a *boolean* value (*true* or *false*) to indicate whether the button is pressed. The read() function needs to execute very frequently in order for the sketch to be responsive. A good place for read() is at the top of loop(). Often, the return value from read() will not be needed if the other functions below are used.
|
Reads the button and returns a *boolean* value (*true* or *false*) to indicate whether the button is pressed. The read() function needs to execute very frequently in order for the sketch to be responsive. A good place for read() is at the top of loop(). Often, the return value from read() will not be needed if the other functions below are used.
|
||||||
##### Syntax
|
##### Syntax
|
||||||
@@ -55,12 +47,12 @@ None.
|
|||||||
myButton.read();
|
myButton.read();
|
||||||
```
|
```
|
||||||
|
|
||||||
### isPressed(void)
|
### isPressed()
|
||||||
### isReleased(void)
|
### isReleased()
|
||||||
##### Description
|
##### Description
|
||||||
These methods check the button state at the point in time when it was last read, and return false or true accordingly. These functions **do not** cause the button to be read.
|
These methods check the button state at the point in time when it was last read, and return false or true accordingly. These functions **do not** cause the button to be read.
|
||||||
##### Syntax
|
##### Syntax
|
||||||
`myButton.isPressed();`
|
`myButton.isPressed();`
|
||||||
`myButton.isReleased();`
|
`myButton.isReleased();`
|
||||||
##### Parameters
|
##### Parameters
|
||||||
None.
|
None.
|
||||||
@@ -76,12 +68,12 @@ else {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### wasPressed(void)
|
### wasPressed()
|
||||||
### wasReleased(void)
|
### wasReleased()
|
||||||
##### Description
|
##### Description
|
||||||
These methods check the button state to see if it changed between the last two reads and return false or true accordingly. These functions **do not** cause the button to be read. Note that these functions may be more useful than `isPressed()` and `isReleased()` since they actually detect a **change** in the state of the button, which is usually what we want in order to cause some action.
|
These methods check the button state to see if it changed between the last two reads and return false or true accordingly. These functions **do not** cause the button to be read. Note that these functions may be more useful than `isPressed()` and `isReleased()` since they actually detect a **change** in the state of the button, which is usually what we want in order to cause some action.
|
||||||
##### Syntax
|
##### Syntax
|
||||||
`myButton.wasPressed();`
|
`myButton.wasPressed();`
|
||||||
`myButton.wasReleased();`
|
`myButton.wasReleased();`
|
||||||
##### Parameters
|
##### Parameters
|
||||||
None.
|
None.
|
||||||
@@ -97,7 +89,7 @@ if ( myButton.wasPressed() ) { ...
|
|||||||
##### Description
|
##### Description
|
||||||
These methods check to see if the button is pressed (or released), and has been in that state for the specified time in milliseconds. Returns false or true accordingly. These functions are useful to detect "long presses". Note that these functions **do not** cause the button to be read.
|
These methods check to see if the button is pressed (or released), and has been in that state for the specified time in milliseconds. Returns false or true accordingly. These functions are useful to detect "long presses". Note that these functions **do not** cause the button to be read.
|
||||||
##### Syntax
|
##### Syntax
|
||||||
`myButton.pressedFor(ms);`
|
`myButton.pressedFor(ms);`
|
||||||
`myButton.releasedFor(ms);`
|
`myButton.releasedFor(ms);`
|
||||||
##### Parameters
|
##### Parameters
|
||||||
**ms:** The number of milliseconds *(unsigned long)*
|
**ms:** The number of milliseconds *(unsigned long)*
|
||||||
@@ -108,7 +100,7 @@ These methods check to see if the button is pressed (or released), and has been
|
|||||||
if ( myButton.pressedFor(1000) ) { //has the button been pressed for one second?
|
if ( myButton.pressedFor(1000) ) { //has the button been pressed for one second?
|
||||||
```
|
```
|
||||||
|
|
||||||
### lastChange(void)
|
### lastChange()
|
||||||
##### Description
|
##### Description
|
||||||
Under certain circumstances, it may be useful to know when a button last changed state. lastChange() returns the time the button last changed state, in milliseconds (the value is derived from the Arduino millis() function).
|
Under certain circumstances, it may be useful to know when a button last changed state. lastChange() returns the time the button last changed state, in milliseconds (the value is derived from the Arduino millis() function).
|
||||||
##### Syntax
|
##### Syntax
|
||||||
|
|||||||
@@ -15,21 +15,20 @@
|
|||||||
* San Francisco, California, 94105, USA. *
|
* San Francisco, California, 94105, USA. *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include <Button.h> //https://github.com/JChristensen/Button
|
#include <JC_Button.h> //https://github.com/JChristensen/JC_Button
|
||||||
|
|
||||||
#define BUTTON_PIN 2 //Connect a tactile button switch (or something similar)
|
#define BUTTON_PIN 7 //Connect a tactile button switch (or something similar) from this pin to ground.
|
||||||
//from Arduino pin 2 to ground.
|
|
||||||
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
|
#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
|
#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
|
//switch is closed, this is negative logic, i.e. a high state
|
||||||
//means the button is NOT pressed. (Assuming a normally open switch.)
|
//means the button is NOT pressed. (Assuming a normally open switch.)
|
||||||
#define DEBOUNCE_MS 20 //A debounce time of 20 milliseconds usually works well for tactile button switches.
|
#define DEBOUNCE_MS 25 //A debounce time of 20 milliseconds usually works well for tactile button switches.
|
||||||
|
|
||||||
#define LED_PIN 13 //The standard Arduino "Pin 13" LED.
|
#define LED_PIN 13 //The standard Arduino "Pin 13" LED.
|
||||||
#define LONG_PRESS 1000 //We define a "long press" to be 1000 milliseconds.
|
#define LONG_PRESS 1000 //We define a "long press" to be 1000 milliseconds.
|
||||||
#define BLINK_INTERVAL 100 //In the BLINK state, switch the LED every 100 milliseconds.
|
#define BLINK_INTERVAL 100 //In the BLINK state, switch the LED every 100 milliseconds.
|
||||||
|
|
||||||
Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Declare the button
|
Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Define the button
|
||||||
|
|
||||||
//The list of possible states for the state machine. This state machine has a fixed
|
//The list of possible states for the state machine. This state machine has a fixed
|
||||||
//sequence of states, i.e. ONOFF --> TO_BLINK --> BLINK --> TO_ONOFF --> ONOFF
|
//sequence of states, i.e. ONOFF --> TO_BLINK --> BLINK --> TO_ONOFF --> ONOFF
|
||||||
@@ -41,12 +40,12 @@ boolean ledState; //The current LED status
|
|||||||
unsigned long ms; //The current time from millis()
|
unsigned long ms; //The current time from millis()
|
||||||
unsigned long msLast; //The last time the LED was switched
|
unsigned long msLast; //The last time the LED was switched
|
||||||
|
|
||||||
void setup(void)
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(LED_PIN, OUTPUT); //Set the LED pin as an output
|
pinMode(LED_PIN, OUTPUT); //Set the LED pin as an output
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(void)
|
void loop()
|
||||||
{
|
{
|
||||||
ms = millis(); //record the current time
|
ms = millis(); //record the current time
|
||||||
myBtn.read(); //Read the button
|
myBtn.read(); //Read the button
|
||||||
|
|||||||
@@ -12,26 +12,25 @@
|
|||||||
* San Francisco, California, 94105, USA. *
|
* San Francisco, California, 94105, USA. *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include <Button.h> //https://github.com/JChristensen/Button
|
#include <JC_Button.h> //https://github.com/JChristensen/JC_Button
|
||||||
|
|
||||||
#define BUTTON_PIN 2 //Connect a tactile button switch (or something similar)
|
#define BUTTON_PIN 7 //Connect a tactile button switch (or something similar) from this pin to ground.
|
||||||
//from Arduino pin 2 to ground.
|
|
||||||
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
|
#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
|
#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
|
//switch is closed, this is negative logic, i.e. a high state
|
||||||
//means the button is NOT pressed. (Assuming a normally open switch.)
|
//means the button is NOT pressed. (Assuming a normally open switch.)
|
||||||
#define DEBOUNCE_MS 20 //A debounce time of 20 milliseconds usually works well for tactile button switches.
|
#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
|
#define LED_PIN 13 //The standard Arduino "Pin 13" LED
|
||||||
|
|
||||||
Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Declare the button
|
Button myBtn(BUTTON_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Define the button
|
||||||
boolean ledState; //A variable that keeps the current LED status
|
boolean ledState; //A variable that keeps the current LED status
|
||||||
|
|
||||||
void setup(void)
|
void setup()
|
||||||
{
|
{
|
||||||
pinMode(LED_PIN, OUTPUT); //Set the LED pin as an output
|
pinMode(LED_PIN, OUTPUT); //Set the LED pin as an output
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(void)
|
void loop()
|
||||||
{
|
{
|
||||||
myBtn.read(); //Read the button
|
myBtn.read(); //Read the button
|
||||||
|
|
||||||
|
|||||||
@@ -13,22 +13,22 @@
|
|||||||
* San Francisco, California, 94105, USA. *
|
* San Francisco, California, 94105, USA. *
|
||||||
*----------------------------------------------------------------------*/
|
*----------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include <Button.h> //https://github.com/JChristensen/Button
|
#include <JC_Button.h> //https://github.com/JChristensen/JC_Button
|
||||||
|
|
||||||
#define DN_PIN 2 //Connect two tactile button switches (or something similar)
|
#define DN_PIN 7 //Connect two tactile button switches (or something similar)
|
||||||
#define UP_PIN 3 //from Arduino pin 2 to ground and from pin 3 to ground.
|
#define UP_PIN 8 //from Arduino pin 7 to ground and from pin 8 to ground.
|
||||||
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
|
#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
|
#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
|
//switch is closed, this is negative logic, i.e. a high state
|
||||||
//means the button is NOT pressed. (Assuming a normally open switch.)
|
//means the button is NOT pressed. (Assuming a normally open switch.)
|
||||||
#define DEBOUNCE_MS 20 //A debounce time of 20 milliseconds usually works well for tactile button switches.
|
#define DEBOUNCE_MS 25 //A debounce time of 20 milliseconds usually works well for tactile button switches.
|
||||||
|
|
||||||
#define REPEAT_FIRST 500 //ms required before repeating on long press
|
#define REPEAT_FIRST 500 //ms required before repeating on long press
|
||||||
#define REPEAT_INCR 100 //repeat interval for long press
|
#define REPEAT_INCR 100 //repeat interval for long press
|
||||||
#define MIN_COUNT 0
|
#define MIN_COUNT 0
|
||||||
#define MAX_COUNT 59
|
#define MAX_COUNT 59
|
||||||
|
|
||||||
Button btnUP(UP_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Declare the buttons
|
Button btnUP(UP_PIN, PULLUP, INVERT, DEBOUNCE_MS); //Define the buttons
|
||||||
Button btnDN(DN_PIN, PULLUP, INVERT, DEBOUNCE_MS);
|
Button btnDN(DN_PIN, PULLUP, INVERT, DEBOUNCE_MS);
|
||||||
|
|
||||||
enum {WAIT, INCR, DECR}; //The possible states for the state machine
|
enum {WAIT, INCR, DECR}; //The possible states for the state machine
|
||||||
@@ -37,12 +37,12 @@ int count; //The number that is adjusted
|
|||||||
int lastCount = -1; //Previous value of count (initialized to ensure it's different when the sketch starts)
|
int lastCount = -1; //Previous value of count (initialized to ensure it's different when the sketch starts)
|
||||||
unsigned long rpt = REPEAT_FIRST; //A variable time that is used to drive the repeats for long presses
|
unsigned long rpt = REPEAT_FIRST; //A variable time that is used to drive the repeats for long presses
|
||||||
|
|
||||||
void setup(void)
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(void)
|
void loop()
|
||||||
{
|
{
|
||||||
btnUP.read(); //read the buttons
|
btnUP.read(); //read the buttons
|
||||||
btnDN.read();
|
btnDN.read();
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
name=Button
|
name=JC_Button
|
||||||
version=1.0.1
|
version=1.0.2
|
||||||
author=Jack Christensen <jack.christensen@outlook.com>
|
author=Jack Christensen <jack.christensen@outlook.com>
|
||||||
maintainer=Jack Christensen <jack.christensen@outlook.com>
|
maintainer=Jack Christensen <jack.christensen@outlook.com>
|
||||||
sentence=Arduino library to debounce button switches, detect presses, releases, and long presses.
|
sentence=Arduino library to debounce button switches, detect presses, releases, and long presses.
|
||||||
paragraph=The Button library is for debouncing and reading momentary contact switches like tactile button switches. "Long presses" of arbitrary length can be detected. Works well in state machine constructs. Use the read() function to read each button in the main loop, which should execute as fast as possible.
|
paragraph=The Button library is for debouncing and reading momentary contact switches like tactile button switches. "Long presses" of arbitrary length can be detected. Works well in state machine constructs. Use the read() function to read each button in the main loop, which should execute as fast as possible.
|
||||||
category=Signal Input/Output
|
category=Signal Input/Output
|
||||||
url=https://github.com/JChristensen/Button
|
url=https://github.com/JChristensen/JC_Button
|
||||||
architectures=avr
|
architectures=avr
|
||||||
|
|||||||
Reference in New Issue
Block a user