Delay in multiple write , cant figure it out how to implement

i want to make a traffic signal which has four leds ,

  1. Red
  2. Yellow
  3. Green forward
  4. Green left

when a virtual pin (v2) is low //defaut value
do {
red on
delay 30 sec
red off
yellow on
delay 2 sec
yellow off
both green on
delay 30
repeat
}
else {
both green on
}

my question are ,
is how to add delay in between multiple digital writes ?
is it possible to do it in code or do i need to create a virtual pin for each led and control it over automation , if yes it web automation doesn’t have loop condition.
if v2 is low does this work offline, even when it cant receive data via internet on v2

some code , documentation, examples are really helpful

You can’t use delays with Blynk, they block the Blynk code execution.

You could use a series of nested timeout timers…

void red_on()
{
  digitalWrite(green_f_led,0);
  digitalWrite(green_l_led,0);   
  digitalWrite(red_led,1);  
  timer.setTimeout(30000L, yellow_on);
}

void yellow_on()
{
  digitalWrite(red_led,0);
  digitalWrite(yellow_led,1);   
  timer.setTimeout(2000L, green_f_on);
} 

etc ...

Pete.

1 Like

please have a look , will the functions work now or do i need to make any changes
ignore the serial data

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

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "TMPLzKn_S2WG"
#define BLYNK_DEVICE_NAME           "Quickstart Device"
#define BLYNK_AUTH_TOKEN            ""


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial

int red =  D1;       // 5; /
int yellow = D2;     //4;
int greenf = D6;     //12;
int greenr = D7;     //14;

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

char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}


BLYNK_WRITE(V4) // Executes when the value of virtual pin 0 changes
{
  if(param.asInt() == 0)
  {
     void red_on();
     void yellow_on();
     void green_on();
     Serial.print("V4 is 0 ");
  }
  else{ 
  Serial.print("v4 is 1");
  digitalWrite(red,0);
  digitalWrite(yellow,0);
  digitalWrite(greenf,1);
  digitalWrite(greenr,1);


  }
}
void red_on()
{ Serial.print("red is on now");
  digitalWrite(greenf,0);
  digitalWrite(greenr,0);   
  digitalWrite(red,1);  
  timer.setTimeout(30000L, yellow_on);
}


void yellow_on()
{ Serial.print("yellow is on now");
  digitalWrite(red,0);
  digitalWrite(yellow,1);   
  timer.setTimeout(2000L, green_on);
} 

void green_on(){
  Serial.print("All green is on now");
  digitalWrite(yellow,0);
  digitalWrite(greenf,1);
  digitalWrite(greenr,1);   
  timer.setTimeout(30000L, red_on);
}


// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.


void setup()
{
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(greenf, OUTPUT);
  pinMode(greenr, OUTPUT);
  // Debug console
  Serial.begin(115200);

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

  // Setup a function to be called every second
 
}

void loop()
{
  Blynk.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!
}



@curiousmind Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

done with edit

You’re missing timer.run(); from your void loop.

I’d also suggest that you remove this QuickStart example code…

Pete.

1 Like

hey pete thanks for the help, but still the red_on(), yellow_on, green_on () doesnt work the is no serial output from these function.

i have a button that send 1 to v4 even turing it on off doesnt work any idea what can be wrong `


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

  This is a simple demo of sending and receiving some data.

  Be sure to check out other examples!

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

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud

// See the Device Info tab, or Template settings

#define BLYNK_TEMPLATE_ID           "TMPLzKn_S2WG"

#define BLYNK_DEVICE_NAME           "Quickstart Device"

#define BLYNK_AUTH_TOKEN            ""

// Comment this out to disable prints and save space

#define BLYNK_PRINT Serial

int red =  D1;       // 5; /

int yellow = D2;     //4;

int greenf = D6;     //12;

int greenr = D7;     //14;

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.

// Set password to "" for open networks.

char ssid[] = "";

char pass[] = "";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes

BLYNK_WRITE(V4) // Executes when the value of virtual pin 0 changes

{

  if(param.asInt() == 0)

  {

     void red_on();

     void yellow_on();

     void green_on();

     Serial.print("V4 is 0 ");

  }

  else{

  Serial.print("v4 is 1");

  digitalWrite(red,0);

  digitalWrite(yellow,0);

  digitalWrite(greenf,1);

  digitalWrite(greenr,1);

  }

}

void red_on()

{ Serial.print("red is on now");

 

  digitalWrite(greenf,0);

  digitalWrite(greenr,0);  

  digitalWrite(red,1);  

  timer.setTimeout(30000L, yellow_on);

}

void yellow_on()

{ Serial.print("yellow is on now");

  digitalWrite(red,0);

  digitalWrite(yellow,1);  

  timer.setTimeout(2000L, green_on);

}

void green_on(){

  Serial.print("All green is on now");

  digitalWrite(yellow,0);

  digitalWrite(greenf,1);

  digitalWrite(greenr,1);  

  timer.setTimeout(30000L, red_on);

}

void setup()

{

  pinMode(red, OUTPUT);

  pinMode(yellow, OUTPUT);

  pinMode(greenf, OUTPUT);

  pinMode(greenr, OUTPUT);

  // Debug console

  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  // You can also specify server:

  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);

  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second

 

}

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!

}

Your BLYNK_WRITE(V4) function makes no sense.
If you want to start the process from the button widget then you’d need to call red_on (without the preceding void).

If you want to stop the process via the button then it’s more complex. You’d need to set a global variable to true and check that it’s true before calling the next function in the sequence.
For the off function you’d need to do digitalWrites to each LED to turn them off.

Pete.

1 Like

thanks pete was able to solve everything , just one last question is there any possible way that
BLYNK_WRITE(V4)
works on its own , currenty it only gets initiated when an change has been in V4,
i want it to start when the power is on and need not manually change the V4 value to get started.
is it possible that this function can work even when internet is down v4 == 0

once again thanks a lot.

one more thing i noticed is when the v4 is 1 then also the values are changing , is there any way that the led shouldnt change only the green light should be turned on but at few sec other lights too are changing any way to stop it.

BLYNK_WRITE(vPin) only executes when the value of vPin changes - that’s the whole point of this function.

If you want to launch the sequence at startup then put the red_led() command in void setup.

I have no idea what you’re asking here, which “values” are you referring to?
I’d suggest you post your full sketch and explain in detail what your issue is.

Pete.

imagine these functions are running in void setup red, yellow, green , is there any way to temporary disable them and allow only green led on if v4 is high , if v4 is low again continue the void setup function red, green , yellow

You seem to misunderstand how these are working. The yellow_led() function is called by the red_led() function once the timeout timer has completed. All you are doing in void setup or your BLYNK_WRITE function is to call the red_led() function to initiate the sequence. From that point onwards it’s self-triggering.

As I said earlier, if you want to interrupt that process via a BLYNK_WRITE function then you need to be setting a global variable tgat acts as a flag. Your cascaded [colour]_led functions would need to check the state of that flag variable before continuing, using an if logical test.

Read the “Overriding a Timer (auto/manual mode)” section of the tutorial I linked to in post #2 for more info.

Pete.

The only problem i am facing is when the v4 is low the cascading ligh doesnt start like , there are time like if red is on and v4 is high after that low , the red function get disturbed, it doesnt start sponanously, it start from next function leading to an no lights on traffic signal, any help would be highly appreciated

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

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID           "TMPLzKn_S2WG"
#define BLYNK_DEVICE_NAME           "Quickstart Device"
#define BLYNK_AUTH_TOKEN            ""


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial

int red =  D1;       // 5; /
int yellow = D2;     //4;
int greenf = D6;     //12;
int greenr = D7;     //14;
int bypass = 0 ;

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

char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes



BLYNK_WRITE(V4) // Executes when the value of virtual pin 0 changes
{
  if(param.asInt() == 1)
  {
  bypass = 1;
  digitalWrite(red,0);
  digitalWrite(yellow,0);
  digitalWrite(greenf,1);
  digitalWrite(greenr,1);

  }
  else{
  digitalWrite(greenf,0);
  digitalWrite(greenr,0);
  red_on();

  }

}




void red_on()
  
  
{ if (bypass == 0) {
   digitalWrite(greenf,0);
   digitalWrite(greenr,0);
   digitalWrite(yellow,0);
   digitalWrite(red,1);   
   Serial.println("Red");
  timer.setTimeout(30000L, yellow_on);
}
}


void yellow_on()
{ Serial.println("yellow");
  digitalWrite(red,0);
  digitalWrite(yellow,1);   
  timer.setTimeout(2000L, green_on);
} 

void green_on(){
  Serial.println("All green");
  digitalWrite(red,0);
  digitalWrite(yellow,0);
  digitalWrite(greenf,1);
  digitalWrite(greenr,1);   
  timer.setTimeout(30000L, red_on);
}





void setup()
{
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(greenf, OUTPUT);
  pinMode(greenr, OUTPUT);
  // Debug console
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  red_on();

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

  // Setup a function to be called every second
 
}


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!
}

I have no idea what this means!

Pete/