Skip to content

Commit 13d5e6b

Browse files
Initial release
0 parents  commit 13d5e6b

File tree

10 files changed

+367
-0
lines changed

10 files changed

+367
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Button Library for Arduino
2+
This library is designed for push button, momentary switches. It is easy to use for not only beginners but also experienced users.
3+
4+
Features
5+
----------------------------
6+
* Uses the internal pull-up resistor to avoid the floating value
7+
* Supports debounce to eliminate the chattering phenomenon
8+
* Supports the pressed and released events
9+
* Easy to use with multiple buttons
10+
11+
Available Examples
12+
----------------------------
13+
* 01.SingleButton
14+
* 02.SingleButtonDebounce
15+
* 03.SingleButtonEvents
16+
* 04.SingleButtonAll
17+
* 05.MultipleButtonAll
18+
19+
References
20+
----------------------------
21+
* [Button Library Reference](https://arduinogetstarted.com/tutorials/arduino-button-library)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Created by ArduinoGetStarted.com
3+
*
4+
* This example code is in the public domain
5+
*
6+
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
7+
*
8+
* This example reads the state of a button without debounce and print it to Serial Monitor.
9+
*/
10+
11+
#include <Button.h>
12+
13+
Button button(7); // create Button object that attach to pin 7;
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
}
18+
19+
void loop() {
20+
button.loop(); // MUST call the loop() function first
21+
22+
int btnState = button.getState();
23+
Serial.println(btnState);
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Created by ArduinoGetStarted.com
3+
*
4+
* This example code is in the public domain
5+
*
6+
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
7+
*
8+
* This example reads the state if a button with debounce and print it to Serial Monitor.
9+
*/
10+
11+
#include <Button.h>
12+
13+
Button button(7); // create Button object that attach to pin 7;
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
button.setDebounceTime(100); // set debounce time to 100 milliseconds
18+
}
19+
20+
void loop() {
21+
button.loop(); // MUST call the loop() function first
22+
23+
int btnState = button.getState();
24+
Serial.println(btnState);
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Created by ArduinoGetStarted.com
3+
*
4+
* This example code is in the public domain
5+
*
6+
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
7+
*
8+
* This example detects the pressed and released events of a button without debounce.
9+
*/
10+
11+
#include <Button.h>
12+
13+
Button button(7); // create Button object that attach to pin 7;
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
}
18+
19+
void loop() {
20+
button.loop(); // MUST call the loop() function first
21+
22+
if(button.isPressed())
23+
Serial.println("The button is pressed");
24+
25+
if(button.isReleased())
26+
Serial.println("The button is released");
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Created by ArduinoGetStarted.com
3+
*
4+
* This example code is in the public domain
5+
*
6+
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
7+
*
8+
* This example:
9+
* + uses debounce for a button.
10+
* + reads state of a button
11+
* + detects the pressed and released events of a button
12+
*/
13+
14+
#include <Button.h>
15+
16+
Button button(7); // create Button object that attach to pin 7;
17+
18+
void setup() {
19+
Serial.begin(9600);
20+
button.setDebounceTime(100); // set debounce time to 100 milliseconds
21+
}
22+
23+
void loop() {
24+
button.loop(); // MUST call the loop() function first
25+
26+
int btnState = button.getState();
27+
Serial.println(btnState);
28+
29+
if(button.isPressed())
30+
Serial.println("The button is pressed");
31+
32+
if(button.isReleased())
33+
Serial.println("The button is released");
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Created by ArduinoGetStarted.com
3+
*
4+
* This example code is in the public domain
5+
*
6+
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
7+
*
8+
* This example:
9+
* + uses debounce for multiple buttons.
10+
* + reads state of multiple buttons
11+
* + detects the pressed and released events of multiple buttons
12+
*/
13+
14+
#include <Button.h>
15+
16+
Button button1(6); // create Button object that attach to pin 6;
17+
Button button2(7); // create Button object that attach to pin 7;
18+
19+
void setup() {
20+
Serial.begin(9600);
21+
button1.setDebounceTime(100); // set debounce time to 100 milliseconds
22+
button2.setDebounceTime(100); // set debounce time to 100 milliseconds
23+
}
24+
25+
void loop() {
26+
button1.loop(); // MUST call the loop() function first
27+
button2.loop(); // MUST call the loop() function first
28+
29+
int btn1State = button1.getState();
30+
int btn2State = button2.getState();
31+
Serial.print("button 1 state: ");
32+
Serial.println(btn1State);
33+
Serial.print("button 2 state: ");
34+
Serial.println(btn2State);
35+
36+
if(button1.isPressed())
37+
Serial.println("The button 1 is pressed");
38+
39+
if(button1.isReleased())
40+
Serial.println("The button 1 is released");
41+
42+
if(button2.isPressed())
43+
Serial.println("The button 2 is pressed");
44+
45+
if(button2.isReleased())
46+
Serial.println("The button 2 is released");
47+
}

keywords.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#######################################
2+
# Syntax Coloring Map For Button
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Button KEYWORD1
10+
button KEYWORD1
11+
12+
#######################################
13+
# Methods and Functions (KEYWORD2)
14+
#######################################
15+
16+
setDebounceTime KEYWORD2
17+
getState KEYWORD2
18+
getStateRaw KEYWORD2
19+
isPressed KEYWORD2
20+
isReleased KEYWORD2
21+
22+
23+
#######################################
24+
# Constants (LITERAL1)
25+
#######################################

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=ezButton
2+
version=1.0.0
3+
author=ArduinoGetStarted.com
4+
maintainer=ArduinoGetStarted.com ([email protected])
5+
sentence=Button library for Arduino
6+
paragraph=Button library supports debounce, pressed/released events. It is easy to use with multiple buttons. It is designed for not only beginners but also experienced users
7+
category=Signal Input/Output
8+
url=https://arduinogetstarted.com/tutorials/arduino-button-library
9+
architectures=avr
10+
includes=Button.h

src/Button.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2019, ArduinoGetStarted.com. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the ArduinoGetStarted.com nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY ARDUINOGETSTARTED.COM "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL ARDUINOGETSTARTED.COM BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#include <Button.h>
33+
34+
Button::Button(int pin) {
35+
btnPin = pin;
36+
debounceTime = 0;
37+
38+
pinMode(btnPin, INPUT_PULLUP);
39+
40+
previousSteadyState = digitalRead(btnPin);
41+
lastSteadyState = digitalRead(btnPin);
42+
lastFlickerableState = digitalRead(btnPin);
43+
}
44+
45+
void Button::setDebounceTime(unsigned long time) {
46+
debounceTime = time;
47+
}
48+
49+
int Button::getState(void) {
50+
return lastSteadyState;
51+
}
52+
53+
int Button::getStateRaw(void) {
54+
return digitalRead(btnPin);
55+
}
56+
bool Button::isPressed(void) {
57+
if(previousSteadyState == HIGH && lastSteadyState == LOW)
58+
return true;
59+
else
60+
return false;
61+
}
62+
bool Button::isReleased(void) {
63+
if(previousSteadyState == LOW && lastSteadyState == HIGH)
64+
return true;
65+
else
66+
return false;
67+
}
68+
69+
void Button::loop() {
70+
// read the state of the switch/button:
71+
currentState = digitalRead(btnPin);
72+
73+
// check to see if you just pressed the button
74+
// (i.e. the input went from LOW to HIGH), and you've waited long enough
75+
// since the last press to ignore any noise:
76+
77+
// If the switch/button changed, due to noise or pressing:
78+
if (currentState != lastFlickerableState) {
79+
// reset the debouncing timer
80+
lastDebounceTime = millis();
81+
// save the the last flickerable state
82+
lastFlickerableState = currentState;
83+
}
84+
85+
if ((millis() - lastDebounceTime) >= debounceTime) {
86+
// whatever the reading is at, it's been there for longer than the debounce
87+
// delay, so take it as the actual current state:
88+
89+
// save the the steady state
90+
previousSteadyState = lastSteadyState;
91+
lastSteadyState = currentState;
92+
}
93+
}
94+

src/Button.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2019, ArduinoGetStarted.com. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of the ArduinoGetStarted.com nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY ARDUINOGETSTARTED.COM "AS IS" AND ANY EXPRESS OR
20+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL ARDUINOGETSTARTED.COM BE LIABLE FOR ANY DIRECT,
23+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef Button_h
33+
#define Button_h
34+
35+
#include <Arduino.h>
36+
37+
class Button
38+
{
39+
private:
40+
int btnPin;
41+
unsigned long debounceTime;
42+
43+
int previousSteadyState; // the previous steady state from the input pin, used to detect pressed and released event
44+
int lastSteadyState; // the last steady state from the input pin
45+
int lastFlickerableState; // the last flickerable state from the input pin
46+
int currentState; // the current reading from the input pin
47+
48+
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
49+
50+
public:
51+
Button(int pin);
52+
void setDebounceTime(unsigned long time);
53+
int getState(void);
54+
int getStateRaw(void);
55+
bool isPressed(void);
56+
bool isReleased(void);
57+
void loop();
58+
};
59+
60+
#endif

0 commit comments

Comments
 (0)