Go Down

Topic: Questions about Smart Arduino library for debouncing switches and buttons (Read 531 times) previous topic - next topic

avandalen

Questions about the switch library can be asked in this topic.
See the article here:
Smart Arduino library for debouncing switches and buttons:

Coding Badly


avandalen

Do you have a Project Guidance question?

No, it's meant to collect here all the questions about the swich library.

azeo


swenodor

Nice work, Thank you!
My question is how to modify the .cpp, in order to add a Single-Click feature - this could be very, very useful!

The problem is that the singleClick needs to return a TRUE only when there is a Click && doubleClickPeriod is exired.

I thought it would be quite easy, but since I am new to the Arduino I didn't get it to work.
Cheers!




avandalen

Nice work, Thank you!
My question is how to modify the .cpp, in order to add a Single-Click feature - this could be very, very useful!
Use pushed() for the Single-Click feature.

swenodor

Use pushed() for the Single-Click feature.
The thing is that pushed() is good when you want to get just the Single-Click. But if you want your button to do one thing on Single-Click, and something different on a Double-Click - that's not an option - Double-Click will be made up from two Single-Clicks, and this would mess things up.

Of course, you can always fix this in the void loop() with some code, but having a singleClick() function is very handy.

Fortunately, I managed to get things working now - and given my coding skills, this should be considered a miracle :)

Here's the code:
(you need to modify the Switch.cpp & Switch.h with the lines commented with '// ADDED' or '// REPLACE' below, most of the code is omitted)
Code: [Select]

/* --------------------------------------------Switch.cpp------------------------------------------------ */
bool Switch::process()
{ singleClickFlag(); // ADDED these two functions
  calcSingleClick(); // ADDED these two functions
 }
void inline Switch::calcDoubleClick()
{ _doubleClick = false;
  if(_switched && !(debounced^polarity))
  { _doubleClick = (ms - pushedTime) < doubleClickPeriod; // pushedTime of previous push
    dcFlag = (ms - pushedTime) < doubleClickPeriod; // ADDED flag for double-click
pushedTime = ms;
scFlag = false; // ADDED flag for single-click, nullify on push
lpFlag = false; // ADDED flag for long-press, nullify on push
  }
}
void inline Switch::calcLongPress()
{ _longPress = false;
  if(!longPressDisable)
  { _longPress = on() && ((ms - pushedTime) > longPressPeriod); // true just one time between polls
longPressDisable = _longPress; // will be reset at next switch
  }
  else lpFlag=true; // ADDED condition to flag a long-press
}
//============================================================ ADDED the two functions below
void inline Switch::singleClickFlag()
{
  if(_switched && (debounced^polarity)) scFlag=true; //on release, flag for singleClick action
}
 
void inline Switch::calcSingleClick()
{ _singleClick = false;
  if(scFlag && !dcFlag && !lpFlag) // conditions for singleClick
  {
_singleClick = (ms - pushedTime) >= doubleClickPeriod; //produce a single click once the doubleClickPeriod is expired
scFlag=!_singleClick; //break to exit
  }
}
bool Switch::singleClick()
{ return _singleClick;
}
/* ------------------------------------------end of Switch.cpp------------------------------------------------ */
/* ------------------------------------------begining of Switch.h--------------------------------------------- */
public:
  bool singleClick(); // ADDED singleClick definition
 protected:
  void inline calcSingleClick(); // ADDED
  void inline singleClickFlag(); // ADDED
//============================================================REPLACE the line below

  bool input, lastInput, equal, deglitched, debounced, _switched, _longPress, longPressDisable, _doubleClick, _singleClick, dcFlag, lpFlag, scFlag;
//============================================================that's it
/* ------------------------------------------end of Switch.h--------------------------------------------- */

avandalen

Note that a doubleClick() always will be preceded by pushed() from the first push. This can't be avoided, because then the pushed() function would have to wait on a possible second click, which would introduce an annoying delay. So, the action on doubleClick() has to undone the previous action on pushed().

blackketter

Hi,
I really like the Switch library, thanks for creating it!
Will you be publishing it on GitHub?  If not, I'd be happy to do it.
Thanks!
-dean

avandalen

Hi,
I really like the Switch library, thanks for creating it!
Will you be publishing it on GitHub?  If not, I'd be happy to do it.
Thanks!
-dean

Yes, if you can do that for me!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy