Button Widget + simple timer

Yes you are right, I changed it now to pinMode
After this changed this, it worked but the timer wont start.

I know the setup is for things that only need to be done once, and the loop is for repeating things. Thats why I made the if/else statement in the loop part.
Am I wrong ?
Is there any change you could just look at the code and help me with the code. I now get send from pillar to post.
My questions are to summarize:

  • how come Blynk is responding to IF/ELSE but not to my timeout timer.
  • Should the IF/ELSE be in loop or setup.

Greetings,
Jozef

Because you’ve removed the timer.run() command.

In reality, the code doesn’t need to be repeated at all.
It simply needs to run once when your BLYNK_WRITE(V1) callback is triggered, so that’s where the code should be located.

I’m not going to write your code for you. I took the time to write two topics which explain how to use virtual pins to control physical devices, and how to use timeout timers. If you’d taken the time to study them your project would have been working a long time ago.

Pete.

Code is working, this topic can be closed.

For people that just want a code for a LED or relay to be powered for a certain amount of time (10s in this sketch), this code might come in handy.

// NODEMCU ESP8266 connect to USB Port, LED (+) connect to D1 (GPIO5) & LED (-) connect to GND
// make Blynk button widget with DATASTREAM V1. 

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "";
char pass[] = "";

int LED = 5; 
int BTN = 0;

BlynkTimer timer10s;

BLYNK_WRITE(V1)  // Virtual button on V1 to activate action
{
 BTN = param.asInt();
     
}

void setup() 
{
  // put your setup code here, to run once:
pinMode(5, OUTPUT);

Serial.begin(9600);
  Blynk.begin(auth, "", "");
}

void loop() {
      timer10s.run();
      Blynk.run();
      
      
if (BTN == 1) 
{
     digitalWrite(LED, HIGH);  // Set pin high
     timer10s.setTimeout(10000L, ActionOFF);  // Run ActionOFF function in 10 seconds
     }
}

void ActionOFF()
{
  digitalWrite(LED, LOW);  // Set pin Low
  
}

Anyone reading this topic in future, and looking for a solution to this issue, PLEASE DO NOT USE THIS CODE!

One of the basic principals of Blynk is that the void loop should be kept free of unnecessary code - otherwise you will suffer unwanted/unnecessary disconnections from the Blynk server in the medium to long term.

The void loop should look like this:

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

As already explained, The BLYNK_WRITE(V1) should look like this

BLYNK_WRITE(V1)
{
  if (param.asInt() == 1)
  {
    digitalWrite(relay, HIGH);
    timer.setTimeout(10000L, []() 
    {  
    // When the timer completes, any code here will be executed:
    digitalWrite(relay, LOW);
    }); 
  }
  else
  {
    digitalWrite(relay, LOW);
  }
}

Happy Blynking!

Pete.