Skip to content

Commit ea58053

Browse files
committed
modified: examples/button/button.ino
modified: examples/resetset/resetset.ino new file: examples/resetset/temp.ino modified: examples/toggle/toggle.ino modified: keywords.txt modified: library.properties modified: src/adebouncer.cpp modified: src/adebouncer.h
1 parent 19d8626 commit ea58053

File tree

8 files changed

+138
-110
lines changed

8 files changed

+138
-110
lines changed

examples/Button/Button.ino

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
/*
2-
Declare debounce mode as delayed mode.
3-
Debounce the input signal from the button
4-
and update LED_BUILTIN with the debounced state.
2+
This code initializes a delayed debounce mode, debounces the button input signal,
3+
and updates the state of the built-in LED accordingly.
54
*/
6-
5+
76
#include "ADebouncer.h"
8-
9-
#define buttonPin 2 // Define the button input pin.
10-
#define debouncePeroid 10 // Define the debounce period in milliseconds
11-
12-
ADebouncer debouncer; // Declare debouncer variable.
13-
7+
8+
#define BUTTON_INPUT_PIN 2 // Pin for the button input.
9+
#define DEBOUNCE_PERIOD_MS 10 // Debounce period in milliseconds.
10+
11+
ADebouncer buttonDebouncer; // Create a debouncer instance.
12+
1413
void setup() {
15-
pinMode(buttonPin, INPUT_PULLUP); // Set the button mode as input pullup.
16-
pinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode as output.
17-
debouncer.mode(DELAYED, debouncePeroid, HIGH); // Set the debounce mode as delayed mode and debounce period as 10 ms, with the initial output in a HIGH state.
14+
pinMode(BUTTON_INPUT_PIN, INPUT_PULLUP); // Set the button input pin as input with pull-up.
15+
pinMode(LED_BUILTIN, OUTPUT); // Set the built-in LED pin as output.
16+
buttonDebouncer.setMode(DELAYED, DEBOUNCE_PERIOD_MS, HIGH); // Set debounce mode to delayed with a 10ms period, starting with a HIGH output.
1817
}
19-
18+
2019
void loop() {
21-
bool buttonState = debouncer.debounce(digitalRead(buttonPin)); // Save the debounced of the button state.
22-
digitalWrite(LED_BUILTIN, buttonState); // Update LED_BUILTIN with the button state.
20+
bool debouncedButtonState = buttonDebouncer.debounce(digitalRead(BUTTON_INPUT_PIN)); // Get the debounced button state.
21+
digitalWrite(LED_BUILTIN, debouncedButtonState); // Update the built-in LED with the debounced button state.
2322
}

examples/ResetSet/ResetSet.ino

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
/*
2-
Declare debounce mode as delayed mode.
3-
Debounce the input signal from the button.
4-
Toggle the state when pressing the button and update LED_BUILTIN with the toggle state.
2+
Description: Debounces the input signals from the set and reset buttons and updates the state of LED_BUILTIN accordingly.
53
*/
64

75
#include "ADebouncer.h"
86

9-
#define setPin 12 // Define the set input pin.
10-
#define resetPin 11 // Define the reset input pin.
11-
#define debouncePeroid 1000 // Define the debounce period in milliseconds
7+
#define SET_PIN 12 // Define the set input pin.
8+
#define RESET_PIN 11 // Define the reset input pin.
9+
#define DEBOUNCE_PERIOD_MS 1000 // Define the debounce period in milliseconds
1210

1311
ADebouncer setButton; // Declare set debouncer variable.
1412
ADebouncer resetButton; // Declare reset debouncer variable.
1513
bool state; // Declare state variable for ResetSet.
1614

1715
void setup() {
18-
pinMode(setPin, INPUT_PULLUP); // Set the button mode as input pullup.
19-
pinMode(resetPin, INPUT_PULLUP); // Set the button mode as input pullup.
20-
pinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode as output.
21-
setButton.mode(DELAYED, debouncePeroid, HIGH); // Set the debounce mode as delayed mode and debounce period, with the initial output in a HIGH state.
22-
resetButton.mode(INSTANT, debouncePeroid, HIGH); // Set the debounce mode as instant mode and debounce period, with the initial output in a HIGH state.
23-
state = LOW; // Initial state in a LOW state.
16+
pinMode(SET_PIN, INPUT_PULLUP); // Set the button mode as input pullup.
17+
pinMode(RESET_PIN, INPUT_PULLUP); // Set the button mode as input pullup.
18+
pinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode as output.
19+
setButton.setMode(DELAYED, DEBOUNCE_PERIOD_MS, HIGH); // Set the debounce mode as delayed mode and debounce period, with the initial output in a HIGH state.
20+
resetButton.setMode(INSTANT, DEBOUNCE_PERIOD_MS, HIGH); // Set the debounce mode as instant mode and debounce period, with the initial output in a HIGH state.
21+
state = LOW; // Initial state in a LOW state.
2422
}
2523

