BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blink once the led

Hello, group, i want to blink for 1 or 2 sec ; once the led or digital pin when is changing the temperature.
But all the time is blinking mi led .
Can someone tell me how to stop blinks.

#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial
#define DHTPIN 1
#define DHTTYPE DHT22
char auth[] = "gtgtrghrthrt";
char ssid[] = "Tggfdg";
char pass[] = "697gtrgrt";
// change as per your button attached.
const int relayPin =  14;
const int rel =  12;
int motorInitiated = 0;
int motorInitiated4 = 0;
int motorInitiated5 = 0;
int motorInitiated6 = 0;
int motorInitiated7 = 0;
int setTemp;
int t1;

SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);
WidgetBridge bridge1(V1);
WidgetBridge bridge2(V2);



BLYNK_CONNECTED() {
  Blynk.syncAll();
  // Place the AuthToken of the second hardware here
  bridge1.setAuthToken("ffferfer);
  bridge2.setAuthToken("7rgergerg");

}




BLYNK_WRITE(V11) {

  int pinData = param.asInt();

  if (pinData == 1) {

    motorInitiated = 0;
  } else {
    motorInitiated++;
    motorInitiated6 = 0;
  }
}

BLYNK_WRITE(V12) {

  int pinData = param.asInt();

  if (pinData == 1) {
    motorInitiated4++;
    digitalWrite(rel, HIGH);
  } else {
    motorInitiated4 = 0;
    delay(6000);
    digitalWrite(relayPin, LOW );
    digitalWrite(rel, LOW);
  }
}



BLYNK_WRITE(V3)
{
  setTemp = param.asInt();
}



void sendSensor() {

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V41, h);
  Blynk.virtualWrite(V31, t);

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }


  if (t >= setTemp) {

    motorInitiated6 = 0;
    if (motorInitiated4 == 0) {
      motorInitiated5 = 0;

    }
    delay(1500);
    timer.restartTimer(t1);
    timer.disable(t1);

  }
  else  {

    if (motorInitiated == 0) {
      motorInitiated6++;
    }
    if (motorInitiated4 == 1) {
      motorInitiated5++;


    }
    timer.enable(t1);


  }
}




void sendSensor3() {

  if (motorInitiated5 == 1) {


    digitalWrite(relayPin, HIGH);
    delay(1000);
    digitalWrite(relayPin, LOW );
    delay(500);
    timer.restartTimer(t1);
    timer.disable(t1);

  }

}



void sendSensor4() {

  if (motorInitiated6 == 0 && motorInitiated7 == 0)
  {

    bridge1.virtualWrite(V72, 0);
  } else {
    bridge1.virtualWrite(V72, 1);

  }
}





void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);
  pinMode(rel, OUTPUT);
  digitalWrite(rel, HIGH);
  dht.begin();
  timer.setInterval(5000L, sendSensor);
  t1 = timer.setInterval(2100L, sendSensor3);
  timer.setInterval(3000L, sendSensor4);





}

void loop() {
  Blynk.run();
  timer.run();
}

the relaypin

Hello,

I can see lot of delay in your sketch. This is a blocking function. It prevents other part of the sketch from running. Replace delay with timer.setInterval.

This will cause disconnections to the server. And other problems like you are facing now.

If I put timer set interval we will repeat again.


#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial
#define DHTPIN 1
#define DHTTYPE DHT22
char auth[] = "ZfhbnfnZ";
char ssid[] = "Tvgfhfhn9";
char pass[] = "rgretgr";
// change as per your button attached.
const int relayPin =  14;
const int rel =  12;
int motorInitiated = 0;
int motorInitiated4 = 0;
int motorInitiated5 = 0;
int motorInitiated6 = 0;
int motorInitiated7 = 0;
int setTemp;

SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);
WidgetBridge bridge1(V1);
WidgetBridge bridge2(V2);



BLYNK_CONNECTED() {
  Blynk.syncAll();
  // Place the AuthToken of the second hardware here
  bridge1.setAuthToken("56chghgf5");
  bridge2.setAuthToken("7032a21b5345f8gbfgbg
}




BLYNK_WRITE(V11) {

  int pinData = param.asInt();

  if (pinData == 1) {

    motorInitiated = 0;
  } else {
    motorInitiated = 1;
    motorInitiated6 = 0;
  }
}

BLYNK_WRITE(V12) {

  int pinData = param.asInt();

  if (pinData == 1) {
    motorInitiated4 = 1;
    digitalWrite(rel, HIGH);
  } else {
    motorInitiated4 = 0;
    delay(6000);
    digitalWrite(relayPin, LOW );
    digitalWrite(rel, LOW);
  }
}



BLYNK_WRITE(V3)
{
  setTemp = param.asInt();
}



void sendSensor() {

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V41, h);
  Blynk.virtualWrite(V31, t);

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }


  if (t >= setTemp) {

    motorInitiated6 = 0;
    if (motorInitiated4 == 1) {
      motorInitiated5 = 1;

    }
  }
  else  {

    if (motorInitiated == 0) {
      motorInitiated6 = 1;
    }
    if (motorInitiated4 == 0) {
      motorInitiated5 = 0;
    }
  }
}



