All Projects → JChristensen → Jc_button

JChristensen / Jc_button

Licence: gpl-3.0
Arduino library to debounce button switches, detect presses, releases, and long presses

Projects that are alternatives of or similar to Jc button

Easybutton
Arduino library for debouncing momentary contact switches, detect press, release, long press and sequences with event definitions and callbacks.
Stars: ✭ 187 (-35.29%)
Mutual labels:  switch, arduino, arduino-library, button
Onebutton
An Arduino library for using a single button for multiple purpose input.
Stars: ✭ 418 (+44.64%)
Mutual labels:  arduino, arduino-library, button
Acebutton
An adjustable, compact, event-driven button library for Arduino that debounces and dispatches events to a user-defined event handler.
Stars: ✭ 180 (-37.72%)
Mutual labels:  arduino, arduino-library, button
Md parola
Library for modular scrolling LED matrix text displays
Stars: ✭ 282 (-2.42%)
Mutual labels:  arduino, arduino-library
Arduino Tvout
Arduino-TVout
Stars: ✭ 216 (-25.26%)
Mutual labels:  arduino, arduino-library
Helios
The free embedded operating system.
Stars: ✭ 223 (-22.84%)
Mutual labels:  arduino, arduino-library
Dsckeybusinterface
An Arduino/esp8266/esp32 library to directly interface with DSC security systems.
Stars: ✭ 202 (-30.1%)
Mutual labels:  arduino, arduino-library
Ioabstraction
Library for Arduino and mbed that abstracts pins and i2c expanders (8574, 23017), supports AVR and I2c AT24 EEPROMs, Rotary encoders, fully debounced switches and simple task management.
Stars: ✭ 84 (-70.93%)
Mutual labels:  switch, arduino
Vue Js Toggle Button
🍥 Vue.js 2 toggle / switch button - simple, pretty, customizable
Stars: ✭ 836 (+189.27%)
Mutual labels:  switch, button
Swifitch
Swifitch is ESP8266 based relay board that could be used to turn any light or any wall socket into smart one!
Stars: ✭ 117 (-59.52%)
Mutual labels:  switch, arduino
RevealLayout
揭示效果布局,可以指定2个子布局,以圆形揭示效果切换选中状态
Stars: ✭ 118 (-59.17%)
Mutual labels:  button, switch
Arduinowebsockets
A library for writing modern websockets applications with Arduino (ESP8266 and ESP32)
Stars: ✭ 213 (-26.3%)
Mutual labels:  arduino, arduino-library
Lpd8806
Arduino library for LED strips and pixels using LPD8806 (and probably LPD8803/LPD8809)
Stars: ✭ 207 (-28.37%)
Mutual labels:  arduino, arduino-library
Linkedlist
🔗 A fully implemented LinkedList made to work with general Microcontrollers and Arduino projects
Stars: ✭ 241 (-16.61%)
Mutual labels:  arduino, arduino-library
Timezone
Arduino library to facilitate time zone conversions and automatic daylight saving (summer) time adjustments.
Stars: ✭ 205 (-29.07%)
Mutual labels:  arduino, arduino-library
Aiflatswitch
Nicely animated flat design switch alternative to UISwitch
Stars: ✭ 904 (+212.8%)
Mutual labels:  switch, button
Tristatetogglebutton
Customizable tri-state toggle button (with three states, three state toggle) for Android
Stars: ✭ 198 (-31.49%)
Mutual labels:  switch, button
encoder
Atmel AVR C++ RotaryEncoder Implementation
Stars: ✭ 39 (-86.51%)
Mutual labels:  button, arduino-library
Teensystep
Fast Stepper Motor Library for Teensy boards
Stars: ✭ 191 (-33.91%)
Mutual labels:  arduino, arduino-library
Radio
An Arduino library to control FM radio chips like SI4703, SI4705, RDA5807M, TEA5767.
Stars: ✭ 193 (-33.22%)
Mutual labels:  arduino, arduino-library

Arduino Button Library

https://github.com/JChristensen/JC_Button
README file

License

Arduino Button Library Copyright (C) 2018-2019 Jack Christensen GNU GPL v3.0

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License v3.0 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/gpl.html

Introduction

The Button library is for debouncing and reading momentary contact switches like tactile button switches. "Long presses" of arbitrary length can be detected. Works well in state machine constructs. Use the read() function to read each button in the main loop, which should execute as fast as possible.

The simplest way to use a button with an AVR microcontroller is to wire the button between a GPIO pin and ground, and turn on the AVR internal pullup resistor. The Button class constructor takes four arguments, but three have default values that work for a button wired in this manner.

A derived class, ToggleButton, implements button objects that need only "push-on, push-off" functionality.

Examples

The following example sketches are included with the Button library:

  • SimpleOnOff: Just turns the Arduino's pin 13 LED on and off.
  • LongPress: Demonstrates detecting long and short button presses.
  • UpDown: Counts up or down, one number at a time or rapidly by holding the button down.
  • Toggle: Demonstrates ToggleButton functionality.

Constructors

Button(pin, dbTime, puEnable, invert)

Description

The constructor defines a button object.

Syntax

Button(pin, dbTime, puEnable, invert);

Required parameter

pin: Arduino pin number that the button is connected to (byte)

Optional parameters

