I echo everyone’s comments – many thanks @peekay123 for sharing this library. I was trying to write my own debounce functions, and this made my life way easier! The fact I can now handle different types of clicks will also be extremely useful in the future…
Since my project asks for debouncing multiple buttons, I combined this ClickButton class with Vectors for instantiating multiple ClickButton objects in a single array. Works great and makes for very elegant code. See below for the modified clickButtonDemo.cpp as an example:
#include <clickButton.h>
#include <vector>
#define INPUT_BTN_NUM 4
// the Buttons
const int buttonPin[INPUT_BTN_NUM] = {D0,D1,D2,D3};
std::vector <ClickButton> button;
// Button results
int function[4] = {0,0,0,0};
void setup()
{
Serial.begin(9600);
for (int i = 0; i < INPUT_BTN_NUM; i++) {
// Instantiate the ClickButton object
button.push_back(ClickButton(buttonPin[i], LOW, CLICKBTN_PULLUP));
pinMode(buttonPin[i], INPUT_PULLUP);
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
button[i].debounceTime = 20; // Debounce timer in ms
button[i].multiclickTime = 250; // Time limit for multi clicks
button[i].longClickTime = 1000; // time until "held-down clicks" register
}
}
void loop()
{
for (int i = 1; i < INPUT_BTN_NUM; i++) {
// Update button state
button[i].Update();
// Save click codes in function, as click codes are reset at next Update()
if(button[i].clicks != 0) function[i] = button[i].clicks;
if(function[i] == 1) Serial.printlnf("Button %u: SINGLE click",i);
if(function[i] == 2) Serial.printlnf("Button %u: DOUBLE click",i);
if(function[i] == 3) Serial.printlnf("Button %u: TRIPLE click",i);
if(function[i] == -1) Serial.printlnf("Button %u: SINGLE LONG click",i);
if(function[i] == -2) Serial.printlnf("Button %u: DOUBLE LONG click",i);
if(function[i] == -3) Serial.printlnf("Button %u: TRIPLE LONG click",i);
function[i] = 0;
}
delay(5);
}
@elosier, nicely done and thanks for sharing. I just want to make sure the creds go to raronzen@gmail.com for creating this library. I simply made it available on the Particle IDE.
Hey @peekay123 thank you very much for porting this library, I really like it.
I tried to use it together with the Adafruit_MCP23017 Library were my buttons are connected to but unfortunately it didn’t work and my photon responded with a red error blinking. I think it was “usage fault”.
I tried to modify the library like that:
#include "Adafruit_MCP23017.h"
...
void ClickButton::Update(Adafruit_MCP23017* mcp)
{
long now = (long)millis(); // get current time
_btnState = mcp->digitalRead(_pin); // current appearant button state
...
}
I have also changed the definition in the corresponding .h file.
Does anyone know how to get this library to compile locally? I can do it if all the code is in one file, but if I split it into a .h, a .cpp and a .ino, even when using includes, I get a load of errors. Should I also be including other files? “not declared in this scope” & “does not name a type” etc
(I have a good reason for needing to compile locally.)
@netpex, I put the clickButton .cpp and .h files along with the example .cpp file in a single folder and it compiled just fine. What errors are you getting?
We usually prefer the more elaborate/explicit naming convention, but since D4 is defined as 4 it doesn’t make any difference really.
I guess the reason for peekay123 having that in his code snippet is that he had taken that code from another source as is.