Vous êtes sur la page 1sur 4

technology workshop living food play outside

Arduino - Dual Function Button - Long Press/Short Press (Without Delay)


by xn1ch1 on April 12, 2015

Table of Contents

Arduino - Dual Function Button - Long Press/Short Press (Without Delay) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Intro: Arduino - Dual Function Button - Long Press/Short Press (Without Delay) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 1: The Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 2: The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 3: The Software In Detail Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Step 4: The Software in Detail Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Step 5: The Full Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

http://www.instructables.com/id/Arduino-Dual-Function-Button-Long-PressShort-Press/
Intro: Arduino - Dual Function Button - Long Press/Short Press (Without Delay)
Using software we can take a single button and have it toggle a light on or off, or complete a more complicated function. But what if we have two functions and still only
one button. Many of us do this on smartphones everyday, this is called a short press or a long press (press and hold).

The following guide will take you through all the steps needed to create a simple push button that can control the state of two separate lights.

Step 1: The Theory


First some theory.

Using a button to turn on a light is simple, if the button is reading HIGH (pressed), then we write the light HIGH (on). Once the button is reading low, we can then write the
light low and turn it off again. This is what we call momentary.

For this set-up though, we are looking to toggle the light on or off; this is a problem for the simple example above as the loop function in Arduino repeats hundreds of
times per second. Even the quickest press could toggle on and off many many times. This is the first problem we will overcome using a simple boolean or two.

The second problem is the long press function, how do we trigger this function without triggering the short press function first? The answer is simple. The long press
function is triggered whist the button is being pressed, the short press function is triggered once the button is let go. This can again be observed on a smart phone by
letting go of an object on screen just before the long press function kicks in.

In the next step we will be creating our hardware set-up, feel free to skip this if you are already at this stage...

Step 2: The Hardware


The hardware is very straight forward so I wont bore you too much. WE have two LED's wired separately from pins 12 and 13 to ground via 220ohm resistors. Then from
5v to the button and on the same side a 10k resistor to ground, the other side of the switch to pin 3.

I told you it was simple.

Step 3: The Software In Detail Part 1


Now to detail each step of the code. If you want to go straight to the full code, you'll find that in the step 5

Let's look at the variables needed for this set-up, starting with the straight forward pin references.
int LED1 = 12;
int LED2 = 13;
int button = 3;

Next we need a few booleans. These will be used to flip the state of the LED's on or off
boolean LED1State = false;
boolean LED2State = false;

And another two booleans. The first is changed dependant on the state of the button; this will allow the code to detect when the state of the button changes for the first
time on each loop. The second will serve two functions, it will allow the code to stop the long press from firing more than once, and will stop the short press function being
fired when we finally release the button after a long press.
http://www.instructables.com/id/Arduino-Dual-Function-Button-Long-PressShort-Press/
boolean buttonActive = false;
boolean longPressActive = false;

Finally two last variables. The first is a variables used to record the time the button is first pressed, the second variable is the length of time (in milliseconds) you wish to
have the button held down for to activate the long press function. This can be changed as you wish.
long buttonTimer = 0;
long longPressTime = 1500;

Now for the set-up, this is again very straight forward setting the pin modes.
void setup() {

pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(button, INPUT);

Step 4: The Software in Detail Part 2


Now for the loop, I'm going to break this down bit by bit to explain what's going on. The first thing we will do is read the state of the button which will give us this code and
and allow us to create functions for both un-pressed and pressed states (remember that the short press function happens upon button release)
if (digitalRead(button) == HIGH) {

//Button pressed

} else {

//Button not pressed

In the button pressed section the first thing we will do is test the buttonActive variable (upon set-up this is false). If this is false, this means the program is detecting the
button being pressed for the first time, because after reading false we immediately set it to true, and only releasing the button can set it back to false. We also record the
time the button was first pressed.
if (buttonActive == false) {

buttonActive = true;
buttonTimer = millis();

The next step within the button pressed section is to test how long we have held the button for, we do this by testing the current time, less the time pressed in
milliseconds and compare that to the length of time we wish for the l long press to activate. We also check that the longPressActive boolean is false, because once we
have activated the long press function we will set this boolean to true to stop repeat activation. Once we have held the button for the desired time, we will flip the state of
the LED1State boolean which will in turn flip the pin high or low accordingly.
if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {

longPressActive = true;
LED1State != LED1State;
digitalWrite(LED1, LED1State);

Moving on to the button not pressed section of the code. The first thing we will check is if the buttonActive boolean is true; if it is the code is looping for the first time since
the button was let go, and we can set the boolean back to false to allow the code above to detect the first loop after the button was pressed.
if (buttonActive == true) {

buttonActive = false;

Within this (as we are detecting the first loop after button release), we are next going to check if long press was activated, which will give up two possible steps. If long
press was activated we will simply set the longPressActive back to false to allow the program to long press once again. If it was not active (button wasn't held long
enough), we will flip the state of the second LED like we did with the first.
if (longPressActive == true) {

longPressActive = false;

} else {

LED2State != LED2State;
digitalWrite(LED2, LED2State);

Step 5: The Full Code


int LED1 = 12;
int LED2 = 13;
int button = 13;

boolean LED1State = false;


boolean LED2State = false;

long buttonTimer = 0;
long longPressTime = 1500;

boolean buttonActive = false;


boolean longPressActive = false;

void setup() {

http://www.instructables.com/id/Arduino-Dual-Function-Button-Long-PressShort-Press/
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(button, INPUT);

void loop() {

if (digitalRead(button) == HIGH) {

if (buttonActive == false) {

buttonActive = true;
buttonTimer = millis();

if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {

longPressActive = true;
LED1State = !LED1State;
digitalWrite(LED1, LED1State);

} else {

if (buttonActive == true) {

if (longPressActive == true) {

longPressActive = false;

} else {

LED2State = !LED2State;
digitalWrite(LED2, LED2State);

buttonActive = false;

Related Instructables

Dual-Motor
Arduino Button SIP&PUFF
Computer Quiz-O-Tron
Tutorial by laxap Controlled Arduino controlled RC 3000: Arduino
Kayak System Self-balancing
Modules - car with two quiz contestant
skateboard/segw*yL298N Dual H-
by kayakdiver project Arduino Arduinos by lockout system
Bridge Motor
Shield by malnas01 by RoysterBot
Controller by
XenonJohn Reichenstein7

Advertisements

Comments

http://www.instructables.com/id/Arduino-Dual-Function-Button-Long-PressShort-Press/