# Using clickButton for first time - not working for me

**URL:** https://community.particle.io/t/using-clickbutton-for-first-time-not-working-for-me/66152
**Category:** Firmware
**Created:** [January 14, 2024, 10:54pm UTC](https://community.particle.io/t/using-clickbutton-for-first-time-not-working-for-me/66152 "2024-01-14T22:54:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![LuckGuy](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@LuckGuy](https://community.particle.io/u/LuckGuy)
#### Post date: [January 14, 2024, 10:54pm UTC](https://community.particle.io/t/using-clickbutton-for-first-time-not-working-for-me/66152/1 "2024-01-14T22:54:53Z")

</div>

I'm setting up a button for the first time and using the previous generation Photon. @peekay123 - tagging you for your knowledge of this library

I got the button working with some test code, but it worked inconsistently - and then found the clickButton library. However, after setting up clickButton when I press the button nothing happens - added some log statements but function always equals 0. Tried some things to get working with no luck which brings me here hoping for help...

* * *

Here's the processor board setup:

- one button pin goes to GND
- other button pin to D2

* * *

For the code, I followed the clickButton sample code as much as possible but here are my key code snippets:

```cpp
#include <Particle.h>
#include <clickButton.h>

SYSTEM_THREAD(ENABLED); 
SYSTEM_MODE(SEMI_AUTOMATIC);

const uint8_t BUTTON_STARTSTOP_PIN = D2; // Start/Stop button pin
const uint8_t buttonPin = 2;

ClickButton ButtonStartStop(buttonPin, LOW, CLICKBTN_PULLUP); // the Button
int function = 0; // Button results 

void setup() {

    Particle.connect(); // connect to the cloud
    pinMode(BUTTON_STARTSTOP_PIN, INPUT_PULLUP); 
    //ButtonStartStop.debounceTime = 20; // Debounce timer in ms
    //ButtonStartStop.multiclickTime = 250; // Time limit for multi clicks
    //ButtonStartStop.longClickTime = 1000; // time until "held-down clicks" register
}

void loop() {
    
    Particle.publish("loop before timer check. timer_on= ", String(timer_on));
    
    / *****Verify the button and buzzer are working properly - commented out*****
    while (digitalRead(BUTTON_STARTSTOP_PIN) == LOW) {
        Particle.publish("Button pushed test");
        digitalWrite(WARNING_LED_PIN, HIGH); // sets the LED on
        delay(1000);
        digitalWrite(WARNING_LED_PIN, LOW);
        delay(1000);
    }
    ************* /

    // Update button state - wait for button push
    ButtonStartStop.Update();

    if (ButtonStartStop.clicks != 0) function = ButtonStartStop.clicks;
    
    if (function == 1 || function == -1) {
        Particle.publish("SINGLE click");
        NUM_SECONDS_WARN_LOW=120; // 2 minutes
    }
    if (function == 2 || function == -2){
        Particle.publish("DOUBLE click");
        NUM_SECONDS_WARN_LOW=300; // 5 minutes
    }
    if (function == 3 || function == -3) {
        Particle.publish("TRIPLE click");
        NUM_SECONDS_WARN_LOW=600; // 10 minutes
    }
    if (function == -1) {
        Particle.publish("SINGLE LONG click");
    }
    if (function == -2) {
        Particle.publish("DOUBLE LONG click");
    }
    if (function == -3) {
        Particle.publish("TRIPLE LONG click");
    }

    Particle.publish("function= ", String(function));

    function = 0;
    delay(5);
}

```

---

<div class="post-metadata">

### Author: ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)
#### Post date: [January 15, 2024, 4:37pm UTC](https://community.particle.io/t/using-clickbutton-for-first-time-not-working-for-me/66152/2 "2024-01-15T16:37:11Z")

</div>

@LuckGuy, this is an _old_ library which extends the built-in `millis()` function and is likely not suitable anymore. You may want to use either a "polled" button Arduino library or interrupts to service your buttons. This polled button library by JChristensen with good examples is a good start:

> **[GitHub - JChristensen/JC\_Button: Arduino library to debounce button switches,...](https://github.com/JChristensen/JC_Button)**
>
> Arduino library to debounce button switches, detect presses, releases, and long presses - GitHub - JChristensen/JC\_Button: Arduino library to debounce button switches, detect presses, releases, and...

---

<div class="post-metadata">

### Author: ![LuckGuy](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@LuckGuy](https://community.particle.io/u/LuckGuy)
#### Post date: [January 15, 2024, 7:21pm UTC](https://community.particle.io/t/using-clickbutton-for-first-time-not-working-for-me/66152/3 "2024-01-15T19:21:36Z")

</div>

😮

Ouch, thanks for your help @peekay123! And thanks for the link to another option.

Unfortunately clickButton is one of the libraries that Particle has in it's list when you search for "button".

Hey **Particle Support Team** - if you're reading this can the clickButton library be removed from the list of libraries?

---

<div class="post-metadata">

### Author: ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)
#### Post date: [January 15, 2024, 8:27pm UTC](https://community.particle.io/t/using-clickbutton-for-first-time-not-working-for-me/66152/5 "2024-01-15T20:27:09Z")

</div>

@LuckGuy, actually, I'm the one who published that library a long time ago in the Spark and Spark Core days!

However, I just realized why your code is not working! You basically call `Particle.publish()` every 5ms, quickly exceeding the 4 pubs-per-sec limit and causing the cloud to stop publishing until your stream stops for at least 4 secs. The documentation says:

> **NOTE 1:** Currently, a device can publish at rate of about 1 event/sec, with bursts of up to 4 allowed in 1 second. Back to back burst of 4 messages will take 4 seconds to recover.

You may want to use `Serial.print()` and use the USB with a terminal program (or the Particle CLI) to view your messages instead.

---

<div class="post-metadata">

### Author: ![LuckGuy](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@LuckGuy](https://community.particle.io/u/LuckGuy)
#### Post date: [January 17, 2024, 2:59pm UTC](https://community.particle.io/t/using-clickbutton-for-first-time-not-working-for-me/66152/6 "2024-01-17T14:59:49Z")

</div>

Thanks @peekay123 - Once I took out all blocking statements including Particle.publish and delay() the clickButton works great!

Now it's learning how to delay without blocking - and looks like there's a whole separate set of libraries for that

---

<div class="post-metadata">

### Author: ![gusgonnet](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/gusgonnet/32/27491_2.png) [@gusgonnet](https://community.particle.io/u/gusgonnet)
#### Post date: [January 17, 2024, 3:10pm UTC](https://community.particle.io/t/using-clickbutton-for-first-time-not-working-for-me/66152/7 "2024-01-17T15:10:42Z")

</div>

Hi!

here's a snippet I use a lot, modify 10000 (msec) to fit your needs.  
Call executeAfterDelay() from your loop() function, or just embbed the contents in loop().

```auto
void executeAfterDelay()
{
    static unsigned long logTime = 0;
    if (millis() - logTime > 10000)
    {
        logTime = millis();
        Log.info("Executing after delay");
        // Do something here
    }
}

```

Add this at the top of your FW so the logger works properly, right after the imports:

```auto
SerialLogHandler logHandler(LOG_LEVEL_INFO);

```

PS: to execute different things after different delays or events you can use a Finite State Machine

> **[Finite State Machines | Firmware](https://docs.particle.io/firmware/software-design/finite-state-machines/)**
>
> Documentation for Particle, a platform for connected devices.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/4/64635adccc35238caa19e9b553ec3e98b163d026.png) [@system](https://community.particle.io/u/system)
#### Post date: [July 18, 2024, 3:11am UTC](https://community.particle.io/t/using-clickbutton-for-first-time-not-working-for-me/66152/8 "2024-07-18T03:11:37Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
