Giter Club home page Giter Club logo

pushbutton's Introduction

PushButton library for Arduino

An Arduino library that makes it easy to check for push button events (click, double-click, hold) without blocking your application with a delay.

Installation

  1. Download the zip-file from https://github.com/kristianklein/PushButton
  2. Open Arduino IDE
  3. Go to Sketch -> Include Library -> Add .ZIP Library...
  4. Browse to the downloaded zip-file and press Open
  5. Read the usage instructions below or open up one of the example sketches in File -> Examples -> PushButton

Simple usage instructions

First include the library either by browsing to Sketch -> Include Library -> PushButton or by typing the include-directive at the beginning of your sketch:

#include <PushButton.h>

Now create a PushButton-object. I'll call mine myButton, but you can call yours whatever you like - and create more than just one! The class constructors takes just one input parameter: the pin that your push button is connected to. I have connected mine to digital pin 2.

PushButton myButton(2)

In your setup() function, you may want to set the given pin as an input, but it is not strictly necessary as all digital pins are inputs by default.

void setup() {
	pinMode(2, INPUT);
}

In your loop() function, it is very important that you run the update() function for each of your buttons before checking for events. The update() function reads the digital pin and determines whether the pin is active or inactive, and if it has been clicked, double-clicked or held down for a specified amount of time. After running the update function, let's check if the button has been clicked.

void loop() {
	myButton.update();
	
	if (myButton.isClicked())
	{
		// ... your code goes here ...
	}
}

All there's left to do now is to write your application! To learn how to check for other button events or to configure the button exactly to your need, keep reading the sections below.

Check for button events

You can use the following member functions to check for button events.

myButton.isActive() returns true if the button is currently being pressed down. You can change the logic from active high to active low if necessary (see Advanced configuration).

myButton.isClicked() returns true if the button has just been clicked. It is automatically debounced without using the delay-function, so it doesn't block the rest of your application.

myButton.isReleased() returns true if the button has just been released. This is also automatically debounced.

myButton.isDoubleClicked() returns true if the button has been clicked twice within a short period of time.

myButton.isHeld() returns true if the button is being held down for a specified amount of time, i.e. a long push.

Advanced configuration

You can configure the push button to suit your needs by using the followin member functions.

myButton.setActiveLogic(HIGH_or_LOW) is used to specify whether the digital logic is active high or active low [default: HIGH].

myButton.setDebounceTime(milliseconds) sets the debounce time (in milliseconds) [default: 10].

myButton.setDoubleClickTime(milliseconds) sets the maximum amount of time (in milliseconds) between two clicks for a double-click event to be triggered [default: 300].

myButton.setHoldTime(milliseconds) sets the amount time (in milliseconds) a button must be held down to trigger a hold event [default: 2000].

myButton.disableDoubleClick() disables the double-click feature. Consecutive button clicks will only trigger a regular click event.

myButton.enableDoubleClick() enables the double-click feature [default: enabled].

pushbutton's People

Contributors

kristianklein avatar per1234 avatar

Watchers

James Cloos avatar Andrei Pozolotin avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.