Little help with code

Hi, i have a function that checks temp and then turns ON pump if temp is low.
Now i’d like to disable pump activation if time is between 21.30 and 23.30
Somehow i can’t figure out any sensible way to accomplish this :frowning:

Libraries i use (timealarms in relevant):

#include "TimeAlarms/TimeAlarms.h"

#include "analogSmooth.h"

#include "OneWire/OneWire.h"

#include "LiquidCrystal_I2C.h"

And my function that needs to be modified:

[code]int tempkontroll(float radikaC, float toaC, int c) {
if(toaC < 20.2 && radikaC > 30 && c == 0) {

        c = 1;
        digitalWrite(relayRadiator, HIGH); //paneb relee "HIGH" asendisse
    
}

else if((toaC > 21 || radikaC < 25) && c == 1) {
    c = 0;
    digitalWrite(relayRadiator, LOW); //paneb relee "LOW" asendisse
} 
return c;
}[/code]

Here is something i created a while ago.. you might find useful

1 Like

If precission is not of high priority for you, you could easily use the Core standard Time object.
See http://docs.spark.io/firmware/#libraries-time

Time.hour() + Time.minute() is all the precesion i need, but due to lack of my programming skills i can’t figure out how to make relay not to switch if time is between 21.30 and 23.30

Also due to lack of my English skill i haven’t been able to find any similar arduino examples.

Hi @qwerty009,

I just wrote up a quick time demo, you could use something like the ‘alarm’ variable, and the if check in this for your project. This demo sets off the alarm anytime between 8:05am and 9am in the US Central time zone:

#define ONE_DAY_MILLIS (24 * 60 * 60 * 1000)
unsigned long lastSync = millis();

bool alarm = false;
bool snooze = false;

void setup() {
    Serial.begin(115200);
    
    // Set time zone to Central USA daylight saving time
    Time.zone(-6);

}

void loop() {
    
  if (millis() - lastSync > ONE_DAY_MILLIS) {
    // Request time synchronization from the Spark Cloud
    Spark.syncTime();
    lastSync = millis();
  }
  
  
  //do something special at a particular time:
  
  if (Time.minute() == 0) {
      Serial.println("**Chime Sounds**");
  }
  
  if (!snooze && (Time.hour() == 8) && (Time.minute() >= 5)) {
      alarm = true;
      Serial.println("The alarm is going off!");
  }
  
  
  
  Serial.println("The time is " + String(Time.hour()) + ":" + String(Time.minute()));
  delay(1000);
}

Thanks,
David

1 Like

@Dave 's sample code does give you some good clues.

But since you said that your programming skills may not suffice, I'd like to elaborate a bit more

This part checks if the last synchronisation time (lastSync) lies more than 24 hours before the current timestamp (millis()) and if so, it will re-synchronize with the cloud timebase. After a long free running time the Cores clock will drift off slightly otherwise.

This means every time when the minute part of time is equal to zero (every full hour) something special will be done (e.g. printing a message via the USB serial port).

And for your particular question about the time between 21:30 and 23:30 you'd write this

  // count of minutes for current day (0..1439)
  int minuteOfDay = Time.hour()*60 + Time.minute(); 

  // check if minuteOfDay lies between 21:30 AND 23:30
  if ( (21*60 + 30) <= minuteOfDay && minuteOfDay <= (23*60 + 30) )
  {
    doQuietTimeAction();
  } 
  else
  {
    doAllTheOtherTime();
  }

I hope this makes things clearer and if you got any other questions, just ask :wink:

2 Likes