first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
## ezButton Library for Arduino
This library is designed to make it easy to use push button, momentary switches, toggle switch, magnetic contact switch (door sensor)... It is easy to use for not only beginners but also experienced users.
__ezButton__ stands for __Easy Button__
Features
----------------------------
* Uses the internal pull-up resistor to avoid the floating value
* Supports debounce to eliminate the chattering phenomenon
* Supports the pressed and released events
* Supports the counting (for FALLING, RISING and BOTH)
* Easy to use with multiple buttons
* All functions are non-blocking
Available Examples
----------------------------
* [01.SingleButton](https://arduinogetstarted.com/library/button/example/arduino-single-button)
* [02.SingleButtonEvents](https://arduinogetstarted.com/library/button/example/arduino-single-button-events)
* [03.SingleButtonDebounce](https://arduinogetstarted.com/library/button/example/arduino-single-button-debounce)
* [04.SingleButtonAll](https://arduinogetstarted.com/library/button/example/arduino-single-button-all)
* [05.MultipleButtonAll](https://arduinogetstarted.com/library/button/example/arduino-multiple-button-all)
* [06.ButtonCount](https://arduinogetstarted.com/library/button/example/arduino-button-count)
* [07.ButtonArray](https://arduinogetstarted.com/library/button/example/arduino-button-array)
Available Functions
----------------------------
* setDebounceTime()
* getState()
* getStateRaw()
* isPressed()
* isReleased()
* setCountMode()
* getCount()
* resetCount()
* loop()
References
----------------------------
* [ezButton Library Reference](https://arduinogetstarted.com/tutorials/arduino-button-library)

View File

@@ -0,0 +1,24 @@
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
*
* This example reads the state of a button without debounce and print it to Serial Monitor.
*/
#include <ezButton.h>
ezButton button(7); // create ezButton object that attach to pin 7;
void setup() {
Serial.begin(9600);
}
void loop() {
button.loop(); // MUST call the loop() function first
int btnState = button.getState();
Serial.println(btnState);
}

View File

@@ -0,0 +1,27 @@
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
*
* This example detects the pressed and released events of a button without debounce.
*/
#include <ezButton.h>
ezButton button(7); // create ezButton object that attach to pin 7;
void setup() {
Serial.begin(9600);
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed())
Serial.println("The button is pressed");
if(button.isReleased())
Serial.println("The button is released");
}

View File

@@ -0,0 +1,28 @@
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
*
* This example reads the state of a button with debounce and print it to Serial Monitor.
*/
#include <ezButton.h>
ezButton button(7); // create ezButton object that attach to pin 7;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
button.loop(); // MUST call the loop() function first
if(button.isPressed())
Serial.println("The button is pressed");
if(button.isReleased())
Serial.println("The button is released");
}

View File

@@ -0,0 +1,34 @@
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
*
* This example:
* + uses debounce for a button.
* + reads state of a button
* + detects the pressed and released events of a button
*/
#include <ezButton.h>
ezButton button(7); // create ezButton object that attach to pin 7;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
button.loop(); // MUST call the loop() function first
int btnState = button.getState();
Serial.println(btnState);
if(button.isPressed())
Serial.println("The button is pressed");
if(button.isReleased())
Serial.println("The button is released");
}

View File

@@ -0,0 +1,59 @@
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
*
* This example:
* + uses debounce for multiple buttons.
* + reads state of multiple buttons
* + detects the pressed and released events of multiple buttons
*/
#include <ezButton.h>
ezButton button1(6); // create ezButton object that attach to pin 6;
ezButton button2(7); // create ezButton object that attach to pin 7;
ezButton button3(8); // create ezButton object that attach to pin 8;
void setup() {
Serial.begin(9600);
button1.setDebounceTime(50); // set debounce time to 50 milliseconds
button2.setDebounceTime(50); // set debounce time to 50 milliseconds
button3.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
button1.loop(); // MUST call the loop() function first
button2.loop(); // MUST call the loop() function first
button3.loop(); // MUST call the loop() function first
int btn1State = button1.getState();
int btn2State = button2.getState();
int btn3State = button3.getState();
Serial.print("button 1 state: ");
Serial.println(btn1State);
Serial.print("button 2 state: ");
Serial.println(btn2State);
Serial.print("button 3 state: ");
Serial.println(btn3State);
if(button1.isPressed())
Serial.println("The button 1 is pressed");
if(button1.isReleased())
Serial.println("The button 1 is released");
if(button2.isPressed())
Serial.println("The button 2 is pressed");
if(button2.isReleased())
Serial.println("The button 2 is released");
if(button3.isPressed())
Serial.println("The button 3 is pressed");
if(button3.isReleased())
Serial.println("The button 3 is released");
}

View File

@@ -0,0 +1,26 @@
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
*
* This example reads the number of the pressed count of a button with debounce and print it to Serial Monitor.
*/
#include <ezButton.h>
ezButton button(7); // create ezButton object that attach to pin 7;
void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds
button.setCountMode(COUNT_FALLING);
}
void loop() {
button.loop(); // MUST call the loop() function first
unsigned long count = button.getCount();
Serial.println(count);
}

View File

@@ -0,0 +1,54 @@
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-library
*
* This example shows how to use array of button.
*/
#include <ezButton.h>
const int BUTTON_NUM = 5;
const int BUTTON_1_PIN = 2;
const int BUTTON_2_PIN = 3;
const int BUTTON_3_PIN = 4;
const int BUTTON_4_PIN = 5;
const int BUTTON_5_PIN = 6;
ezButton buttonArray[] = {
ezButton(BUTTON_1_PIN),
ezButton(BUTTON_2_PIN),
ezButton(BUTTON_3_PIN),
ezButton(BUTTON_4_PIN),
ezButton(BUTTON_5_PIN)
};
void setup() {
Serial.begin(9600);
for (byte i = 0; i < BUTTON_NUM; i++) {
buttonArray[i].setDebounceTime(50); // set debounce time to 50 milliseconds
}
}
void loop() {
for (byte i = 0; i < BUTTON_NUM; i++)
buttonArray[i].loop(); // MUST call the loop() function first
for (byte i = 0; i < BUTTON_NUM; i++) {
if (buttonArray[i].isPressed()) {
Serial.print("The button ");
Serial.print(i + 1);
Serial.println(" is pressed");
}
if (buttonArray[i].isReleased()) {
Serial.print("The button ");
Serial.print(i + 1);
Serial.println(" is released");
}
}
}

View File

@@ -0,0 +1,31 @@
#######################################
# Syntax Coloring Map For ezButton
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
ezButton KEYWORD1
button KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
setDebounceTime KEYWORD2
getState KEYWORD2
getStateRaw KEYWORD2
isPressed KEYWORD2
isReleased KEYWORD2
setCountMode KEYWORD2
getCount KEYWORD2
resetCount KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
COUNT_FALLING LITERAL1
COUNT_RISING LITERAL1
COUNT_BOTH LITERAL1

View File

@@ -0,0 +1,10 @@
name=ezButton
version=1.0.4
author=ArduinoGetStarted.com
maintainer=ArduinoGetStarted.com (ArduinoGetStarted@gmail.com)
sentence=Button library for Arduino
paragraph=Button library supports debounce, pressed/released events and the press counting. It is easy to use with multiple buttons. The library can be used for push-button, momentary switches, toggle switch, magnetic contact switch (door sensor)... It is designed for not only beginners but also experienced users.
category=Signal Input/Output
url=https://arduinogetstarted.com/tutorials/arduino-button-library
architectures=*
includes=ezButton.h

View File

@@ -0,0 +1,127 @@
/*
* Copyright (c) 2019, ArduinoGetStarted.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the ArduinoGetStarted.com nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ARDUINOGETSTARTED.COM "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ARDUINOGETSTARTED.COM BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ezButton.h>
ezButton::ezButton(int pin): ezButton(pin, INPUT_PULLUP) {};
ezButton::ezButton(int pin, int mode) {
btnPin = pin;
debounceTime = 0;
count = 0;
countMode = COUNT_FALLING;
pinMode(btnPin, mode);
previousSteadyState = digitalRead(btnPin);
lastSteadyState = previousSteadyState;
lastFlickerableState = previousSteadyState;
lastDebounceTime = 0;
}
void ezButton::setDebounceTime(unsigned long time) {
debounceTime = time;
}
int ezButton::getState(void) {
return lastSteadyState;
}
int ezButton::getStateRaw(void) {
return digitalRead(btnPin);
}
bool ezButton::isPressed(void) {
if(previousSteadyState == HIGH && lastSteadyState == LOW)
return true;
else
return false;
}
bool ezButton::isReleased(void) {
if(previousSteadyState == LOW && lastSteadyState == HIGH)
return true;
else
return false;
}
void ezButton::setCountMode(int mode) {
countMode = mode;
}
unsigned long ezButton::getCount(void) {
return count;
}
void ezButton::resetCount(void) {
count = 0;
}
void ezButton::loop(void) {
// read the state of the switch/button:
int currentState = digitalRead(btnPin);
unsigned long currentTime = millis();
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch/button changed, due to noise or pressing:
if (currentState != lastFlickerableState) {
// reset the debouncing timer
lastDebounceTime = currentTime;
// save the the last flickerable state
lastFlickerableState = currentState;
}
if ((currentTime - lastDebounceTime) >= debounceTime) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// save the the steady state
previousSteadyState = lastSteadyState;
lastSteadyState = currentState;
}
if(previousSteadyState != lastSteadyState){
if(countMode == COUNT_BOTH)
count++;
else if(countMode == COUNT_FALLING){
if(previousSteadyState == HIGH && lastSteadyState == LOW)
count++;
}
else if(countMode == COUNT_RISING){
if(previousSteadyState == LOW && lastSteadyState == HIGH)
count++;
}
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2019, ArduinoGetStarted.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the ArduinoGetStarted.com nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ARDUINOGETSTARTED.COM "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ARDUINOGETSTARTED.COM BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ezButton_h
#define ezButton_h
#include <Arduino.h>
#define COUNT_FALLING 0
#define COUNT_RISING 1
#define COUNT_BOTH 2
class ezButton
{
private:
int btnPin;
unsigned long debounceTime;
unsigned long count;
int countMode;
int previousSteadyState; // the previous steady state from the input pin, used to detect pressed and released event
int lastSteadyState; // the last steady state from the input pin
int lastFlickerableState; // the last flickerable state from the input pin
unsigned long lastDebounceTime; // the last time the output pin was toggled
public:
ezButton(int pin);
ezButton(int pin, int mode);
void setDebounceTime(unsigned long time);
int getState(void);
int getStateRaw(void);
bool isPressed(void);
bool isReleased(void);
void setCountMode(int mode);
unsigned long getCount(void);
void resetCount(void);
void loop(void);
};
#endif