2624
void loop() {
27-
setButton.debounce(digitalRead(setPin)); // Debounce input of the set button state.
28-
resetButton.debounce(digitalRead(resetPin)); // Debounce input of the reset button state.
29-
state = (state | !setButton.debounced()) & resetButton.debounced(); // Reset and Set the state
30-
digitalWrite(LED_BUILTIN, state); // Update LED_BUILTIN with the state.
25+
// Debounce input of the set button state.
26+
setButton.debounce(digitalRead(SET_PIN));
27+
// Debounce input of the reset button state.
28+
resetButton.debounce(digitalRead(RESET_PIN));
29+
30+
// Update the state based on set and reset button outputs.
31+
state = (state | !setButton.getDebouncedOutput()) & resetButton.getDebouncedOutput();
32+
// Update LED_BUILTIN with the state.
33+
digitalWrite(LED_BUILTIN, state);
3134
}

examples/ResetSet/temp.ino

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
This code initializes a delayed debounce mode, debounces the button input signal,
3+
and updates the state of the built-in LED accordingly.
4+
*/
5+
6+
#include "ADebouncer.h"
7+
8+
#define BUTTON_INPUT_PIN 2 // Pin for the button input.
9+
#define DEBOUNCE_PERIOD_MS 10 // Debounce period in milliseconds.
10+
11+
ADebouncer buttonDebouncer; // Create a debouncer instance.
12+
13+
void setup() {
14+
pinMode(BUTTON_INPUT_PIN, INPUT_PULLUP); // Set the button input pin as input with pull-up.
15+
pinMode(LED_BUILTIN, OUTPUT); // Set the built-in LED pin as output.
16+
buttonDebouncer.setMode(DELAYED, DEBOUNCE_PERIOD_MS, HIGH); // Set debounce mode to delayed with a 10ms period, starting with a HIGH output.
17+
}
18+
19+
void loop() {
20+
bool debouncedButtonState = buttonDebouncer.debounce(digitalRead(BUTTON_INPUT_PIN)); // Get the debounced button state.
21+
digitalWrite(LED_BUILTIN, debouncedButtonState); // Update the built-in LED with the debounced button state.
22+
}

examples/Toggle/Toggle.ino

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
/*
2-
This example is designed to have a set button and a reset button to Reset-Set the state.
3-
- Declare debouncer for the set button. set the debounce as a delayed mode
4-
- Declare debouncer for the reset button. Set the debounce as an instant mode
2+
This example sets up a debouncer for a set button and a reset button to control the state.
3+
- Declare a debouncer for the set button with delayed mode for debouncing.
4+
- Declare a debouncer for the reset button with instant mode for debouncing.
55
6-
Both set and reset buttons have the same debounce period. in this example, will be set to debounce period for 1 second,
6+
Both set and reset buttons have a debounce period set to 1 second.
77
- To set the state, press the set button for 1 second.
8-
- To reset the state, the state will be instantly reset after pressing the reset button.
8+
- To reset the state, press the reset button for an instant reset.
99
10-
The LED_BUILTIN will be updated with the state value.
10+
The LED_BUILTIN will reflect the state value.
1111
*/
1212

1313
#include "ADebouncer.h"
1414

15-
#define buttonPin 12 // Define the button input pin.
16-
#define debouncePeroid 10 // Define the debounce period in milliseconds
15+
#define BUTTON_INPUT_PIN 12 // Define the button input pin.
16+
#define DEBOUNCE_PERIOD_MS 10 // Define the debounce period in milliseconds
1717

18-
ADebouncer debouncer; // Declare debouncer variable.
19-
bool state; // Declare state variable.
18+
ADebouncer buttonDebouncer; // Declare debouncer variable.
19+
bool state; // Declare state variable.
2020

