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:
#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);
}