void sendSensor3() {

  if (motorInitiated5 == 1) {


    digitalWrite(relayPin, HIGH);
    delay(1000);
    digitalWrite(relayPin, LOW );
  }

  else {


    digitalWrite(relayPin, HIGH);
    delay(1000);
    digitalWrite(relayPin, LOW );


  }
}




void sendSensor4() {

  if (motorInitiated6 == 0 && motorInitiated7 == 0)
  {


    bridge1.virtualWrite(V72, 0);

  } else {

    bridge1.virtualWrite(V72, 1);

  }
}





void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);
  pinMode(rel, OUTPUT);
  digitalWrite(rel, HIGH);
  dht.begin();
  timer.setInterval(800L, sendSensor);
  timer.setInterval(2000L, sendSensor3);
  timer.setInterval(3000L, sendSensor4);




}

void loop() {
  Blynk.run();
  timer.run();
}




still is repeating the blink.
i want to blink only one time when is changing temperature.

Try using a lambda timing function. So first you turn on led the insert lambda timer for 1 second and turn off

Use this logic

byte runXTimes = 1; // set no. of times to run a function

void loop()
{
 if (runXTimes)
 {
   //Blink Led function here
    runXTimes--;
 }
}

Ok I will check thanks



#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial
#define DHTPIN 1
#define DHTTYPE DHT22
char auth[] = "ZPZ";
char ssid[] = "TALKTALKB971A9";
char pass[] = "697NNQ4Y";
// change as per your button attached.
const int relayPin =  14;
const int rel =  12;
int motorInitiated = 0;
int motorInitiated4 = 0;
int motorInitiated5 = 0;
int motorInitiated6 = 0;
int motorInitiated7 = 0;
int setTemp;
byte runXTimes = 1; // set no. of times to run a function
SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);
WidgetBridge bridge1(V1);
WidgetBridge bridge2(V2);



BLYNK_CONNECTED() {
  Blynk.syncAll();
  // Place the AuthToken of the second hardware here
  bridge1.setAuthToken("56e95");
  bridge2.setAuthToken("7");

}




BLYNK_WRITE(V11) {

  int pinData = param.asInt();

  if (pinData == 1) {

    motorInitiated = 0;
  } else {
    motorInitiated = 1;
    motorInitiated6 = 0;
  }
}

BLYNK_WRITE(V12) {

  int pinData = param.asInt();

  if (pinData == 1) {
    motorInitiated4 = 1;
    digitalWrite(rel, HIGH);
  } else {
    motorInitiated4 = 0;
    delay(6000);
    digitalWrite(relayPin, LOW );
    digitalWrite(rel, LOW);
  }
}



BLYNK_WRITE(V3)
{
  setTemp = param.asInt();
}



void sendSensor() {

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V41, h);
  Blynk.virtualWrite(V31, t);

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }


  if (t >= setTemp) {

    motorInitiated6 = 0;
    if (motorInitiated4 == 1) {
      motorInitiated5 = 1;
      if (runXTimes)
      {
        digitalWrite(relayPin, HIGH);
        delay(1000);
        digitalWrite(relayPin, LOW );
        runXTimes = 1;
      }
    }
  }
  else  {

    if (motorInitiated == 0) {
      motorInitiated6 = 1;
    }
    if (motorInitiated4 == 0) {
      motorInitiated5 = 0;
    }
  }
}






void sendSensor4() {

  if (motorInitiated6 == 0 && motorInitiated7 == 0)
  {


    bridge1.virtualWrite(V72, 0);

  } else {

    bridge1.virtualWrite(V72, 1);

  }
}





void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);
  pinMode(rel, OUTPUT);
  digitalWrite(rel, HIGH);
  dht.begin();
  timer.setInterval(800L, sendSensor);
  timer.setInterval(3000L, sendSensor4);




}

void loop() {
  Blynk.run();
  timer.run();


}







still is repeating ; i use the digital signal to connect in optocuplor to turn on or off the ac portable; because no have a thermostat .
the power button is digital if i pres 1 time then release is turning on again if i pres and release is turning off.