2121
void setup() {
22-
pinMode(buttonPin, INPUT_PULLUP); // Set the button mode as input pullup.
23-
pinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode as output.
24-
debouncer.mode(DELAYED, debouncePeroid, HIGH); // Set the debounce mode as delayed mode and debounce period, with the initial output in a HIGH state.
25-
state = HIGH; // Initial state in a HIGH state.
22+
pinMode(BUTTON_INPUT_PIN, INPUT_PULLUP); // Set the button mode as input pullup.
23+
pinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode as output.
24+
buttonDebouncer.setMode(DELAYED, DEBOUNCE_PERIOD_MS, HIGH); // Set the debounce mode as delayed mode and debounce period, with the initial output in a HIGH state.
25+
state = HIGH; // Initial state is set to HIGH.
2626
}
2727

2828
void loop() {
29-
debouncer.debounce(digitalRead(buttonPin)); // Debounce input of the button state.
30-
if (debouncer.falling()) state = !state; // Toggle state of the state variable.
31-
digitalWrite(LED_BUILTIN, state); // Update LED_BUILTIN with the state.
29+
buttonDebouncer.debounce(digitalRead(BUTTON_INPUT_PIN)); // Debounce input of the button state.
30+
if (buttonDebouncer.isFallingEdge()) state = !state; // Toggle state of the state variable.
31+
digitalWrite(LED_BUILTIN, state); // Update LED_BUILTIN with the state.
3232
}

keywords.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ instant_t KEYWORD1
1111
#######################################
1212
# Methods and Functions (KEYWORD2)
1313
#######################################
14-
mode KEYWORD2
15-
debounce KEYWORD2
16-
input KEYWORD2
17-
debouncing KEYWORD2
18-
debounced KEYWORD2
19-
rising KEYWORD2
20-
falling KEYWORD2
21-
MS2US KEYWORD2
14+
setMode KEYWORD2
15+
debounce KEYWORD2
16+
getInputState KEYWORD2
17+
isDebouncing KEYWORD2
18+
getDebouncedOutput KEYWORD2
19+
isRisingEdge KEYWORD2
20+
isFallingEdge KEYWORD2
21+
MS2US KEYWORD2
2222

2323
#######################################
2424
# Constants (LITERAL1)

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ADebouncer
2-
version=1.1.1
2+
version=1.2.0
33
author=MicroBeaut
44
maintainer=MicroBeaut
55
sentence=Advanced Debouncer Library for Arduino.

src/ADebouncer.cpp

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,60 @@
1010
#include "ADebouncer.h"
1111

1212
ADebouncer::ADebouncer() {
13-
_instant = DEFAULT_DEBOUNCE_MODE;
14-
_debouncePeriod = MS2US(DEFAULT_DEBOUNCE_PERIOD);
15-
_output = DEFAULT_INITIAL_OUTPUT;
13+
_debounceMode = DEFAULT_DEBOUNCE_MODE;
14+
_debouncePeriodMicros = MS2US(DEFAULT_DEBOUNCE_PERIOD);
15+
_outputState = DEFAULT_INITIAL_OUTPUT;
1616
}
1717

