BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Run between Hours

Hello Blynk :heart: users

I am having a LOGICAL doubt, I need to try to “take out the event system” to make it 100% automatic to my system that does not depend on anything.

The idea is to have an IF change between certain “Hours” to change state to a few pins & after those “Hours” only turn them off once.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <DHT.h>
#include <Wire.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

#define DHTPIN 23
#define DHTTYPE DHT11     // DHT 11

char auth[] = "";

#define W5100_CS  10
#define SDCARD_CS 4

BlynkTimer timer;

WidgetTerminal terminal(V73);

WidgetLED ledbomba(V72);

DHT dht(DHTPIN, DHTTYPE);

WidgetRTC rtc;

const int BombaAgua = 30;
const int ReflectorPatio = 32;
const int Presurizadora = 34;
const int SensorPorton = 38;
const int BotonCuarto = 40;

const int ReflectorPorton = 31;
const int ReflectorTerreno = 33;
const int AperturaPorton = 35;
const int LuzCuarto = 37;

int EstadoBombaAgua = LOW;
int EstadoReflectorPatio = LOW;
int EstadoReflectorPorton = LOW;
int EstadoReflectorTerreno = LOW;
int EstadoPresurizadora = LOW;

bool isFirstConnect = true;

int EstadoLuzCuarto, EstadoBotonCuarto, EstadoAperturaPorton, EstadoSensorPorton;

void setup()
{
  Serial.begin(9600);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);

  Blynk.begin(auth, IPAddress(192, 168, 1, 14), 8080);

  pinMode(BombaAgua, OUTPUT);
  pinMode(ReflectorPatio, OUTPUT);
  pinMode(Presurizadora, OUTPUT);

  pinMode(LuzCuarto, OUTPUT);
  pinMode(ReflectorPorton, OUTPUT);
  pinMode(ReflectorTerreno, OUTPUT);
  pinMode(AperturaPorton, OUTPUT);

  pinMode(BotonCuarto, INPUT_PULLUP);
  pinMode(SensorPorton, INPUT_PULLUP);


  dht.begin();
  rtc.begin();
  setSyncInterval(10 * 60);
  terminal.clear();
  terminal.println(F("Jarvis Home Iniciado V2"));
  terminal.println(F("-------------"));
  terminal.println(F("Blynk Version: " BLYNK_VERSION));
  terminal.flush();

  timer.setInterval(1000L, botonluzcuarto);
  //timer.setInterval(10000L, temperaturayhumedad);
  timer.setInterval(10000L, checkconnect);
  timer.setInterval(1000L, sensorporton);

}

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

BLYNK_CONNECTED() {

  if (isFirstConnect) {
Blynk.syncAll();
isFirstConnect = false;
  }
}

BLYNK_WRITE(V1) {
  EstadoBombaAgua = param.asInt();
  digitalWrite(BombaAgua, EstadoBombaAgua);

  if (EstadoBombaAgua == 0) {
ledbomba.on();
terminal.println(F("Bomba Encendida"));
terminal.flush();
  }
  else {
ledbomba.off();
terminal.println(F("Bomba Apagada"));
terminal.flush();
  }
}

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);
}

BLYNK_WRITE(V7) {
  EstadoAperturaPorton = param.asInt();
  digitalWrite(AperturaPorton, EstadoAperturaPorton);
}


void botonluzcuarto()
{
  if (digitalRead(BotonCuarto) == LOW) {
if (EstadoBotonCuarto != LOW) {
  EstadoLuzCuarto = !EstadoLuzCuarto;
  digitalWrite(LuzCuarto, EstadoLuzCuarto);
  Blynk.virtualWrite(V4, EstadoLuzCuarto);
}
EstadoBotonCuarto = LOW;
  } else {
EstadoBotonCuarto = HIGH;
  }
}

void temperaturayhumedad()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + "/" + month() + "/" + year();
terminal.println("Error Temperatura Cuarto Kevin: ");
terminal.print(currentTime);
terminal.print(" ");
terminal.print(currentDate);
terminal.println();
terminal.flush();
return;
  }
  Blynk.virtualWrite(V10, h);
  Blynk.virtualWrite(V11, t);

}
void checkconnect()
{
  bool result = Blynk.connected();

  if (result == false) {
Blynk.connect();

  }
}

void sensorporton() {

  EstadoSensorPorton = digitalRead(SensorPorton);
  if (EstadoSensorPorton == 1)
  {
Blynk.virtualWrite(V8, "Abierto");
  } else {
Blynk.virtualWrite(V8, "Cerrado");
  }
}

But I am having problems in that, like that nothing is executed at all …

Something in my logic goes wrong.

Thank you so much.

It’s impossible to say without knowing what the time, TimeDay and TimeNight variables contain.

You don’t appear to be using the hour value at all in this logic.

