I developed a simple recipe where I press a button and my beloved photon blinks a bright LED and buzzes a few tones with a piezo to alert the wife that I will soon be approaching with an empty stomach.
Thing kinda went haywire a little bit today. Our toddler was in a rare nap on the couch when it went nuts and constantly buzzed and flashed. What is going on? Is it my kindy-garden coding? Is it a poor power source to the photon? Gremlins?
You guys are the experts, you are supposed to know this stuff instinctively. oy.
//The pin you want to control, in this case I'm blinking the on-board blue LED on digital pin 7
int led1 = D5;
int led2 = D0;
//This function is called after the Photon has started
void setup(){
//We set the pin mode to output
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
//We "Subscribe" to our IFTTT event called button so that we get events for it
Particle.subscribe("button", myHandler);
}
//The program loop, not sure if this has to be here for the program to run or not
void loop() {
}
//The function that handles the event from IFTTT
void myHandler(const char *event, const char *data){
// We'll turn the LED on
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(1000);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(100);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(100);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(100);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(100);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
//I've commented all the lines, so hopefully you understand what everything does, but if not feel free to ask questions in the comments and I'll do my best to answer them.
I’ve stuck largely to the script, but did some copypasta to get some blinks at the end, so that’s the likely source of the issue.
Not the reason for your problem, but try to keep Particle.function()s and Particle.subscribe() handlers short and fast.
So don’t hang round in delays or loops inside a handler.
Rather just set a flag - or if you are interested in the data that comes in, take a copy of it - and leave.
The processing of flag and/or data can/should then be done inside loop().
To pinpoint your actual problem you’d need to do some more debugging, since the code itself should not cause the problem.
Reasons could well be
an error condition in cloud due to timeout caused by your delay()s in the handler
multiple triggers due to cloud glitch
multiple triggers due to bad phone connection
multiple triggers due shaky finger on smart phone
and of course the most obvious reason which @Moors7 spotted and I missed (see below - doh’ )
But you can take precautions (e.g. add timeout before allowing a retrigger).
Well then... I'm going to instinctively guess that you're not the most original person that roams the earth. (un)fortunately for you, you're probably not the only one. My best guess is that someone else was also too lazy to come up with a better name than button for their event name Had you published that privately, that wouldn't have been a problem, but the way it currently is, somebody else is probably triggering your handler by accident.
Either think of something more unique, or publish/subscribe privately. You could also use a function trigger, rather than a subscribe.
That said, follow the suggestions by @ScruffR as well, he seems to know what he's talking about
That's a keeper (never thought Pavlov was really that useful...)
Ah! That has got to be it. I was using code near verbatim, down to the title. I did have the title changed, but stuck to the template just until I got my sealegs. I know it was public script, but I hadn’t fully grasped what that entailed. I had a uniquely named event, but ditched it to ensure I followed a project that definitely worked. I will take your advice on the handler brevity, ScruffR, thank you.
There must have been some other confused souls who were getting one heck of a show when my 18 month old manned the helm and went nuts.
The worst part of this whole debacle is that despite the extraneous blinking, I only ended up with one lunch. Thanks anyway, mysterious kindhearted internet hero.