AnalogButtons
Arduino multi button support library
AnalogButtons.h
1/*
2 AnalogButtons.h - Multiple buttons on one pin library
3 Copyright (c) 2014 Roberto Lo Giacco.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#ifndef ANALOGBUTTONS_H_
19#define ANALOGBUTTONS_H_
20
21/*
22 AnalogButtons
23
24 In order to reduce the number of pins used by some projects, sketches can use
25 this library to wire multiple buttons to one single analog pin.
26 You can register a call-back function which gets called when a button is
27 pressed or held down for the defined number of seconds.
28 Includes a software key de-bouncing simple algorithm which can be tweaked and
29 is based on the max sampling frequency of 50Hz (one sample every 120ms)
30
31 Minimum hold duration (time that must elapse before a button is considered
32 being held) and hold interval (time that must elapse between each activation
33 of the hold function) can both be configured.
34
35 By default max number of buttons per pin is limited to 8 for memory
36 consumption reasons, but it can be controlled defining the
37 ANALOGBUTTONS_MAX_SIZE macro _before_ including this library.
38
39 This work is largely inspired by the AnalogButtons library available in the
40 Arduino Playground library collection.
41
42 */
43#if defined(ARDUINO) && ARDUINO >= 100
44#include "Arduino.h"
45#else
46#include "WProgram.h"
47#endif
48
49#ifndef ANALOGBUTTONS_MAX_SIZE
50#define ANALOGBUTTONS_MAX_SIZE 8
51#endif
52
53#ifndef ANALOGBUTTONS_SAMPLING_INTERVAL
54#define ANALOGBUTTONS_SAMPLING_INTERVAL 20
55#endif
56
57class Button {
58 friend class AnalogButtons;
59public:
60 Button() {};
70 Button(uint16_t value, void (*clickFunction)(void) = 0, void (*holdFunction)(void) = 0, uint16_t holdDuration = 1000, uint16_t holdInterval = 250);
71
72 // Override this function if you want
73 inline void pressed() {
74 if (clickFunction)
75 (*clickFunction)();
76 }
77
78 // Override this function if you want
79 inline void held() {
80 if (holdFunction)
81 (*holdFunction)();
82 else
83 pressed();
84 }
85private:
86 uint16_t value;
87 uint32_t duration;
88 uint16_t interval;
89 boolean isHeldDown;
90 void (*clickFunction)(void);
91 void (*holdFunction)(void);
92};
93
95private:
96 uint32_t previousMillis;
97 uint16_t debounce;
98 uint32_t time;
99 uint8_t counter;
100 uint8_t margin;
101
102 // AnalogPin
103 uint8_t pin;
104
105 uint8_t buttonsCount;
106 Button buttons[ANALOGBUTTONS_MAX_SIZE];
107
108 // last button pressed
109 Button* lastButtonPressed;
110
111 Button* debounceButton;
112
113public:
122 AnalogButtons(uint8_t pin, uint8_t mode = INPUT, uint16_t debounce = 5, uint8_t margin = 10);
123
127 void add(Button button);
128
132 void check();
133};
134
135#endif // ANALOGBUTTONS_H_
Definition: AnalogButtons.h:94
void check()
Definition: AnalogButtons.cpp:43
AnalogButtons(uint8_t pin, uint8_t mode=INPUT, uint16_t debounce=5, uint8_t margin=10)
Definition: AnalogButtons.cpp:29
void add(Button button)
Definition: AnalogButtons.cpp:37
Definition: AnalogButtons.h:57