This is in contrast to the blynk recommendation to keep the loop as clean as possible and to work with timers.
I have now switched from blynk timer to particle timer and in my concrete project everything works - except for blynk.
I’ve updated several photons on FWv1.0.0 today and get red panic attacks on my photons with this program. They restart around 10 seconds. It was not until I commented out Blynk that it worked again. Whether it is the Blynk or the Photon I can not judge.
#include <blynk.h>
//#define BLYNK_DEBUG // Optional, this enables lots of prints
//#define BLYNK_PRINT Serial
int inputBrightness = A3; // choose the input pin (for ALSPT19 Light - adafru.it/2748) > measure the analog voltage on the OUT pin. The voltage will increase when sensor detects more light.
int inputPIR = D4; // choose the input pin (for PIR sensor)
int ledPin = D3; // LED Pin
int pirState = LOW; // we start, assuming no motion detected
int boardLed = D7; // photon onBoard LED
int calibrateTime = 5000; // wait for the thingy to calibrate
int lightThreshold = 1000; // brightness as analog value to decide its time for more light as analog value
int ambientBrightness; // Here we are declaring the integer variable ambientBrightness, which we will use later to store the value of the photoresistor.
int i = 0; // delay-Ersatz fĂĽr colorFade - for-Schleife
int red = 255;
int green = 255;
int blue = 255;
#define REDPIN A5
#define GREENPIN WKP
#define BLUEPIN A4
int delayColorFade = 15; // presents the delay for the colorFade
Timer particle_timer_PIR(1000, reportPIRStatus);
Timer particle_timer_Brightness(10000, readBrightness);
char auth[] = "<<your blync auth-code here>>";
// *********
BLYNK_WRITE(V4) // Blynk app WRITES Slider widget
{
lightThreshold = param.asInt();
Serial.println("******************************");
Serial.print(lightThreshold);
Serial.println(" analog value = new Threshold for Light");
Serial.println("******************************");
Blynk.virtualWrite(V3, lightThreshold);
}
// *********
BLYNK_WRITE(V2) // Blynk app WRITES Slider widget
{
delayColorFade = param.asInt(); // macht aus den Blynk-Werten millisekunden
Serial.println("******************************");
Serial.print(delayColorFade);
Serial.println(" milli-sec = new Delay for Function ColorFade");
Serial.println("******************************");
Blynk.virtualWrite(V1, delayColorFade);
}
// *********
BLYNK_WRITE(V0) // zeRGBa - Color
{
// The param can contain multiple values, in such case:
int red = param[0].asInt();
int green = param[1].asInt();
int blue = param[2].asInt();
analogWrite(REDPIN, red);
analogWrite(GREENPIN, green);
analogWrite(BLUEPIN, blue);
}
/// *********
void setup() {
Serial.begin(9600);
digitalWrite(boardLed,HIGH); // Now flash the D7 LED on and off
// if the sensor is calibrated
if (calibrated())
{
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(boardLed,OUTPUT); // on-board LED
pinMode(ledPin, OUTPUT); // control PIR LED
pinMode(inputPIR, INPUT); // declare sensor as input
pinMode(inputBrightness, INPUT); // declare sensor as input
digitalWrite(boardLed,HIGH); // Now flash the D7 LED on and off
Particle.publish("PIR-Motion", "PIR now online - Flur oben", PRIVATE); // informs user via Particle Cloud
digitalWrite(boardLed,LOW);
}
particle_timer_PIR.start();
particle_timer_Brightness.start();
digitalWrite(boardLed,LOW);
// Blynk magic
Blynk.begin(auth); // Start Blynk
}
// *********
void loop() {
// Blynk.run();
}
// *********
bool calibrated() {
return millis() - calibrateTime > 0;
}
// ********
void reportPIRStatus() {
// check the PIR-Sensor : all 1 sec
int pirNewState = digitalRead(inputPIR);
Serial.println(pirNewState);
if (pirNewState == pirState) return; // no further action required
pirState = pirNewState;
setLED(pirState);
if (pirState) {
// send events and start color fading
if (ambientBrightness <= lightThreshold) {
Particle.publish("PIR Motion", "LED Streifen an", PRIVATE);
ColorFade();
}
}
}
// *********
void setLED( int state )
{
digitalWrite(ledPin, state);
}
// *********
void ColorFade() {
// LED Streifen anschalten
analogWrite(REDPIN, red);
analogWrite(GREENPIN, green);
analogWrite(BLUEPIN, blue);
// circa! 15 Sekunden bleibt Licht per Default an bzw. solange, wie ĂĽber den Blynk Slider eingestellt wurde
for (i = 1; i <= (delayColorFade * 100000); i++)
// LED Streifen ausschalten
analogWrite(REDPIN, 0);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPIN, 0);
}
// ********** Neuaufnahme von Funktionen fĂĽr den TSL2561 - Start
void readBrightness()
{
// read the input on analog pin A3:
// 0-3.3V input and 0-4095 range
// https://docs.particle.io/reference/device-os/firmware/photon/#analogread-adc-
int ambientBrightness = analogRead(inputBrightness);
if (ambientBrightness)
{
// print out the value you read:
Serial.println(ambientBrightness);
Blynk.virtualWrite(V7, ambientBrightness);
}
else
{
Serial.println("Sensor ALSPT19 Light - reading error");
}
}
ScruffR gaves me also feedback in another older thread here.
I opened also a Thread in Blynk-Community with no feedback.