This is a simple library that handles real buttons, button-like digital switches (photo gates, Hall Effect switches, etc), as well as virtual buttons such as auto buttons and null buttons. If you want to be able to sense a whole bunch of input devices, such as buttons, keypads, rotary encoders, joysticks, etc, use the phi_interfaces library.
An auto button gets automatically pressed at a constant rate. It saves you an I/O pin when selecting from a short list or entering number. You just set the up button as an auto button so a list will cycle through all its items and all you need is one real confirm button.
A null button never gets pressed. If all you need is say 2 buttons, define the rest as null makes your buttons structure compatible with the rest of your/my codes. You don’t have to go into your code and delete the other buttons that you don’t use.
Any button has repeat when you hold on to it for some time. The button will be automatically pressed repeatedly as long as you hold it down. After repeating for 10 times, it enters a dash mode where the repeat is more rapid. This helps covering larger range of changes.
Lots of button status are defined.
To create phi_buttons objects, do the following:
phi_buttons myButton(btn_pin, Polarity);
Now myButton is a button that is attached to arduino digital pin btn_pin, its polarity is either LOW or HIGH. If you use LOW polarity, then the button is depressed only when the digital pin changes to LOW. If you use HIGH polarity, then the button is depressed only when the digital pin changes to HIGH.
The phi_buttons constructor automatically sets the internal pull-up resistor on the corresponding pin so all you have to do is to connect one side of the button to GND, and the other side the the Arduino pin, and use LOW polarity. If you prefer the opposite polarity, you need a resistor to pull down the Arduino pin. Read this tutorial for HIGH polarity buttons: http://www.arduino.cc/en/Tutorial/button
To sense a button do the following:
Then you can react to the sensed button status according to the returned button_status:
buttons_up button is not pressed
buttons_pressed button is pressed (not in use)
buttons_down button is down
buttons_held button is held
buttons_released button is released (use this to trigger actions when button is released)
Example 01: To sense a button release:
Make sure that your button is connected between digital pin 5 and ground. You can change the pin in the define command to any other pin.
#define btn 5
phi_buttons btn_1(btn, LOW);
if (temp==buttons_released) Serial.println(“Button pressed, time to do something”);
Example 02: To sense all 6 buttons on Phi-2 shield:
#define btn_u 5
#define btn_d 10
#define btn_l 11
#define btn_r 4
#define btn_b 14
#define btn_a 15
phi_buttons btn_1(btn_u, LOW);
phi_buttons btn_2(btn_d, LOW);
phi_buttons btn_3(btn_l, LOW);
phi_buttons btn_4(btn_r, LOW);
phi_buttons btn_5(btn_b, LOW);
phi_buttons btn_6(btn_a, LOW);
if (temp!=0) Serial.println(temp);
byte temp1, temp2, temp3, temp4, temp5, temp6;
} while (1);
The current version is 327, released on 3/27/2011. Please find the library hosted on Google code under downloads:
First download the package, then extract the files under your Arduino Sketchbook/Libraries folder, so that the three files will be under c:/Arduino22/Sketchbooks/Libraries/phi_buttons/ (Just example, your directory may be different). Make sure you don’t have c:/Arduino22/Sketchbooks/Libraries/phi_buttons/phi_buttons/
Then close all Arduino IDE windows.
Reopen Arduino IDE, open a sketchbook you want to use the phi_buttons. Then choose under the Arduino IDE meny Sketch->Import library->phi_buttons
The Arduino IDE automatically creates a line in the beginning of your sketchbook: #include <phi_buttons.h>
OK you’re done!
This library is still supported but its future is unclear. I have constructed a phi_interfaces library to sense a whole bunch of different input devices and replaced phi_buttons with phi_interfaces in all my libraries that use input devices. I may absorb phi_buttons in a future release of phi_interfaces.