Ohh Thanks alot
@Trieu_Le I am looking at your sketch now. Stripped it down to this (note there is no header required for the timer, whatever name it has).
// Press4Xseconds.ino per https://community.blynk.cc/t/solved-virtual-button-pressed-for-x-seconds/11379/58
#define BLYNK_PRINT Serial
//#include <DNSServer.h>
//#include <ESP8266WebServer.h>
//#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>
//#include <BlynkTimer.h>
//#include <LiquidCrystal_I2C.h>
//#include "EmonLib.h"
//#include <EEPROM.h>
//EnergyMonitor emon1;
BlynkTimer timer;
int newTimer = 1;
int sb = 1;
WidgetLED led5(V1);
WidgetLED led6(V2);
char auth[] = "xxx";
char ssid[] = "GargoyleTest";
char pass[] = "xxx";
char server[] = "xxx";
void setup()
{
Serial.begin(115200);
Serial.println();
//WiFiManager wifiManager;
Blynk.begin(auth, ssid, pass, server);
timer.setInterval(1000L, LoadPower);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates SimpleTimer
}
void LoadPower()
{
/*
double Irms = emon1.calcIrms(1480); // Calculate Irms only
if (Irms < 0.3) Irms = 0;
long watt = Irms*230.0; // default was 230
wattsume = wattsume+watt;
energy=wattsume/(3600*1000);
Blynk.virtualWrite(V8, Irms);Blynk.run();
Blynk.virtualWrite(V9, watt);Blynk.run();
Blynk.virtualWrite(V10, energy);Blynk.run();
EEPROM.put(50,energy);
delay(100);
EEPROM.commit();
*/
}
BLYNK_CONNECTED() {
// Request the latest state from the server
//Blynk.syncVirtual(V1,V2,V3,V4,V5,V6);
Blynk.syncVirtual(V5);
}
BLYNK_WRITE(V5)
{
if(param.asInt()){
// button pressed
newTimer = timer.setTimeout(2000,unlock);
led6.on();
}
else {
timer.disable(newTimer);
led6.off();
}
}
void unlock()
{
if (sb==1){
sb=0;
led5.off();
Serial.println("LED5 OFF");
}
else if (sb==0){
sb=1;
led5.on();
Serial.println("LED5 ON");
}
led6.off();
Serial.println("LED6 OFF");
}
I still need to add some more simulation but I’ll get back to you when I have worked something out.
@Trieu_Le if you run the following sketch with the timer count being displayed in V0 value display you will see it toggles between 1 and 2. All is fine, the bugs are in the rest of your sketch
// Press4Xsecs.ino per https://community.blynk.cc/t/solved-virtual-button-pressed-for-x-seconds/11379/58
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
int newTimer = 1;
int sb = 1;
WidgetLED led5(V1);
WidgetLED led6(V2);
char auth[] = "xxx";
char ssid[] = "GargoyleTest";
char pass[] = "xxx";
char server[] = "xxx";
void setup()
{
Serial.begin(115200);
Serial.println();
Blynk.begin(auth, ssid, pass, server);
timer.setInterval(1000L, LoadPower);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates SimpleTimer
}
void LoadPower()
{
// do some Emon stuff here
Blynk.virtualWrite(V0, timer.getNumTimers()); // display # of running timers in Blynk
}
// V0 value display
// V1 Green LED
// V2 Red LED
BLYNK_WRITE(V5)
{
if(param.asInt()){
newTimer = timer.setTimeout(2000,unlock); // button pressed
led6.on();
}
else {
timer.disable(newTimer);
led6.off();
}
}
void unlock()
{
if (sb==1){
sb=0;
led5.off();
Serial.println("LED5 OFF");
}
else if (sb==0){
sb=1;
led5.on();
Serial.println("LED5 ON");
}
led6.off();
Serial.println("LED6 OFF");
}
The simulation is only using V5 in the ON position but you can have ON / OFF with minor mods.
Edit: I have just used the button in PUSH mode and I notice the timer count is forever increasing.
It stops at the maximum 16. Mod the code above to delete the running timer in PUSH mode.
Were you working in PUSH or SWITCH mode?
If you are working in PUSH mode comment out this line:
timer.disable(newTimer); // for switch mode only
and after:
Serial.println("LED6 OFF");
add the following:
timer.deleteTimer(newTimer); // for button in PUSH mode
Thank you !!! Already upload and testing !!! It over an hour and still perfect.
I’m running in Push mode
It working perfectly, . Thank you Costas !!