Skip to content

PanGalacticTech/buttonObject_Library

Repository files navigation

buttonObject_Library (v1.0.0)

Built and maintained by Imogen Heard on behalf of Pan Galactic Tech

Features

  • Libaray to handle and debounce button inputs using OOP topology.
  • Button is sampled periodically and history saved into a bitstream.
  • Bitmasks used to compare bitstream to a rising edge or falling edge over a number of samples.
  • This method is great at rejecting noise from dirty contacts or cheap buttons.
  • Button defined as a Pull Up or Pull Down button in constructor.
  • Method can differentiate between long press & short press.
  • Adjustable long press duration.
  • Returns total number of button presses.
  • Returns total number of button releases.
  • Returns total number of button longPresses (In resting state: releaseCount + longPressCount = pressCount).
  • Ultra fast and low overhead method.
  • Each buttonObject controls one input, add a new constructer for each button required.

Getting Started

Deployment

  1. Clone or Download library folder into Arduino IDE libraries folder.
  2. Extract files from zipped folder if required.
  3. Open Arduino IDE and select example program.

Dependencies

No dependency for library function, however examples have the following dependencies:

  • ledObject_library available from here - written by PanGalacticTech

Use

Precompiler Requirements

#include <buttonObject.h> // Libary include

Button Pin Declaration

#define BUTTON_PIN 4 // Select Button input pin

Long Press Time Declaration

#define LONG_PRESS_TIME 750 // time in milliseconds


Object Constructors

Declaring a button object:

buttonObject button(BUTTON_PIN, BUTTON_PULL_HIGH);

If button is LOW pulled HIGH on press, use: BUTTON_PULL_HIGH

Else If button is HIGH pulled LOW on press, use: BUTTON_PULL_LOW



Setup Functions:

Setup function must be run once before .buttonLoop() method.

button.begin();

.begin() Function sets up button input pin into input mode, and sets INPUT_PULLUP, if button is specified
BUTTON_PULL_LOW




Loop Functions:

Loop function must be called in every loop & as often as possible. Delays between loop executions will affect function.

button.buttonLoop(LONG_PRESS_TIME); // long int controls the time required for a long press. (defaults to 1000 mS)

This function will record the bitstream and return boolean flags when button states are detected.



Detecting Button Press:

Short Press

A short press is detected when the user releases the button in a time shorter than LONG_PRESS_TIME.
bool is set true on button release.

button.shortPress

Long Press

A long press is detected when the user keeps button depressed longer than LONG_PRESS_TIME.
bool is set true while button is depressed and button will not trigger short press untill it is released and reset.

button.longPress

Resetting the Triggers

button.shortPress & button.longPress bools act as triggers and must be reset after triggering associated behaviour.

button.buttonReset(); // .buttonReset method resets longPress & shortPress variables once action has been taken

Example Loop Using Reset Behaviour

button.buttonLoop(); // long int passed controls the time required for a long press. (defaults to 1 second)

if (button.shortPress) {
led.toggleLED();
button.buttonReset(); // .buttonReset method resets longPress & shortPress variables once action has been taken
}


Advanced Uses:





Limitations:


Known Issues V1.0.0

  • Please report any bugs or issues found.

Features to be Added



About

Built With

Author

Imogen Wren H

About

Libaray to handle and debounce button inputs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages