clickButton library


#19

Hello,

Please if any help me completing my project i possibly want this.
There should be a button to change the device status (only one button please).
If the device is already off, pressing this button should bring the device in running mode.
If the device is on, pressing this button for 5 seconds should bring the device in configuration mode
If the device is in configuration mode, pressing the button for 5 seconds should restart the device, and pressing the button 3 times should set the variables to default values.
If the device is on, pressing the button for 10 seconds turn of the device
now please tell me how can i do that i weak in programming and i seriously want to do this.
thanks in advance for helping


#20

What have you tried so far?


#21

Hello, I want to utilize the “DOUBLE LONG Click” for a button to control some LEDs. I want the LEDs to Loop until the button is released. This means the button will be double clicked and held down; the LEDs will Loop; then when the button is released the LEDs will stop Looping.

if(function == -2) Serial.println(“DOUBLE LONG click”);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
delay(delayTime);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
delay(delayTime);


#22

When i varify this code clickButton.ccp i get that im missing a type

clickButton.cpp:3:1: error: ‘ClickButton’ does not name a type
ClickButton::ClickButton(uint8_t buttonPin)

clickButton.cpp:20:1: error: ‘ClickButton’ does not name a type
ClickButton::ClickButton(uint8_t buttonPin, boolean activeType)

clickButton.cpp:36:1: error: ‘ClickButton’ does not name a type
ClickButton::ClickButton(uint8_t buttonPin, boolean activeType, boolean internalPullup)

I have not changed anything in the main clickButton.ccp.
What type do i need to put here?

Br
Dimi


#23

What IDE are you using?
How have you imported the library?
What device are you targeting?

When I build the “ClickButtonDemo.cpp” in Particle Build it builds just fine for Core/Photon/P1/Electron (all latest firmware version).

clickButton.cpp is the library file, not project main file.


#24

Hello, sorry should have included that information.

Im using the Particle webide and a Photon.
I created a new clickButton.h file and copy pasted the .h code in to it,
then i also copy pasted the .cpp code in to the clickButton.cpp file.

I guess i need to make use of the #include command?
When i hit varify code i get that message.

I tried to include the clickButton.h to the .cpp file then i get other errors :frowning:

In file included from clickButton.cpp:2:0:
clickButton.h:8:25: error: expected ')' before 'buttonPin'
ClickButton(uint8_t buttonPin);

Note that im under process of learning so im not an advanced user :slight_smile:

Many thanx
Dimi


#25

Why do you not import the library via the import button?
Do you need to change the library’s behaviour? If not, use the intended workflow.

If you have a look at the demo, you’ll see how to use the lib.

Usually when you add a set of .h/.cpp files via the (+) button top right in Build, you’ll get a corresponding #include line inside your main .ino file.


#26

Thank you very much. That did the trick.
I never used that function so dident think about it.

Thank you.

/Dimi


#27

Some other thing came up with this library.

I am using CoolTerm to check for my serial.print.
I have notice that when i press the button sometimes my prints comes 1 and a half time, and sometimes just some of my print comes.
Here is how it looks when i pushed 5 times.

Reset button was pressed, all values are set to default
Reset button was pressed, all values are set to default
Reset button was pressed, all values are set to default
Reset button was pressed, all values are set to default
Reset button was pressed, all values are set to default
Reset b

As you see the last line should not be there.
Im not sure how to make sure this do not happen?
Is it the delay i need to change or the debounce timer, i have tried many different values but in vain.
Not sure how to continue troubleshoot, can anyone help? Here is my code.

    // This #include statement was automatically added by the Particle IDE.
#include "clickButton/clickButton.h"


//---- Declare variables -----------------------------------------------
const int pinResetButton = D1; //the pin where we connect the reset button
const int pinGreenLed = D6; //the pin where we connect the Green led
const int pinYellowLed = D7;  //the pin where we connect the Yellow led
//------------------------------------------------------------------------
//---- Here i declare the state check for the inputs --------------------
ClickButton stateResetButton(pinResetButton, HIGH);
//-----------------------------------------------------------------------
//---- Here we declare the counter for the inputs ----
int resetFunction = 0;
//---- Herer i declare the function i will use ---------------------------
void PressResetButton();
//-----------------------------------------------------------------------

void setup() {
//---- We declare respective digital point as outputs and inputs here ----
  pinMode(pinResetButton, INPUT_PULLDOWN); // Its an input since it waits for a button to be pressed 
  pinMode(pinGreenLed, OUTPUT); // Its an output since it need to send a voltage Green led
  pinMode(pinYellowLed, OUTPUT); // Its an output since it need to send a voltage to Yellow led
//----------------------------------------------------------------------
//---- We declare the state of each variable when the units gets powered on ----
  digitalWrite(pinGreenLed, HIGH); // The GreenLed should always be on if water is good, if water is going bad This one will be LOW
//----------------------------------------------------------------------  
//---- For troubleshooting ---------------------------------------------
  Serial.begin(9600); // Open a serial port
//----------------------------------------------------------------------
//---- Here is some criterias for the inputs ----
  // Setup button timers (all in milliseconds / ms)
  // (These are default if not set, but changeable for convenience)
  stateResetButton.debounceTime   = 10;   // Debounce timer in ms
  stateResetButton.multiclickTime = 250;  // Time limit for multi clicks
  stateResetButton.longClickTime  = 1000; // time until "held-down clicks" register
//----------------------------------------------------------------------
}

void loop() {
//---- Here we update the status of all Input pin -----
  stateResetButton.Update();
//----------------------------------------------------  
//---- Here we tell the controller what to do if the status of the inputs meet certain criterias ----
  if(stateResetButton.clicks != 0) resetFunction = stateResetButton.clicks;
  if(stateResetButton.clicks == 1) PressResetButton();
  if(resetFunction == 2) PressResetButton();
  if(resetFunction == 3) PressResetButton();
  if(resetFunction == -1) PressResetButton();
  if(resetFunction == -2) PressResetButton();
  if(resetFunction == -3) PressResetButton();
//-----------------------------------------------------
//---- Reset function as this one controlls how many clicks we used ----
  resetFunction = 0;
  delay(5);
//----------------------------------------------------------------------  
}
void PressResetButton(){ // This function is called when reset button is pressed
  digitalWrite(pinGreenLed, HIGH); // Turn on green led
  digitalWrite(pinYellowLed, LOW); // Turn off yellow led
  Serial.println("Reset button was pressed, all values are set to default");
}

I noticed that the LED turns on just fine, its only the text that gets updated wierd in CoolTerm.
I cant say if this is that the Photon is sending the text faulty or if coolterm recive it wierd.

Many thanx
Dimi


#28

I haven’t looked at your code yet, but the output suggests a possible reason for the broken line.
One full line plus the partial next line look as if you are hitting the 64byte limit of the output buffer.

But the underlying problem is most likely that you are printing too quickly (e.g. debounce or delay might help).


#29

Thank you for the answere.

I dont think its delay or debounce, i have tried with so many different values now and always same result.
I noticed one thing.

When i flash decive and first time i press the button, nothing happens.
second time i press the button it sends the serial.print 2 times.
I guess it has something to do with that the variable “function” is empty the first time?

But it do seems that this cause issues with upcoming press on the button.

This is what is seen when i pressed 2 times after a flash on the photon.

All values are set to default
All v

When i click a third time i see this

All values are set to default
All values are set to default // it populated the rest of the sentence on third press? 

Fourth press

All values are set to default
All values are set to default
All values are set to default
All values are set to d

It makes sense that it might hit the 64 bit limit but how come it dont write all the text always, why does it save some text for the other press of button. its the same eventhou i put a delay in the end.

Many thanx for your patience with me :slight_smile:
/Dimi


