Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

I will be maintaining my libraries here:
https://bit.ly/pATDBi

I am the lead developer for libraries 
that ship with the Wiring distribution. 
As per version 1.0 -
Wiring will support Arduino boards.
You are welcome to check it out!
https://wiring.org.co/download/
Button Library for Arduino
Author:  Alexander Brevig
Contact: alexanderbrevig@gmail.com


Navigation


Current version

Note: Alexander has ported Button to Wiring, and is currently maintaining it at http://wiring.uniandes.edu.co/source/trunk/wiring/firmware/libraries/Button/ A current Arduino of his Wiring version can be found at http://github.com/tigoe/Button. This version is compatible with the current development version of Arduino and should be compatible with version 1.0 and forward''

1.6 2009-05-05: Added uniquePress():bool


History

1.6 2009-05-05: Added uniquePress():bool
1.5 2009-04-24: Added stateChanged():bool
1.4 2009-04-24: Added wasPressed():bool
1.3 2009-04-12: Constructor that only requires a buttonPin. Presumes PULLDOWN
1.2 2009-04-10: Name changed from Switch to Button
1.1 2009-04-07: Altered API
1.0 2008-10-23: Initial Release


Description

Button is a library for the Arduino.

It is created to help Hardware Abstraction, and readability of code. It hides the pinMode, and digitalRead calls for the user.

Button library is part of the Hardware Abstraction libraries.
Also a part of the tutorial: How to extend libraries using Hierarchy.


Download, install and import

Download here: Attach:Button.zip (for version 0022 and previous)
version for Arduino 1.0

Put the Button folder in "hardware\libraries\".
In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sketch->Import Library->Button".
Once the library is imported, an '#include <Button.h>' line will appear at the top of your Sketch.


Creation

Button(byte buttonPin,byte buttonMode)

Button button = Button(12,PULLUP);

Instanciates a Button object with a digital pin number (12), and a PULLUP (or PULLDOWN) configuration.


Functions

void pullup()

Changes the Button mode to a pullup configuration

void pulldown()

Changes the Button mode to a pulldown configuration

boolean isPressed()

Checks to see if the button is pressed and returns true if it is.

boolean wasPressed()

Checks to see if the button was pressed the last time isPressed() was called, and returns true if it was.

boolean stateChanged()

Checks to see if the state of the button has changed. Such as from pressed to released or released to pressed. Returns true if state has changed since last isPressed() call.

boolean uniquePress()

Checks to see if the state of the button has changed AND the button is pressed. This will only return true the first time the button has been pressed. (As opposed to the isPressed() which will return true as long as the button is pressed.


Example

  1. #include <Button.h>
  2. /*
  3. create a Button object at pin 12
  4. connect button between pin 12 and GND
  5. */
  6. Button button = Button(12,PULLUP);
  7.  
  8. void setup(){
  9.   pinMode(13,OUTPUT); //debug to led 13
  10. }
  11.  
  12. void loop(){
  13.   if(button.isPressed()){
  14.         digitalWrite(13,HIGH);
  15.   }else{
  16.         digitalWrite(13,LOW);
  17.   }
  18. }


FAQ

How can I use multiple buttons?

Button is a class. Therefore to Button multiple digital pins, you must create an instance for each of them. In the example above, a Button instance (called button) for pin 12 is created on line 6. If you create several buttons, give each a better name then button, use something that says what it does:

Button lightOn = Button(12,PULLUP);

To Button an additional pin (pin 4 for example), you could create the following instance called nextStep:

Button nextStep = Button(4,PULLUP);

And now it's just to call the isPressed() on the button you are interested in getting the state of:

//check buttones
  if (lightOn.isPressed()){ someFunction(); }
  if (nextStep.isPressed()) { someOtherFunction(); }


Information about this page

Part of AlphaBeta Libraries.
Last Modified: November 30, 2018, at 01:26 AM
By: pert