Run between Hours

void automatic() {

 int Hora = hour();
 int TimeDay = 20;
 int TimeNight = 5;
 
Serial.print(String(Hora)+" "+String(TimeDay) + " "+ String(TimeNight));
  
 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");
    }
 }

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

[6757] Connecting to 192.168.1.14
[6889] Ready (ping: 8ms).
[7250] Time sync: OK
13 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 513 20 5

Ok
13 20 5
if (13>= 20 && 13 <= 5) {
Never match

And what is Blynk showing for V2, V5, V6 and V3 ?

How is this function being called?

Pete.

Its , 13 PM (TIME) - 20 (Time Set to START) - 5 (Time Set to STOP)

And what is Blynk showing for V2, V5, V6 and V3 ? -> Chage To 0 ALL TIME.

in Setup-> timer.setInterval(1000L, automatic);

Well, that’s what I’d expect when hora = 12

Pete.

See my edit post :v:

void automatic() {

 int Hora = hour();
 int TimeDay = 5;
 int TimeNight = 20;
 
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");
    }
 }

So if Hora = 21 (9pm) it IS greater than TimeDay, but IS NOT less than TimeNight. So V2, V5, V6, V3 are all set to 1 (if statement is false).

If Hora = 1 (1am), it IS NOT greater than TimeDay, but it IS less than TimeNight. So they are still all set to 1 (if statement is false).

Once Hora = 6 (6am), it IS greater than TimeDay, and IS less than TimeNight, So V2, V5, V6, V3 are all set to 0 (if statement is true). This would be the case until Hora = 20 (8pm) .

For this you would need to add a flag so that it only runs the one time as needed.

Add in the flag (along with some flag setting stuff), some Serial Print statements to help know where you are at, and you have some untested code that may do what you need (or may not) …

void automatic() {

 int Hora = hour();
 int TimeDay = 5;
 int TimeNight = 20;
 static int Flag = 0;
 
Serial.println(" Hora is:"+ String(Hora)+"  "+"TimeDay is: "+String(TimeDay) + "  "+ "TimeNight is: "+ String(TimeNight));
  
 if (Hora >= TimeDay && Hora <= TimeNight && Flag == 0)
{
  Serial.println(" Hora is between 6AM and 7PM" );

  Blynk.virtualWrite (V2, "0");

  Blynk.virtualWrite (V5, "0");
  Blynk.virtualWrite (V6, "0");
  Blynk.virtualWrite (V3, "0");
  Flag = 1;
     } 
else if ((Hora <TimeDay || Hora >TimeNight) 
{
 if (Flag == 1)
{
   Serial.println(" Hora is between 8PM and 5AM" );
  Blynk.virtualWrite (V3, "1");
  Blynk.virtualWrite (V6, "1");
  Blynk.virtualWrite (V5, "1");
  Blynk.virtualWrite (V2, "1");
  Flag = 0;
    }
   }
else{
Flag = 1;
}
}
1 Like

Thx mate.

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

[6757] Connecting to 192.168.1.14
[6918] Ready (ping: 12ms).
[7279] Time sync: OK
 Hora is between 8PM and 5AM
 Hora is between 8PM and 5AM
 Hora is between 8PM and 5AM

Keep repeating…

And Serial.println(" Hora is:" String(Hora) + " " + "TimeDay is: " String(TimeDay) + " " + "TimeNight is: " String(TimeNight));

Show me error

JarvisHome_New:228:30: error: expected ')' before 'String'

I edited the code above. Hopefully it works. I edited it on my phone.

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

[6757] Connecting to 192.168.1.14
[6900] Ready (ping: 7ms).
[7280] Time sync: OK
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 6AM and 7PM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 8PM and 5AM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 6AM and 7PM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 8PM and 5AM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 6AM and 7PM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 8PM and 5AM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 6AM and 7PM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 8PM and 5AM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 6AM and 7PM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 8PM and 5AM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 6AM and 7PM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 8PM and 5AM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 6AM and 7PM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 8PM and 5AM
 Hora is:23  TimeDay is: 5  TimeNight is: 23
 Hora is between 6AM and 7PM

Try it now. Edited it again.
If not I’ll look tomorrow when im at my computer.

i think the real problems its 24 hour format…

When its 20 = 8 PM

if transform to this 20 > 0 , and dont work.

OK, so after a little fiddling and head scratching I think I got something. Although I didn’t run it through every scenario. The tricky part was making sure it ran correctly no matter what time you booted the device.

To make sure it runs correctly if booted up at any time, I had to add a check of the current Hour at boot. This check had to compare the current Hour at boot against the TimeDay and TimeNight settings, and set the flag accordingly. So all of these variables needed to be global. Also, since you are only concerned with the hour, we can change the timer interval to 1 minute. As checking every second is unnecessary (although may be helpful if you are testing. I set up a counter that incremented Hora from 0-23, and it seemed to work as intended).

Getting the TimeDay and Time Night parameters to be configurable in BLYNK will take a little work, but should be possible. If this is your end goal, please share the results.

The Time Input Widget may be an alternate choice here. Although, that one can be a bit confusing as well.

int Hora = 0; //will be set to current hour at boot
int Flag = 0; // will be adjusted once current hour is established
int TimeDay = 5; //0=12AM - 23=11PM (24Hr Format)
int TimeNight = 20; //0=12AM - 23=11PM (24Hr Format)

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

//OTHER STUFF


timer.setTimeout(1000L,setFlag); //establishes current hour and sets the flag accordingly
}

