Hold LED state HIGH for 10 seconds then return LOW, everytime the button pushed

Hi, it’s my first time here asking to the community,

So, its simple problem i guess, i’ve been looking around almost for 3 days but none of the existing code i’ve learned work in my sketch,

What i want to do is, create a SwitchButton on V1, When the switch turn on, the LED will turn on for 10 seconds, and then, after 10 seconds the switch automatically back to off also the LED,

I’m using ESP8266-01 GPI02 connected to my LED,

here is my existing code without timer/hold/delay function, only turn on and off

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "invisible";

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

int LED = 2;

BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
  int pinData = param.asInt(); 
  if (pinData == 1) 
  {
    digitalWrite(LED, HIGH);}
    else 
    {
      digitalWrite(LED, LOW);
    }
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  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();
  // 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!
}

Check PUSH_DATA example and Arduino timer.setTimeout().

@costas Thanks for the reply, i’m trying this code after reading timer.setTimeout()

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "this_is_private";

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

int LED = 2;

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);
    timer.setTimeout(5000, LEDoff);
  }
void setup()
{
  // Debug console
  Serial.begin(9600);

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

But won’t compile, what did i missed ?

@ferrizall try the code below, I haven’t compiled it. Check over each of my comments.

/* 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[] = "xxxxxxxx"; // don't include your security token on a public forum

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

int LED = 2;

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(5000, 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!
}

@costas
it said ‘BlynkTimer’ does not name a type, even after i updated to the latest Blynk library.

What library version are you using and did you do it all manually as auto generally fails. Manually means close the IDE, copy and paste the old libraries to a new location for safe keeping, delete old libraries and paste in new libraries. Then start IDE.

@Costas

i updated it manually, and now the error code move to :

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

it said :
exit status 1
a function-definition is not allowed here before ‘{’ token

highlighted right after void loop()
{ <---- this one is highlighted.

Post your entire, current, code here… easier for us to track the syntax error.

Edited from your post :

Check the last line

Beat you to it :stuck_out_tongue_closed_eyes:

Yup, I am slow typer :stuck_out_tongue: I had the wrong quote anyhow, so I just deleted it all.

1 Like

Yup, my bad,
thanks, @Fettkeewl .

Now Uploading, hope this code work…

Thanks guys it worked, just have to reverse the state low and high.

What solved the compilation error on this? I am confused

@danielkitchen please take note of TimeDate stamps before posting… this topic is over 8 months old :wink:

Compilation resolution was adding in a closing bracket.

1 Like