In the spirit of not using “delay()” when flashing LEDs and such, I have put together a script from a couple of projects. I am using Particle.function and the Particle App string command to trigger an event that will flash the LED 10 times, then reset at the end of 10 flashes and be ready for another trigger. The problem is: the loop works once , flashes the LED 10 times, then does not reset the flag to allow the loop to run again ( that’s my assumption). The trigger command is getting through to the Photon every time, but it is not triggering the LED to blink 10 times. I am also assuming there is a simple mistake in my method to clear or reset the “blink” flag. Or ??
I am also using the “Blynk” app with this script which works for reading the RSSI.
Many thanks in advance.
Here is the full Code:
// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
//*****************This is currently on C_M *********************************
// This #include statement was automatically added by the Particle IDE.
#include <photon_book.h>
// On and Off Times (as int)
const unsigned int onTime = 50;
const unsigned int offTime = 100;
// Tracks the last time event fired
unsigned long previousMillis=0;
// Interval is how long we wait
int interval = onTime;
int maxnum = 20;
int count = 0;
// Used to track if LED should be on or off
boolean LED3state = true; //primary LED to blink 10 times first
boolean LED4state = true; //this would be the next LED to blink right after the first one blinks 10 times (to be added next) FUTURE USE
boolean blink; //Establishes "blink" as boolean, this was "boolean blink=false"
char auth[]= "xxxxxxxxxxxxxxxxxxxxxx";
int wifiStrength;
float startTime;
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
Particle.function ("LED_control", LED_control); // create a cloud function ("LED_control"),
//this will show up in the Particle app and can be triggered from the app to cause the "LED_control(String command) to go "true" ?, not sure how this works, but it does)
Particle.function ("Reset", Reset); //This is to manually reset for troubleshooting the "blink" not resetting at the end of the loop.
Blynk.begin(auth);
WiFi.selectAntenna(ANT_EXTERNAL);
}
int LED_control(String command) // when you trigger this from the Particle App it starts the script directly following this,
//it flashes LED 2 to let you know you received the trigger from the Particle App
{
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
blink = true; //Sets the "blink flag to "true" to start the "if (blink)" in the loop
return 1; // tells the Particle App the command was sucsessfully received
}
int Reset(String command) //This is a manual reset, This will make the LED on pin 2 come on but does not seem to reset the "blink" to "false".
{
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
blink = false; //Sets the "blink flag to "true" to start the "if (blink)" in the loop (Does not reset to false)
return 1; // tells the Particle App the command was sucsessfully received
}
void loop() //this works once when triggered, then the Photon needs to be reset to run this again. The "blink" flag is not being reset after the loop.
{
if (blink) // this gets triggered from the mini script above "(LED_(String command)"
{
if (count < maxnum) // used to count the number of times through the loop to count 10 flashes
{
// Set Pin 3 to state of LED3state each timethrough loop()
// If LED13State hasn't changed, neither will the pin
digitalWrite(3, LED3state);
// Grab snapshot of current time, this keeps all timing
// consistent, regardless of how much code is inside the next if-statement
unsigned long currentMillis = millis();
// Compare to previous capture to see if enough time has passed
if ((unsigned long)(currentMillis - previousMillis) >= interval)
{ // Change wait interval, based on current LED state
if (LED3state)
{
// LED is currently on, set time to stay off
interval = offTime;
}
else
{
// LED is currently off, set time to stay on
interval = onTime;
}
// Toggle the LED's state, Fancy, eh!?
LED3state = !(LED3state);
// Save the current time to compare "later"
previousMillis = currentMillis;
count++; //increment the counter
}
}
}
if (millis() > startTime + 2000)
{
startTime = millis();
//wifiStrength = map(WiFi.RSSI(), -100, 0, 0, 100); // This version maps -100 to 0 to 0-100 sends a value between 0-100
wifiStrength = WiFi.RSSI(); // This version of RSSI sends the true value in -dBm to Blynk App
Blynk.virtualWrite(V20, wifiStrength); //Sends the wifi RSSI to Blynk widget
Blynk.run();
Particle.process();
}
}
// This works but only once through the loop, when the "LED_control" command is received a second or third time it is ignored :-(
// ??? SOMEWHERE I NEED TO SET THE FLAG BACK TO "FALSE" ? So the loop is ready to receive (blink) again ??