Hello, good night everyone, after reading this topic I was able to do a “timer countdown” I need to do something very similar but with “auto reset” that is to say that when it finishes everything is restarted, because what I need to do is put a button physical that activates a “relay” for 10 minutes, I do not need PAUSE I only need to START and be able to cut it at any time.
When the button is pressed, an LED lights up and activates RELAY (for 10 minutes)
If it is pressed again within 10 minutes, IT STOPS and RESETS ALL. (The LED OFF AND RELAY OFF timer returns to 0)
I would also pretend to be able to set the time to run (to be able to change the 10 minutes) - I have already done this.
I also want to synchronize it with the APP, so I can run it from the APP - I have already done this.
Thank you very much to all ! I’ve been with my smart house for 3 years now. I have control of all the exterior lights, water, electricity consumption, now I am working on this and the irrigation system!
Link original code: [SOLVED] How to countdown? - #6 by Jamin
My Code for this work:
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Wire.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
char auth[] = "";
#define W5100_CS 10
#define SDCARD_CS 4
BlynkTimer timer;
WidgetTerminal terminal(V73);
WidgetLED ledbomba(V72);
WidgetRTC rtc;
bool Connected2Blynk = false;
const int BombaAgua = 22;
const int ReflectorPatio = 24;
const int Presurizadora = 26;
const int BotonCuarto = 50;
const int ReflectorPorton = 53;
const int ReflectorTerreno = 43;
const int LuzCuarto = 47;
int EstadoReflectorPatio = LOW;
int EstadoPresurizadora = LOW;
int CountdownRemainReset;
int CountdownRemain;
int CountdownTimer;
bool isFirstConnect = false;
int EstadoLuzCuarto, EstadoBotonCuarto, EstadoReflectorPorton, EstadoReflectorTerreno;
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH);
pinMode(ReflectorPatio, OUTPUT);
pinMode(Presurizadora, OUTPUT);
pinMode(LuzCuarto, OUTPUT);
pinMode(ReflectorPorton, OUTPUT);
pinMode(ReflectorTerreno, OUTPUT);
pinMode(BotonCuarto, INPUT_PULLUP);
Blynk.begin(auth, IPAddress(192, 168, 1, 14), 8080);
rtc.begin();
terminal.clear();
terminal.println(F("Jarvis Home Iniciado V3"));
terminal.println(F("-------------"));
terminal.println(F("Blynk Version: " BLYNK_VERSION));
terminal.flush();
timer.setInterval(1400L, botonluzcuarto);
CountdownTimer = timer.setInterval(1000, CountdownTimerFunction);
timer.disable(CountdownTimer); // disable it on boot
}
void CountdownTimerFunction() {
CountdownRemain--; // remove 1 every second
CountdownShowFormatted(CountdownRemain);
if (!CountdownRemain) { // check if CountdownRemain == 0/FALSE/LOW
timer.disable(CountdownTimer); // if 0 stop timer
Blynk.virtualWrite(V20, LOW); // reset START/STOP button status
Blynk.virtualWrite(V21, "TIEMPO COMPLETO");
Blynk.virtualWrite(V22, 255); // LED for timer completed
Blynk.virtualWrite(V3, 1); // Timer LED status light off
digitalWrite(Presurizadora, HIGH);
Blynk.virtualWrite(V103, "Normal");
} else {
Blynk.virtualWrite(V22, 0); // LED for timer completed
}
}
// Button Widget (Switch Type): Start/Pause Timer
BLYNK_WRITE(V20) {
if (param.asInt()) {
if (CountdownRemain) { // check if there is a time set or not
timer.enable(CountdownTimer);
Blynk.virtualWrite(V3, 0); // Timer LED status light on
Blynk.virtualWrite(V103, "Presurizada Timer");
digitalWrite(Presurizadora, LOW);
} else {
Blynk.virtualWrite(V20, LOW); // if CountdownRemain is set to 0, then dont start hte timer.
Blynk.virtualWrite(V21, "SIN TIEMPO (RESETEAR)"); // if CountdownRemain is set to 0, then tell the user
}
} else {
timer.disable(CountdownTimer);
Blynk.virtualWrite(V3, 1); // Timer LED status light off
Blynk.virtualWrite(V103, "Normal Timer");
digitalWrite(Presurizadora, HIGH);
}
}
// Button Widget (Momentary): Reset Timer
BLYNK_WRITE(V24) {
CountdownRemain = CountdownRemainReset; // reset to original start time
}
// Slider Widget (60-180): Set Timer (mins)
BLYNK_WRITE(V25) {
if (timer.isEnabled(CountdownTimer)) { // only update if timer not running
Blynk.virtualWrite(V25, param.asInt() ); // if running, refuse to let use change slider
} else {
CountdownRemainReset = param.asInt() * 60 + 1; // + 1 set the timer to 1:00:00 instead of 00:59:59
CountdownRemain = param.asInt() * 60;
CountdownShowFormatted(CountdownRemain);
}
}
void CountdownShowFormatted(int seconds) {
long days = 0;
long hours = 0;
long mins = 0;
long secs = 0;
String secs_o = ":";
String mins_o = ":";
String hours_o = ":";
secs = seconds; // set the seconds remaining
mins = secs / 60; //convert seconds to minutes
hours = mins / 60; //convert minutes to hours
days = hours / 24; //convert hours to days
secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
if (secs < 10) {
secs_o = ":0";
}
if (mins < 10) {
mins_o = ":0";
}
if (hours < 10) {
hours_o = ":0";
}
Blynk.virtualWrite(V21, days + hours_o + hours + mins_o + mins + secs_o + secs);
}
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncAll();
isFirstConnect = false;
}
}
BLYNK_WRITE(V2) {
EstadoReflectorPatio = param.asInt();
digitalWrite(ReflectorPatio, EstadoReflectorPatio);
}
BLYNK_WRITE(V3) {
EstadoPresurizadora = param.asInt();
digitalWrite(Presurizadora, EstadoPresurizadora);
if (EstadoPresurizadora == 0) {
Blynk.virtualWrite(V103, "Presurizada");
}
else {
Blynk.virtualWrite(V103, "Normal");
}
}
BLYNK_WRITE(V4) {
EstadoLuzCuarto = param.asInt();
digitalWrite(LuzCuarto, EstadoLuzCuarto);
}
BLYNK_WRITE(V5) {
EstadoReflectorPorton = param.asInt();
digitalWrite(ReflectorPorton, EstadoReflectorPorton);
}
BLYNK_WRITE(V6) {
EstadoReflectorTerreno = param.asInt();
digitalWrite(ReflectorTerreno, EstadoReflectorTerreno);
}
void loop()
{
Blynk.run();
timer.run();
}
void botonluzcuarto()
{
if (digitalRead(BotonCuarto) == LOW) {
if (EstadoBotonCuarto != LOW) {
EstadoLuzCuarto = !EstadoLuzCuarto;
digitalWrite(LuzCuarto, EstadoLuzCuarto);
Blynk.virtualWrite(V4, EstadoLuzCuarto);
}
EstadoBotonCuarto = LOW;
} else {
EstadoBotonCuarto = HIGH;
}
}