cbutton help please.

Folks, I have googled and found nothing.

I am back into programming after several months of having bricks instead of Arduinos.

It would be nice to get this CButton.h thing in the code so buttons can do more than one thing.

I am reading the code supplied but the explanation is beyond me.

Could someone help me with a small code for one button which does multiple things depending on how long it is pressed?

Thanks in advance.

Can you please give a link to CButton.h and post any code that you have written so far ?

Darned if I can find any links now.

(That is why I am so frustrated)

But here are the two files - attached.

I have no code written as I don’t understand any of it.

cbutton.zip (1.74 KB)

Based on scanning through the cpp and h files, and applying a bit of Google-Fu, I found a similar project that has examples and seems to be a bit more general purpose (like being able to change the delay values by changing object properties instead of changing library source…)

Check out http://code.google.com/p/clickbutton/

Looks to me as if you would create a CButton instance with the pin number as a constructor argument, then call checkButton() at regular intervals. That returns an enumerated value which will be one of NO_ACTION, BTN_PUSH, BTN_DBL_PUSH, BTN_HOLD, BTN_HOLD_LONG which indicates what input action has been detected on that pin.

The logic to detect those events seems to be defined by these hard-coded constants, which you'd need to change if you want to alter the behaviour:

    static const int debounce      = 20;      // ms debounce period to prevent flickering when pressing or releasing the button
    static const int DCgap         = 250;     // max ms between clicks for a double click event
    static const int holdTimeShort = 1000;    // ms hold period: how long to wait for press+hold event
    static const int holdTimeLong  = 5000;    // ms long hold period: how long to wait for press+hold event


Yeah, thanks.

Alas I am still a bit foggy about how to make an "instance" and that kinda thing.

That's why I am looking for an example sketch.

Am looking at the google link supplied now.

Got the "example" sketch working -kinda.

I am having trouble making the LED do too much other than a basic on/off of one of the blinks.
I am not understanding how you can LONG DOUBLE CLICK a button as opposed to SHORT DOUBLE CLICK.

It is probably not easy with only wires and not prototyping board to try it with/on.

I am not understanding how you can LONG DOUBLE CLICK a button as opposed to SHORT DOUBLE CLICK.

I suppose it corresponds to some sequence of pressing and releasing the button. If you don't understand what physical actions that represents then presumably you aren't expecting to make use of that in your sketch, in which case you could just ignore the heck out of it. The library seems to me a curious combination of highly general about what types of input sequences it can recognise, and completely inflexible about how each sequence is defined. Having to edit the source code to adjust the behaviour seems bizarre. The library Sembazuru linked to sounds rather more promising, although I haven't investigated it.



Where I am "stuck" with needing to try this library is my "limited" inputs.

With it I can supposedly have one button doing many things depending on HOW it is pressed.

I have made some head way with it. But I shall check the other library you mentioned.


Have you considered using an analogue input and multiple buttons using resistors to generate different voltages when pressed in order to save pins ?

Yes and no.

I am building/working on a project

It is going to be a small hand held line tester for phones.

I want as few components/parts/things in it as possible.

Adding analogue inputs means resistors. As I am not into SMC yet, they will be taking up space.
And… Having more buttons them selves, means more space/area used and more cost.

So I was looking for a way to add multiple functions to one button, so I have less buttons.

For now it is a passing interest. Though I may be able to use it in future projects if I can get it worked out.

OK. How about just 2 buttons ?

Button 1 increments a state variable each time it is pressed.
An LED flashes a number of times to indicate which state it is in.
Button 2 carries out the appropriate action for the current state.

What Arduino are you using ?

An UNO equivalent for now.

If you're going to use any significant number of different button sequences then I think you will find you outgrow that that cbutton library straight away.

If it was me, I'd write a state machine as UKHeliBob suggested to enable my code to recognise different sequences of button presses.

Also, focus from the outset on how you want the user interface to behave. If you understand the behaviour you want then you can implement it, and if you can describe it clearly then we can help you do that if necessary. But only you can decide what that behaviour should be.

(yuck my post just disappeared and I have to type it again.)

Having buttons with multiple functions seems like a good idea.

With fewer buttons it is smaller and I can "group" similar functions to the same button.