The classic LOOP problem :-)

Hello All,

I am aware that breaking out of a loop condition is much talked about in the forums. I did extensive search in the forum but could not come to a clear conclusion after testing.

I want to achieve below actions from my NodeMCU (ESP8266) which has a Relay connected to it

  1. When I press V0 from Blynk App the relay fires up- This is working as expected

  2. When I press V1 from Blynk App, the relay should Pulse or Switch On for 2 seconds and OFF for 2 seconds continually till V1 comes back to 0. --> This goes into loop once V1 is activated and does not read value of V1 if it changes. I assume that due to loop this issue happens and Blynk goes offline.

I have tried lot of codes - mix and match but frustrated that it’s not working :slight_smile: Please do help me…I’m a beginner in the world of IOT and coding…

My code is below:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

/*** Here are the things that you have to change ***/
/***************************************************/

/* Blynk AUTH key */
char const AUTH[] = "XXXXX";

/* Wi-Fi Details */
char const SSID[] = "XXXX";
char const PASS[] = "XXXX";
char const HOSTNAME[] = "HOSTNAME";
char const OTAPSW[] = "OTA PASSWORD";

/* Blynk Virtual Pins */
#define RELAYVPIN1 V0
#define RELAYVPIN2 V1

/* Device Pins */
#define RELAY 14
#define LED 13
#define BUTTON 0

/**************************************************/



/* General variables and declarations */
BlynkTimer timer;
volatile bool state;
volatile bool state2;

void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(RELAY,OUTPUT);
//  pinMode(BUTTON,INPUT);
  
  WiFi.hostname(HOSTNAME);
  Blynk.begin(AUTH, SSID, PASS);

  ArduinoOTA.setPassword(OTAPSW);
  ArduinoOTA.setHostname(HOSTNAME);
  ArduinoOTA.begin();


  
  digitalWrite(LED, LOW);
  delay(700);
  digitalWrite(LED, HIGH);
  delay(700);
  digitalWrite(LED, LOW);
  delay(700);
  digitalWrite(LED, HIGH);
}

BLYNK_CONNECTED() {
    Blynk.syncAll();
    Blynk.syncVirtual(V1);
}

void loop() {
  if (Blynk.connected()) { Blynk.run(); }
  else { Blynk.connect(); }
  timer.run();
  ArduinoOTA.handle();
}

/***************************************************
 * Read virtual pin 0 and turn on/off relay
 **************************************************/
BLYNK_WRITE(RELAYVPIN1){
  state = param.asInt();
  digitalWrite(RELAY, state);
}


BLYNK_WRITE(RELAYVPIN2)
{
  state2 = param.asInt();
 if (state2 == 1) 
  {
    do 
    {
      digitalWrite(RELAY, 1);
      delay(2000);
       digitalWrite(RELAY, 0);
      delay(2000);
    }  while (state2 == 1);
}


}

Your BLYNK_WRITE(RELAYVPIN2) code needs to be re-written and it needs to fire a Blynk timeout timer that is used to flip-flop between the on/off states every two seconds.

When V0 is pressed it should delete the timeout timer.

Pete.

I m clueless on timers. Please can someone guide.

Thanks

please read this post

i always use this for generating delays

timer.setTimeout(5000L, [](){ digitalWrite(pin, LOW) } );