Go Down

Topic: Yet another button library (Read 1 time) previous topic - next topic

Hutkikz

Jan 29, 2018, 03:29 am Last Edit: Feb 04, 2018, 03:04 pm by Hutkikz
 I'm an old button masher and I found that many of the button libraries did not react quickly or reliably enough for me. The ones that did utilized hysteresis but had other features or issues I didn't care for.

This simply returns a 1 every time the button becomes pressed. It reads the button at a set interval and uses a ring buffer to record the last 8 reads giving a maximum of 7 intervals. the debounce time for both press and release is determined by the number of intervals desired for each.

The default constructor is set for a 10ms interval and uses 1 interval(10ms) for the press debounce and 5 intervals(50ms) for the release debounce. This seems to work reliably for as fast as I can press a button.

There is also an overloaded constructor where you can set all 3 of these parameters. This is demonstrated in the example sketch.

Feel free to criticize any aspect as this is mostly just a learning exercise.  

EDIT:  Library has been updated to include @pert's and @TonyWilk's suggestion's and also eliminated the need to pass  millis() into the update function

pert

I recommend you to put your example sketch in a folder of the same name under an examples subfolder:

SimpleButton
|_SimpleButton.h
|_SimpleButton.cpp
|_examples
  |_SimpleButton_Example
     |_SimpleButton_Example.ino

This will allow the example sketch to be accessed via the File > Examples > SimpleButton > SimpleButton_Example menu in the Arduino IDE after installing the library.

Hutkikz

#2
Jan 29, 2018, 03:50 am Last Edit: Jan 30, 2018, 12:42 am by Hutkikz
Thank you very much! I would  love to hear any other thoughts you may have about it.

TonyWilk

Hi,
Had a look at your 'SimpleButton':

Shifting the input state into buttonBuffer, yep that's a good way of doing it.

Testing a number of bits to determine the button state, ok.

However...

  if ((_buttonBuffer & _numIntervalsPress) == _numIntervalsPress)

to test the bits in _buttonBuffer e.g. binary 0011111 after 5 reads of 'button is pressed'
you need _numIntervalsPress to be binary 0011111

But, from the constructor:
   _numIntervalsPress = (pressIntervals * 2) + 1;

so if you pass pressIntervals = 5, _numIntervalsPress = binary 00001011

Did you test it with all values of 'pressIntervals' from 1 to 7 ?

It looks like it won't work at all with pressInterval values of 2, 4, 5 or 6

Yours,
  TonyWilk


Hutkikz

#4
Jan 30, 2018, 12:22 am Last Edit: Jan 30, 2018, 12:42 am by Hutkikz
 Ouch you got me I didn't test that last minute addition. I have changed it to use a lookup table now.
Thanks for taking the time to check.

Hutkikz

#5
Jan 30, 2018, 12:41 am Last Edit: Jan 30, 2018, 02:06 am by Hutkikz
Ugh more issues. Think I got it now... I hope

Also figured out how to remove all the bad versions so only the corrected version can be downloaded. Both here and in the original post

TonyWilk

You don't need a look-up table to get a bitmask...

With 'press' having a value from 0 thru 7, mask =  (1 << (press+1)) -1

for example, press= 2.
press+1 = 3
1 << 3  = 8
8-1     = 7


Yours,
  TonyWilk


pert

I recommend you to sign up for a free GitHub account and host your library there. There is a little bit of a learning curve to getting started with GitHub and Git but they make it really easy to do the basic stuff. GitHub is a great way to share your work with the community and also to collaborate on projects. It will also ease you into using Git for version control, which is an incredibly valuable tool.

Hutkikz

 @TonyWilk

I had seen that before but didn't understand what the -1 did. Now I get it and have incorporated it into the library.

@pert
 Yeah, I think your right. Editing this has been a nightmare but I think I got it for now. headed to GitHub next.

Much thanks to both of you. (and karma)

Go Up