#30

Not that it would account for your issue, but I’d avoid all these ifs and do it this way

  switch(stateResetButton.clicks)
  {
    case 0:
      // do nothing for 0
      break;
    //case x:
    //  do whatever for x
    //  break;  
    default:  // all non explicitly dealt cases
      PressResetButton();
      break;
  }

If you don’t need it for anything else you can drop resetFunction


#31

Thank you so much, that makes more sense :slight_smile:

The problem i have beats me thou but it seems it only affect the serial message so i dont really need it
for anything, was for troubleshooting.
Im just worried it will be the same for the actual message that i will send to the cloud but im not that far in the code yet.

meanwhile thank you so much for all the help.

BR
Dimi


#32

You could also forget pinMode(pinResetButton, INPUT_PULLDOWN) if you use the three parameter constructor ClickButton stateResetButton(pinResetButton, HIGH, true) which attaches the pull-down for you.


#33

Thanks for this peekay123.

I am trying to use this in one of my project which uses several buttons. How would I go about that? A comment in the code under “History” says: “Added another example code for multiple buttons in an object array” but I am not able to find such an example. I gave it a try anyway in my sketch as outlined below, but it only seems to work for Button1 but not for Button2 and Button 3.

In my sketch I just defined several instances like this:

ClickButton Button1(Button1Pin, LOW, CLICKBTN_PULLUP);
ClickButton Button2(Button2Pin, LOW, CLICKBTN_PULLUP);
ClickButton Button3(Button3Pin, LOW, CLICKBTN_PULLUP);

Then in the loop i do

Button1.Update();
Button2.Update();
Button3.Update();
if(Button1.clicks == 1) {
  Particle.publish("xxx", "yyy", PRIVATE);
    ...;
    ...;
}
if(Button2.clicks == 1) {
  Particle.publish("xxx", "yyy", PRIVATE);
    ...;
    ...;
}

and so forth for the third one. However, it seems only Button1 works and the others don’t. Am I using the library in a wrong way?


#34

@hbierau, try re-ordering this way:

Button1.Update();
if(Button1.clicks == 1) {
  Particle.publish("xxx", "yyy", PRIVATE);
    ...;
    ...;
}
Button2.Update();
if(Button2.clicks == 1) {
  Particle.publish("xxx", "yyy", PRIVATE);
    ...;
    ...;
}
Button3.Update();

#35

@peekay123: Thanks, this way it works.I thought that the …clicks variable would exist for each of the instances created. And if not, should then …clicks not contain the respective value of the last instance refreshed by …Update (in my case it was for the one that was first called with Update)?
Anyway, thx for the quick reply.


#36

Hi everyone,

This library is great and really usefull.
I needed something else that wasnt included so i added myself: report when the button is released after a long click.
Now it works like this for me:

ClickButton(uint8_t buttonPin);
ClickButton(uint8_t buttonPin, boolean reportRelease);
ClickButton(uint8_t buttonPin, boolean reportRelease, boolean active);
ClickButton(uint8_t buttonPin, boolean reportRelease, boolean active, boolean internalPullup);

if i set “reportRelease” as false, then it works as it was before. If i set it as true, then it will give me:
-2 after a long double click and 102 once the button is released. The same for any other amount of clicks.
100 + “amount of clicks” after released.

My questions is:
What should i do with this to share it? Create a new library? Update the clickbutton library?


#37

@alexgrauer, cool stuff! I would say add the the new functions to the existing library, keeping the existing ones as-is for compatibility. Then, do a pull request against my github repo so I can update the code with your request. I’ll then publish it back to the IDE.

I did not create clickButton library and it is a unknown gem IMO. I’m glad folks are using and augmenting it. I look forward to seeing your pull request!


#38

@peekay. Did it!!! :slight_smile: … and i changed the code a bit to keep the compatibility.
Sorry im new to all this so im not sure i did the pull request correctly. Let me know please.