dbTime: Debounce time in milliseconds. Defaults to 25ms if not given. (unsigned long)
puEnable: true to enable the microcontroller's internal pull-up resistor, else false. Defaults to true if not given. (bool)
invert: false interprets a high logic level to mean the button is pressed, true interprets a low level as pressed. true should be used when a pull-up resistor is employed, false for a pull-down resistor. Defaults to true if not given. (bool)

Returns

None.

Example
// button connected from pin 2 to ground, 25ms debounce, pullup enabled, logic inverted
Button myButton(2);

// same as above but this button needs a longer debounce time (50ms)
Button myButton(3, 50);

// a button wired from the MCU pin to Vcc with an external pull-down resistor
Button myButton(4, 25, false, false);

ToggleButton(pin, initialState, dbTime, puEnable, invert)

Description

The constructor defines a toggle button object, which has "push-on, push-off" functionality. The initial state can be on or off. See the section, ToggleButton Library Functions for functions that apply specifically to the ToggleButton object. The ToggleButton class is derived from the Button class, so all Button functions are available, but because it is inherently a more limited concept, the special ToggleButton functions will be most useful, along with begin() and read().

Syntax

ToggleButton(pin, initialState, dbTime, puEnable, invert);

Required parameter

pin: Arduino pin number that the button is connected to (byte)

Optional parameters

initialState: Initial state for the button. Defaults to off (false) if not given. (bool)
dbTime: Debounce time in milliseconds. Defaults to 25ms if not given. (unsigned long)
puEnable: true to enable the microcontroller's internal pull-up resistor, else false. Defaults to true if not given. (bool)
invert: false interprets a high logic level to mean the button is pressed, true interprets a low level as pressed. true should be used when a pull-up resistor is employed, false for a pull-down resistor. Defaults to true if not given. (bool)

Returns

None.

Example
// button connected from pin 2 to ground, initial state off,
// 25ms debounce, pullup enabled, logic inverted
ToggleButton myToggle(2);

// same as above but this button is initially "on" and also
// needs a longer debounce time (50ms).
ToggleButton myToggle(3, true, 50);

// a button wired from the MCU pin to Vcc with an external pull-down resistor,
// initial state is off.
Button myButton(4, false, 25, false, false);

Button Library Functions

begin()

Description

Initializes the Button object and the pin it is connected to.

Syntax

myButton.begin();

Parameters

None.

Returns

None.

Example
myButton.begin();

read()

Description

Reads the button and returns a boolean value (true or false) to indicate whether the button is pressed. The read() function needs to execute very frequently in order for the sketch to be responsive. A good place for read() is at the top of loop(). Often, the return value from read() will not be needed if the other functions below are used.

Syntax

myButton.read();

Parameters

None.

Returns

true if the button is pressed, else false (bool)

Example
myButton.read();

isPressed()

isReleased()

Description

These functions check the button state from the last call to read() and return false or true accordingly. These functions do not cause the button to be read.

Syntax

myButton.isPressed();
myButton.isReleased();

Parameters

None.

Returns

true or false, depending on whether the button has been pressed (released) or not (bool)

Example
if ( myButton.isPressed() )
{
	//do something
}
else
{
	//do something else
}

wasPressed()

wasReleased()

Description

These functions check the button state to see if it changed between the last two calls to read() and return false or true accordingly. These functions do not cause the button to be read. Note that these functions may be more useful than isPressed() and isReleased() since they actually detect a change in the state of the button, which is usually what we want in order to cause some action.

Syntax

myButton.wasPressed();
myButton.wasReleased();

Parameters

None.

Returns

true or false, depending on whether the button was pressed (released) or not (boolean)

Example
if ( myButton.wasPressed() )
{
	//do something
}

pressedFor(ms)

releasedFor(ms)

Description

These functions check to see if the button is pressed (or released), and has been in that state for the specified time in milliseconds. Returns false or true accordingly. These functions are useful to detect "long presses". Note that these functions do not cause the button to be read.

Syntax

myButton.pressedFor(ms);
myButton.releasedFor(ms);

Parameters

ms: The number of milliseconds (unsigned long)

Returns

true or false, depending on whether the button was pressed (released) for the specified time (bool)

Example
if ( myButton.pressedFor(1000) )
{
    // button has been pressed for one second
}

lastChange()

Description

Under certain circumstances, it may be useful to know when a button last changed state. lastChange() returns the time the button last changed state, in milliseconds (the value is derived from the Arduino millis() function).

Syntax

myButton.lastChange();

Parameters

None.

Returns

The time in milliseconds when the button last changed state (unsigned long)

Example
unsigned long msLastChange = myButton.lastChange();

ToggleButton Library Functions

changed()

Description

Returns a boolean value (true or false) to indicate whether the toggle button changed state the last time read() was called.

Syntax

myToggle.changed();

Parameters

None.

Returns

true if the toggle state changed, else false (bool)

Example
if (myToggle.changed())
{
    // do something
}
else
{
    // do something different
}

toggleState()

Description

Returns a boolean value (true or false) to indicate the toggle button state as of the last time read() was called.

Syntax

myToggle.toggleState();

Parameters

None.

Returns

true if the toggle is "on", else false (bool)

Example
if (myToggle.toggleState())
{
    // do something
}
else
{
    // do something different
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].