void setFlag()
{
 Hora = hour(); 
 Serial.println(" Hora AT START_UP is:"+ String(Hora));
 if (Hora > TimeDay && Hora < TimeNight)
 {
  Flag = 0;
 }
 else
 {
  Flag = 1;
 }
timer.setInterval(60000L,automatic); //starts the check
 
}

void automatic() {

 Hora = hour();
Serial.println(" Hora is:"+ String(Hora)+"  "+"TimeDay is: "+String(TimeDay) + "  "+ "TimeNight is: "+ String(TimeNight));

 Serial.println(" Flag is:"+ String(Flag));
  
 if (Flag == 0)
{
  if (Hora > TimeDay && Hora < TimeNight)
  {
  Serial.println(" Hora is between 6AM and 7PM" );
  Serial.println(" Hora is:"+ String(Hora));
  Blynk.virtualWrite (V2, "0");
  Blynk.virtualWrite (V5, "0");
  Blynk.virtualWrite (V6, "0");
  Blynk.virtualWrite (V3, "0");
  Flag = 1;
   } 
}
else if (Flag == 1) 
{
 if (Hora <= TimeDay || Hora >= TimeNight)
  {
   Serial.println(" Hora is between 8PM and 5AM" );
   Serial.println(" Hora is:"+ String(Hora));
  Blynk.virtualWrite (V6, "1");
  Blynk.virtualWrite (V5, "1");
  Blynk.virtualWrite (V2, "1");
  Blynk.virtualWrite (V3, "1");
  Flag = 0;
  }

}

}

Thx for ur time.

Check ->

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

[6757] Connecting to 192.168.1.14
[6821] Ready (ping: 8ms).
[7182] Time sync: OK
 Hora AT START_UP is:15
 Hora is:15  TimeDay is: 15  TimeNight is: 16
 Flag is:1
 Hora is between 8PM and 5AM
 Hora is:15
 Hora is:15  TimeDay is: 15  TimeNight is: 16
 Flag is:0
 Hora is:15  TimeDay is: 15  TimeNight is: 16
 Flag is:0
 Hora is:15  TimeDay is: 15  TimeNight is: 16
 Flag is:0
15:58:58.801 ->  Hora is:15  TimeDay is: 15  TimeNight is: 16
15:58:58.836 ->  Flag is:0
15:58:58.836 -> [308395] Time sync: OK
15:59:58.747 ->  Hora is:15  TimeDay is: 15  TimeNight is: 16
15:59:58.782 ->  Flag is:0
16:00:58.762 ->  Hora is:16  TimeDay is: 15  TimeNight is: 16
16:00:58.797 ->  Flag is:0
16:01:58.752 ->  Hora is:16  TimeDay is: 15  TimeNight is: 16
16:01:58.820 ->  Flag is:0

and this is full code:

#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;

int Hora = 0; //will be set to current hour at boot
int Flag = 0; // will be adjusted once current hour is established
int TimeDay = 15; //0=12AM - 23=11PM (24Hr Format)
int TimeNight = 16; //0=12AM - 23=11PM (24Hr Format)

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(300);
  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);
  timer.setTimeout(1000L, setFlag);

}

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

void setFlag()
{
  Hora = hour();
  Serial.println(" Hora AT START_UP is:" + String(Hora));
  if (Hora > TimeDay && Hora < TimeNight)
  {
    Flag = 0;
  }
  else
  {
    Flag = 1;
  }
  timer.setInterval(60000L, automatic); //starts the check

}
void automatic() {

  Hora = hour();
  Serial.println(" Hora is:" + String(Hora) + "  " + "TimeDay is: " + String(TimeDay) + "  " + "TimeNight is: " + String(TimeNight));

  Serial.println(" Flag is:" + String(Flag));

  if (Flag == 0)
  {
    if (Hora > TimeDay && Hora < TimeNight)
    {
      Serial.println(" Hora is between 6AM and 7PM" );
      Serial.println(" Hora is:" + String(Hora));
      Blynk.virtualWrite (V2, "0");
      Blynk.virtualWrite (V5, "0");
      Blynk.virtualWrite (V6, "0");
      Blynk.virtualWrite (V3, "0");
      Flag = 1;
    }
  }
  else if (Flag == 1)
  {
    if (Hora <= TimeDay || Hora >= TimeNight)
    {
      Serial.println(" Hora is between 8PM and 5AM" );
      Serial.println(" Hora is:" + String(Hora));
      Blynk.virtualWrite (V6, "1");
      Blynk.virtualWrite (V5, "1");
      Blynk.virtualWrite (V2, "1");
      Blynk.virtualWrite (V3, "1");
      Flag = 0;
    }

  }

}

This ->

 Hora AT START_UP is:15
 Hora is:15  TimeDay is: 15  TimeNight is: 16
 Flag is:1
 Hora is between 8PM and 5AM

Are perfect but when its Hora = 16 , dont show

Serial.println(" Hora is between 8PM and 5AM" );

Dont put Virtual Pins to 1

You need >= and <=

I was under the impression it was to only set then to 1 once. That is when it turn to the TimeDay.

So if TimeDay is 15, when Hora becomes 15 it will set the Virtual Pins to 1, ONCE. When Hora becomes Greater than TimeNight (17 in this case), it will set the virtual pins to 0, ONCE.

the >= and <= are there, but only in one of the conditions. Otherwise, it would be true for both cases.

1 Like

Yes, but it does not. When it is 15, this is executed Serial.println(" Hora is between 6AM and 7PM" ); - then when it is 16 it does nothing, they remain at 0