18-
void ADebouncer::mode(debounce_t instant, unsigned long debouncePeriod, boolean initOutput) {
19-
_instant = instant;
20-
_debouncePeriod = MS2US(debouncePeriod);
21-
_output = initOutput;
18+
void ADebouncer::setMode(DebounceMode mode, unsigned long debouncePeriod, boolean initialOutputState) {
19+
_debounceMode = mode;
20+
_debouncePeriodMicros = MS2US(debouncePeriod);
21+
_outputState = initialOutputState;
2222
}
2323

2424
boolean ADebouncer::debounce(boolean input) {
2525
unsigned long currentTime = micros();
26-
_prevInput = _input;
27-
_prevOutput = _output;
28-
_input = input;
29-
if (_input ^ _prevInput) _debounceStartTime = currentTime;
30-
if (_debouncing) {
26+
_previousInputState = _inputState;
27+
_previousOutputState = _outputState;
28+
_inputState = input;
29+
if (_inputState ^ _previousInputState) {
30+
_debounceStartTime = currentTime;
31+
}
32+
if (_isDebouncing) {
3133
unsigned long elapsedTime = currentTime - _debounceStartTime;
32-
if (elapsedTime >= _debouncePeriod) {
33-
_debouncing = false;
34-
_output = _input;
34+
if (elapsedTime >= _debouncePeriodMicros) {
35+
_isDebouncing = false;
36+
_outputState = _inputState;
3537
}
3638
} else {
37-
if (_input ^ _output) {
38-
if (_instant) _output = _input;
39-
_debouncing = true;
39+
if (_inputState ^ _outputState) {
40+
if (_debounceMode) {
41+
_outputState = _inputState;
42+
}
43+
_isDebouncing = true;
4044
}
4145
}
42-
_rising = _output & ~_prevOutput;
43-
_falling = ~_output & _prevOutput;
44-
return _output;
46+
_risingEdge = _outputState & ~_previousOutputState;
47+
_fallingEdge = ~_outputState & _previousOutputState;
48+
return _outputState;
4549
}
4650

47-
boolean ADebouncer::input() {
48-
return _input;
51+
boolean ADebouncer::getInputState() {
52+
return _inputState;
4953
}
5054

51-
boolean ADebouncer::debouncing() {
52-
return _debouncing;
55+
boolean ADebouncer::isDebouncing() {
56+
return _isDebouncing;
5357
}
5458

55-
boolean ADebouncer::debounced() {
56-
return _output;
59+
boolean ADebouncer::getDebouncedOutput() {
60+
return _outputState;
5761
}
5862

59-
boolean ADebouncer::rising() {
60-
return _rising;
63+
boolean ADebouncer::isRisingEdge() {
64+
return _risingEdge;
6165
}
6266

63-
boolean ADebouncer::falling() {
64-
return _falling;
67+
boolean ADebouncer::isFallingEdge() {
68+
return _fallingEdge;
6569
}

src/ADebouncer.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,45 @@
1212

1313
#include <Arduino.h>
1414

15-
#define ADEBOUNCER_VERSION "1.1.1"
15+
#define ADEBOUNCER_VERSION "1.2.0"
1616
#define ADEBOUNCER_AUTHOR "MicroBeaut"
1717

18-
enum debounce_t : boolean {DELAYED, INSTANT};
18+
enum DebounceMode : boolean {DELAYED, INSTANT};
1919

20-
#define DEFAULT_DEBOUNCE_PERIOD 10 // Default debounce Period in milliseconds
21-
#define DEFAULT_DEBOUNCE_MODE DELAYED // Default debounce mode
20+
#define DEFAULT_DEBOUNCE_PERIOD 10 // Default debounce period in milliseconds
21+
#define DEFAULT_DEBOUNCE_MODE DebounceMode::DELAYED // Default debounce mode
2222
#define DEFAULT_INITIAL_OUTPUT true // Default initial output
2323

2424
#ifndef MS2US
25-
#define MS2US(ms) (ms*1000UL)
25+
#define MS2US(ms) (ms * 1000UL)
2626
#endif // MS2US
2727

2828
class ADebouncer {
2929
private:
30-
unsigned char _instant: 1;
31-
unsigned char _debouncing: 1;
30+
unsigned char _debounceMode: 1;
31+
unsigned char _isDebouncing: 1;
3232

33-
unsigned char _input: 1;
34-
unsigned char _prevInput: 1;
33+
unsigned char _inputState: 1;
34+
unsigned char _previousInputState: 1;
3535

36-
unsigned char _output: 1;
37-
unsigned char _prevOutput: 1;
36+
unsigned char _outputState: 1;
37+
unsigned char _previousOutputState: 1;
3838

39-
unsigned char _rising: 1;
40-
unsigned char _falling: 1;
39+
unsigned char _risingEdge: 1;
40+
unsigned char _fallingEdge: 1;
4141

42-
unsigned long _debouncePeriod;
42+
unsigned long _debouncePeriodMicros;
4343
unsigned long _debounceStartTime;
4444

4545
public:
4646
ADebouncer();
47-
void mode(debounce_t instant, unsigned long debouncePeriod = DEFAULT_DEBOUNCE_PERIOD, boolean initOutput = DEFAULT_INITIAL_OUTPUT);
48-
boolean debounce(boolean input);
49-
boolean input();
50-
boolean debouncing();
51-
boolean debounced();
52-
boolean rising();
53-
boolean falling();
47+
void setMode(DebounceMode mode, unsigned long debouncePeriod = DEFAULT_DEBOUNCE_PERIOD, boolean initialOutputState = DEFAULT_INITIAL_OUTPUT);
48+
boolean debounce(boolean inputState);
49+
boolean getInputState();
50+
boolean isDebouncing();
51+
boolean getDebouncedOutput();
52+
boolean isRisingEdge();
53+
boolean isFallingEdge();
5454
};
5555

5656
#endif // ADEBOUNCER_H

0 commit comments

Comments
 (0)