ESP8266 Turn on and off D0 Three Times

Hello all,

I’m almost there on a project but just can’t figure out the last bit. I am trying to press V1 and then cause D0 to turn on (for 350ms) then off (for 350ms) three times. Below is my code. I have it working but only on, then off once. How can I change this to cycle three times then stop?

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>    // library is now included in main Blynk library
//SimpleTimer timer;              // now renamed to BlynkTimer as per Sketch Builder
BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "x"; // don't include your security token on a public forum

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Spank_The_Tank";
char pass[] = "x";

int LED = D0;

void LEDoff()
{
  digitalWrite(LED, LOW);
}

BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
  int pinData = param.asInt(); 

  if (pinData == 1) 
  {
    digitalWrite(LED, HIGH);
    int delayedOff = timer.setTimeout(350, LEDoff);  // syntax for some timer functions
  }
} //
void setup()
{
  // Debug console
  Serial.begin(115200);  // 9600 is for those old slow Arduino MCU's not ESP's

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
  }
1 Like

Hope this one helps :slight_smile:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>    // library is now included in main Blynk library
//SimpleTimer timer;              // now renamed to BlynkTimer as per Sketch Builder
BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "x"; // don't include your security token on a public forum

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Spank_The_Tank";
char pass[] = "x";

int LED = D0;

void LEDoff()
{
  digitalWrite(LED, LOW);
}

BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
  int pinData = param.asInt(); 

  if (pinData == 1) 
  {
    for(int i=0;i<3;i++)
          digitalWrite(LED, HIGH);
    int delayedOff = timer.setTimeout(350, LEDoff);  // syntax for some timer function
  }
} //
void setup()
{
  // Debug console
  Serial.begin(115200);  // 9600 is for those old slow Arduino MCU's not ESP's

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
  }

---- Animesh

1 Like

Hi Animesh, thank you for your help!

I tried your code and I’m still getting the same result. The LED comes on for 350ms, then goes off, and does not come back on again. How can I change the code so that when I press a V1 push button once then D0 is:

  1. On for 350ms
  2. Off for 350ms
  3. On for 350ms
  4. Off for 350ms
  5. On for 350ms
  6. Off

Thank you again, or can anyone else help?

Button on V1 should be set to switch.

int count = 0
BLYNK_WRITE(V1)  
  {
    value = param.asInt();
      if (value == 1) 
      {
         if (count <3)
         {
               digitalWrite(LED, HIGH);
               timer.setTimeout(350L, []() {  // Start 1st Timed Lambda Function - Turn OFF LED AFTER 350 ms (once per loop)
                       digitalWrite(LED, LOW);
                       Serial.println("FIRST timer - LED OFF"); 
                       count++;
        timer.setTimeout(350L, []() {  // Start 2nd Timed Lambda Function - Check function again after 350ms (once per loop)
                       Blynk.syncVirtual(V1); // Run V15 Function Again
              });  // END 2nd Timer Function
         
           });  // END 1st Timer Function
        }
       else 
        {
          count = 0 ;
           Blynk.virtualWrite(V1, 0);
        }
      }
  }
1 Like

Thank you!

I got the following error though:

“Error compiling for board Generic ESP8266 Module”

Also same error when switching to NodeMCU 0.9 (ESP-12 Module)

Any idea why I’m getting the error?

Here is the complete sketch:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>    // library is now included in main Blynk library
//SimpleTimer timer;              // now renamed to BlynkTimer as per Sketch Builder
BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "x"; // don't include your security token on a public forum

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Spank_The_Tank";
char pass[] = "x";

int count = 0
BLYNK_WRITE(V1)  
  {
    value = param.asInt();
      if (value == 1) 
      {
         if (count <3)
         {
               digitalWrite(LED, HIGH);
               timer.setTimeout(350L, []() {  // Start 1st Timed Lambda Function - Turn OFF LED AFTER 350 ms (once per loop)
                       digitalWrite(LED, LOW);
                       Serial.println("FIRST timer - LED OFF"); 
                       count++;
        timer.setTimeout(350L, []() {  // Start 2nd Timed Lambda Function - Check function again after 350ms (once per loop)
                       Blynk.syncVirtual(V1); // Run V15 Function Again
              });  // END 2nd Timer Function
         
           });  // END 1st Timer Function
        }
       else 
        {
          count = 0 ;
           Blynk.virtualWrite(V1, 0);
        }
      }
  }
void setup()
{
  // Debug console
  Serial.begin(115200);  // 9600 is for those old slow Arduino MCU's not ESP's

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
  }

You aren’t declaring LED as a variable.

Pete.

What I provided was not really a complete sketch, and probably had some simple errors that needed fixing (I see I was missing a ; after the count variable declaration).

You are missing the declaration for the LED pin, and for VALUE.

I figured you would be able to troubleshoot these simple errors, but I guess not.

This compiles on my computer.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>    // library is now included in main Blynk library
//SimpleTimer timer;              // now renamed to BlynkTimer as per Sketch Builder
BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "x"; // don't include your security token on a public forum

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Spank_The_Tank";
char pass[] = "x";

int count = 0;
int LED = 16;

BLYNK_WRITE(V1)  
  {
    int value = param.asInt();
      if (value == 1) 
      {
         if (count <3)
         {
               digitalWrite(LED, HIGH);
               timer.setTimeout(350L, []() {  // Start 1st Timed Lambda Function - Turn OFF LED AFTER 350 ms (once per loop)
                       digitalWrite(LED, LOW);
                       Serial.println("FIRST timer - LED OFF"); 
                       count++;
        timer.setTimeout(350L, []() {  // Start 2nd Timed Lambda Function - Check function again after 350ms (once per loop)
                       Blynk.syncVirtual(V1); // Run V15 Function Again
              });  // END 2nd Timer Function
         
           });  // END 1st Timer Function
        }
       else 
        {
          count = 0 ;
           Blynk.virtualWrite(V1, 0);
        }
      }
  }
void setup()
{
  // Debug console
  Serial.begin(115200);  // 9600 is for those old slow Arduino MCU's not ESP's

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
  }
2 Likes

It works thank you!!!

I know this was simple and I see how you did it now. I’m new to this as you can tell.

Hopefully others will be able to use this code to blink an led or relay multiple times.