Just tell me if I insert the correct parameters on the code or I have to make separating loop

it should be runXtimes--;

So it will subtract the existing value…so 1-1=0 so that function will not run next time.

Find a strategic place and place runXtimes = 1; so that next time when the value changes it will allow the code to run.

I would use it as a flag, not so much a counter.

  if (t >= setTemp) {

    motorInitiated6 = 0;
    if (motorInitiated4 == 1) {
      motorInitiated5 = 1;
      if (runXTimes == 1)
      {
        digitalWrite(relayPin, HIGH);
        delay(1000);
        digitalWrite(relayPin, LOW );
        runXTimes = 0;
      }
    }
  }
  else   {
    runXTimes = 1;
    if (motorInitiated == 0) {
      motorInitiated6 = 1;
    }
    if (motorInitiated4 == 0) {
      motorInitiated5 = 0;
    }
  }
1 Like

#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BLYNK_PRINT Serial
#define DHTPIN 1
#define DHTTYPE DHT22
char auth[] = "ZPFFlm";
char ssid[] = "TALKTALKB971A9";
char pass[] = "697NNQ4Y";
// change as per your button attached.
const int relayPin =  14;
const int rel =  12;
int motorInitiated = 0;
int motorInitiated4 = 0;
int motorInitiated5 = 0;
int motorInitiated6 = 0;
int motorInitiated7 = 0;
int setTemp;
byte runXTimes = 1;
byte runXTimes1 = 1;

SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);
WidgetBridge bridge1(V1);
WidgetBridge bridge2(V2);



BLYNK_CONNECTED() {
  Blynk.syncAll();
  // Place the AuthToken of the second hardware here
  bridge1.setAuthToken("56e9098a5");
  bridge2.setAuthToken("705f8");

}




BLYNK_WRITE(V11) {

  int pinData = param.asInt();

  if (pinData == 1) {

    motorInitiated = 0;
  } else {
    motorInitiated = 1;
    motorInitiated6 = 0;
  }
}

BLYNK_WRITE(V12) {

  int pinData = param.asInt();

  if (pinData == 1) {
    motorInitiated4 = 1;
    digitalWrite(rel, HIGH);
  } else {
    motorInitiated4 = 0;
    delay(6000);
    digitalWrite(relayPin, LOW );
    digitalWrite(rel, LOW);
  }
}



BLYNK_WRITE(V3)
{
  setTemp = param.asInt();
}



void sendSensor() {

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V41, h);
  Blynk.virtualWrite(V31, t);

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }


  if (t >= setTemp) {
    runXTimes1 = 1;
    motorInitiated6 = 0;
    if (motorInitiated4 == 1) {
      motorInitiated5 = 1;
      if (runXTimes == 1)
      {
        digitalWrite(relayPin, HIGH);
        delay(1000);
        digitalWrite(relayPin, LOW );
        runXTimes = 0;
      }
    }
  }
  else  {
    runXTimes = 1;
    if (motorInitiated == 0) {
      motorInitiated6 = 1;
    }
    if (motorInitiated4 == 1) {
      motorInitiated5 = 0;
      if (runXTimes1 == 1)
      {
        digitalWrite(relayPin, HIGH);
        delay(1000);
        digitalWrite(relayPin, LOW );
        runXTimes1 = 0;
      }


    }
  }


}



void sendSensor4() {

  if (motorInitiated6 == 0 && motorInitiated7 == 0)
  {


    bridge1.virtualWrite(V72, 0);

  } else {

    bridge1.virtualWrite(V72, 1);

  }
}





void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);
  pinMode(rel, OUTPUT);
  digitalWrite(rel, HIGH);
  dht.begin();
  timer.setInterval(1500L, sendSensor);
  timer.setInterval(3000L, sendSensor4);




}

void loop() {
  Blynk.run();
  timer.run();


}


Thank you [Toro_Blanco] and [Madhukesh] the code is working perfectly when is v12 is on; just tell me if the timing on set interval sensor is ok or any small mistake on code appear just let me know please and THANK YOU.

Not so much a mistake, but just not the best way to do with BLYNK. I would get rid of the delay() in your code. This is a blocking function, and can cause connection issues with BLYNK.

You could use what is referred to as the lamda function. For example:

if (t >= setTemp) {
    runXTimes1 = 1;
    motorInitiated6 = 0;
    if (motorInitiated4 == 1) {
      motorInitiated5 = 1;
      if (runXTimes == 1)
      {
        digitalWrite(relayPin, HIGH);
       timer.setTimeout(1000L, []()
            { digitalWrite(relayPin, LOW ); }); 
       runXTimes = 0;
      }
    }
  }