I assume that the umlaut over the e in else is a typo?

Pete.

1 Like

Thx for response.

"ëlse" // error in POST , not in code

int hora = hour (); // This RTC
TimeDay = 5;
TimeNight = 20;

i edit firts post with changes , BAD TYPES in translate

Your code seems ok.
but why don’t you use time widget ?

The problem its not Inputed time or widget the problem is

“IF” not working I think if I arm it badly, it doesn’t work.

yes of cause, hora = time()
but time is not hour()
time is a string like 12:45:36:51
Maybe I’m wrong ?

int hora = hour ();

Fixed , when i transfer code to here change all but now i change ALL…

The problem its LOGIC of if i think.

Do you have an issue ? or it doesn’t work ?

Not working - All time put al Virtual Pins to 1.

Example:

hora = 10
TimeDay = 10
TimeNight = 20

When HORA = 10 , if put al Vpins to 0 , but when its 11 put al Vpins to 1.

When the “hora” is between these 2 variables, all Vpins set 0 - when HORA more than “TimeNight” whe need set 1 TIME all Vpins to 1

first I don’t see any int TimeDay and int TimeNight

void automatic ignition () ---< missing void
   {
     int hora = hour ();
     if (hora > = TimeDay && hora <= TimeNight) {
 Blynk.virtualWrite (V2, "0");
 Blynk.virtualWrite (V5, "0");
 Blynk.virtualWrite (V6, "0");
 Blynk.virtualWrite (V3, "0");
    } else {
 Blynk.virtualWrite (V3, "1");
 Blynk.virtualWrite (V6, "1");
 Blynk.virtualWrite (V5, "1");
 Blynk.virtualWrite (V2, "1");
   }
} ---< missing bracket

i set in TOP the PROBLEM ITS IF not other thing…

But void is missing ?
add Serial.print(String(hora)+String(TimeDay) + String(TimeNight));
after if and else to see what is the result in your serial monitor

I find this post very confusing. I think you need to rewrite it so that we can understand exactly what you’re saying.

It would also be very helpful if you could copy and paste your EXACT code.

The easiest way to debug ‘if’ statements is to add some debug serial print statements so that you can see what the values of the variables are before the if statement, and so that you can see the program flow within the if statement.

Pete.

3 Likes

it’s what I said Pete :rofl:
And there is a lot of typo in his code, so it’s confusing too.

2 Likes

Check my code in firts POST i updated all.

Now to be clearer.

I need between 8:00 p.m. and 5:00 a.m.

Virtual pins 2, 3, 4 and 5 are set to 1 and after that time they are 0 but not constantly but only once to be able to activate them manually.

i think i need at this code.

void automatic() {

     int hour = hour();
     int TimeDay = 5 (I put with app i know this)
     int TimeNight = 20 (i put with app i know this)
     
     if (hour> = TimeDay && hour <= TimeNight) {
      Blynk.virtualWrite (V2, "0");
      Blynk.virtualWrite (V5, "0");
      Blynk.virtualWrite (V6, "0");
      Blynk.virtualWrite (V3, "0");
         } else {
      Blynk.virtualWrite (V3, "1");
      Blynk.virtualWrite (V6, "1");
      Blynk.virtualWrite (V5, "1");
      Blynk.virtualWrite (V2, "1");
        }
     }

But the logic of the IF I think is not right.

and I don’t know how to do it to run only once, to be able to ACTIVATE them manually after the set time

i know too i need add timer.setInterval(1000L, automatic); to setup.

And what happens if you add a serial print to show the value of hour?

Pete.

It shows me the exact time of the moment without minutes or seconds.

and as I use RTC I get the time of the Application, therefore everything should be fine.

Copy and paste your serial output, and the code with the serial print statement in it.

Pete.

i add this to my code

void automatic() {

     int Hora = hour();
     int TimeDay = 5;
     int TimeNight = 20;
     
     if (Hora >= TimeDay && Hora <= TimeNight) {
      Serial.println(Hora);
      Blynk.virtualWrite (V2, "0");
      Blynk.virtualWrite (V5, "0");
      Blynk.virtualWrite (V6, "0");
      Blynk.virtualWrite (V3, "0");
         } else {
      Blynk.virtualWrite (V3, "1");
      Blynk.virtualWrite (V6, "1");
      Blynk.virtualWrite (V5, "1");
      Blynk.virtualWrite (V2, "1");
        }
     }

And Serial Print showme this.

0] Getting IP...
[6639] IP:192.168.1.6
[6640] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[6757] Connecting to 192.168.1.14
[6806] Ready (ping: 6ms).
[7165] Time sync: OK
12
12
12
12
12
12
12
12
12
12
12
12
12
12

You need to print the 3 values as I said.
Serial.print(String(hora)+" "+String(TimeDay) + " "+ String(TimeNight));