Arduino Time Library

Okey, now I figured it out(and with help from you), atleast most of the code I think...
But the remaining problem is I can't figure out how to set the time right now + 5 minutes as a statement, I tried this as in code below but that was only active for 3minutes and 20seconds :confused:

Some advices?

Thanks

void loop(void)
{
  displayTime(); // displays the time on the monitor
  delay(1000); // 1 sec delay
  
  time_t starttime = now(); 

  if (RTC.alarm(ALARM_1) || RTC.alarm(ALARM_2))
  {

    while (now() < starttime + 5* minute()) <------<------<---- PROBLEM HERE!!
    {
      digitalWrite(13, HIGH);
    }
    digitalWrite(13, LOW);

  }//end loop
    while (now() < starttime + 5* minute()) <------<------<---- PROBLEM HERE!!

minute() is the current minute.
Did you have a problem with the predefined constant I posted, SECONDS_PER_MINUTE?
Ah, my bad, it's SECS_PER_MIN .

Have a look at Time.h and you will see that it has a lot of useful constants and macros for time calculations.

Ah okey! Thank you, will test that tomorrow :slight_smile: Will keep you updated :slight_smile:

Thanks!

I've had good results with these, see reply #3, good luck.

http://forum.arduino.cc/index.php?topic=372427.msg2569333#msg2569333

What is the difference between Time lib's "time_t" and adafruit's RTClib "DateTime"?

If I want to change the timezone, is that the timelibrary that needs to be modified or do I have to include another library? I want 24h format and central europe timezone :slight_smile:

Thank you!

i googled "Arduino Timezone" and I got this, Jack Christensen's terrific timezone library as the top return.

Do anyone have any clue how to use TimeZone library it in my project? I've tried but can't get it working...
How can I actually programme the DS3231 chip to keep the time with some help from the battery with the right timezone, I have +1 UTC? Yes I include the timezone library but can't figure out how to attach it to my project in a properly way :confused:

Code I got so far, work good for the other features that I want, just timezone and the external interrupts that doesn't work, which means I want it to go to sleep and when the alarm triggers, it will wake the Arduino up, same if I push a button and the go back to sleep. Any ideas? :slight_smile:

Thank you!

#include <DS3232RTC.h>
#include <Streaming.h>
#include <time.h>
#include <Wire.h>
#include <LowPower.h>
#include <Timezone.h>

#define SQW_PIN 2
#define DS3231_I2C_ADDRESS 0x68

volatile boolean alarmIsrWasCalled = false;

int button_pin = 3;
int fan_power_pin = 9;
int wv_power_pin = 10;
int controller_pin = 13; //Starts to indicate that the system is running

void setup(void)
{
  pinMode(button_pin, INPUT); //BUTTON PRESSED
  digitalWrite(button_pin, HIGH); //
  pinMode(fan_power_pin, OUTPUT); //Pin to power the PC fan
  digitalWrite(fan_power_pin, LOW);
  pinMode(wv_power_pin, OUTPUT); //Pin to power the water valve
  digitalWrite(wv_power_pin, LOW);
  pinMode(controller_pin, OUTPUT); //Starts to indicate that the system is running
  digitalWrite(controller_pin, LOW);

  Wire.begin();
  Serial.begin(115200);
  setSyncProvider(RTC.get);
  Serial << "RTC Sync";
  if (timeStatus() != timeSet)
  {
    Serial << " FAIL!";
  }
  Serial << endl;

  RTC.squareWave(SQWAVE_NONE);
  pinMode(SQW_PIN, INPUT_PULLUP);
  attachInterrupt(INT0, alarmIsr, FALLING);

  //Set an alarm at every 20th second of every minute.
  RTC.setAlarm(ALM1_MATCH_HOURS, 0, 34, 2, 1);
  RTC.alarm(ALARM_1);
  RTC.alarmInterrupt(ALARM_1, true);

  //Set an alarm every minute.
  RTC.setAlarm(ALM2_MATCH_HOURS, 0, 1, 0, 1);
  RTC.alarm(ALARM_2);
  RTC.alarmInterrupt(ALARM_2, true);
}
void alarmIsr()
{
  alarmIsrWasCalled = true;
}

void loop(void)
{
  displayTime();
  delay(1000);

  time_t starttid = now(); //Variabel med den aktuella tiden då detta körs
  bool alarmActive = false; //Variabel för att säga till att alarmet inte är aktiverat
  bool RTCAlarmActive = false;

  if (RTC.alarm(ALARM_1) || RTC.alarm(ALARM_2))
  {
    RTCAlarmActive = true;
    while (now() < starttid + 5 * SECS_PER_MIN)
    {
      digitalWrite(wv_power_pin, HIGH);
      digitalWrite(controller_pin, HIGH);
    }
    digitalWrite(wv_power_pin, LOW);
    digitalWrite(controller_pin, LOW);
    RTCAlarmActive = false;
  }

  if (digitalRead(3) == LOW && RTCAlarmActive == false)
  {
    //IF BUTTON WAS PRESSED, THEN WATER FOR 3 MINUTES INSTEAD OF 5 AS WITH ALARM
    // Ena stiftet p knappen till ground och andra stiftet till digital 2(interrupt Pin)
    while (now() < starttid + 3 * SECS_PER_MIN)
    {
      digitalWrite(wv_power_pin, HIGH);
      digitalWrite(controller_pin, HIGH);
    }
    digitalWrite(wv_power_pin, LOW);
    digitalWrite(controller_pin, LOW);
  }
  //digitalWrite(rtc_power_pin, LOW); //if this line is commented it works as expected.
  //LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
}


///////////////////////////////////////////////////////////
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}

void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute < 10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}

Anyone? :slight_smile:

Thanks!

Jack has included some great examples on working with the library!