The extra parenthesis was finger trouble, and my last attempt before calling it a night…
I think I’m missing something as I’ve taken snippets from different parts of the post so here is the complete sketch.
It compiles fine, however I would like the relays_off timer here:
timer.setTimeout(10000, relays_OFF); // delay 10sec (10000) from first action and turn off
to be dynamic using the slider widget. Currently its fixed at 10secs
I’ve been experimenting with a Countdown variable but hasn’t been working.
BLYNK_WRITE(V0){ // add a slider on V0.
The complete sketch
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <Blynk.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXX";
char pass[] = "XXXXXXXXXX";
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXXXXXX";
// Set your LED and physical button pins here
// const int ledPin = 0;
// const int relay2 = 0; // Currently not used
const int btnPin = 2;
const int relay1 = 0;
SimpleTimer timer;
void checkPhysicalButton();
void actionRelays();
void relay1_ON();
void relay2_ON();
void relays_OFF();
int ledState = LOW;
int btnState = LOW;
// int relay1 = LOW;
int relay2 = LOW;
int Countdown; // Stores Slider Widget Value
int Counter = 0; // Set counter to 0. Use to count # of runs
int CountdownValue; // Variable Used for Relay Timer
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
// Blynk.syncAll();
// Alternatively, you could override server state using:
//Blynk.virtualWrite(V14, ledState);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V14) {
ledState = param.asInt();
// digitalWrite(relay1, ledState);
Serial.println("App Button pressed");
actionRelays();
}
void checkPhysicalButton()
{
if (digitalRead(btnPin) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState != LOW) {
// Toggle LED state
ledState = !ledState;
Serial.println("Phyical Button pressed");
actionRelays();
// Update Button App
Serial.println("Updated App Button status");
Blynk.virtualWrite(V14, ledState);
}
btnState = LOW;
} else {
btnState = HIGH;
}
}
BLYNK_WRITE(V0){ // add a slider on V0.
Countdown = param.asInt(); // set variable from Slider Widget as countdown, that can be then used in the actioRelays() setTimeout
Serial.print("V0 Slider value is: ");
Serial.println(Countdown);
CountdownValue=(Countdown*60000); // Converst the value milliseconds
Serial.print("CountdownValue will be: ");
Serial.println(CountdownValue);
}
void actionRelays(){
relay1_ON(); // turn on relay1
timer.setTimeout(2000, relay2_ON); // delay 2sec then turn on relay2
timer.setTimeout(10000, relays_OFF); // delay 10sec (10000) from first action and turn off both relays
}
void relay1_ON(){
Serial.println("relay1 on");
digitalWrite(relay1,HIGH); // turn on relay1
Blynk.virtualWrite(V18, 255); // turn on an LED widget (optional)
}
void relay2_ON(){
Serial.println("relay2 on");
digitalWrite(relay2,HIGH); // turn on relay2
Blynk.virtualWrite(V20, 255); // turn on an LED widget (optional)
}
void relays_OFF(){
Serial.println("relays off");
digitalWrite(relay1,LOW); // turn off relay1
digitalWrite(relay2,LOW); // turn off relay2
Blynk.virtualWrite(V18, 0); // turn off an LED widget (optional)
Blynk.virtualWrite(V20, 0); // turn off an LED widget (optional)
Blynk.virtualWrite(V14, 0); // turn off relay1 Button App
Counter++; // Increment the run counter by 1
Blynk.virtualWrite(V16, Counter);
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(relay1, OUTPUT);
digitalWrite(relay1, LOW); // ensure relay is OFF on reboot
pinMode(btnPin, INPUT_PULLUP);
digitalWrite(relay1, ledState);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
}
void loop()
{
Blynk.run();
timer.run();
}