I was adding code to a photon 2 project and noticed it did not like 2 or more Mode button presses. One press would work ok. Once in a while 2 clicks might work without disconnecting from cloud. Also, when the Mode button is pressed once the Cyan LED changes to dim Green then flashes Green 4 or 5 times then returns to breathing Cyan. For multiple clicks, the LED goes dim white then dark. Cloud is disconnected and I need to reboot device.
This code below works ok for Argon but does not for Photon 2 when multiple button presses occur. Is there something I am doing wrong? Thanks for your help.
// Device Argon & Photon 2
// Device OS 5.4.0
#include "Particle.h"
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
SerialLogHandler logHandler;
bool buttonPressed = false;
int buttonClicks = 0;
char eventData[64];
void buttonHandler(system_event_t event, int data) {
buttonPressed = true;
buttonClicks = system_button_clicks(data);
}
void setup()
{
System.on(button_final_click, buttonHandler);// https://docs.particle.io/reference/device-os/firmware/#system-events
Particle.connect();//comment out this line for testing, too
}
void loop()
{
if (buttonPressed) {
buttonPressed = false;
Log.info("button pressed %d time(s)", buttonClicks);
if (Particle.connected()) {
snprintf(eventData, sizeof(eventData), "number of times: %d", buttonClicks);
Particle.publish("button_pressed", eventData);
}
}
}