DS1307 RTC Useage
Moderators: adafruit_support_bill, adafruit

DS1307 RTC Useage

by KaGe_Diver on Mon Feb 04, 2013 12:22 pm

Hello, I am fairly new to Arduino and C++ programming, and I apologize if this is a repeat post. I searched and searched and could not find anything that would help with my current problem.

I have an Arduino UNO R3 and I am trying to set up a RTC DS1307 (with the I2C comm protocol) to take reading every 5 min of from a analog temp sensor. The RTC works perfectly! I am just unsure what commands I need to use to get the full potential out of the RTC.

Below is the current code I am using.
Thank you in advance for your help.

Code: Select all | TOGGLE FULL SIZE
// Date and time functions using just software, based on millis() & timer

#include <Wire.h>
#include "RTClib.h"

const int temp = 0;
const int LED = 13;
const int MIN = 0'

RTC_Millis RTC;

void setup ()
{
 
    Serial.begin(9600);
    pinMode(LED,OUTPUT);
    RTC.begin(DateTime(__DATE__, __TIME__)); //sets the RTC to the date & time this sketch was compiled

}

void loop ()
{

  float volt, degC;
  int room, deadband, setpoint, degF, dispHour, dispDOW;
 
  volt = getVoltage(temp);
  degC = (volt - .5) * 100.0;
  degF = degC * (9.0/5.0) + 32.0;
  room = 70;
  deadband = 5;
  setpoint = room - deadband;
 
 
    DateTime now = RTC.now();
      dispHour = now.hour();
      dispDOW = now.dayOfWeek();
     
  if(now.minute == MIN   
      // RTD Day Of the Week
    if(dispDOW == 0)
      Serial.print("SUN: ");
    if(dispDOW == 1)
      Serial.print("MON: ");
    if(dispDOW == 2)
      Serial.print("TUE: ");
    if(dispDOW == 3)
      Serial.print("WED: ");
    if(dispDOW == 4)
      Serial.print("THU: ");
    if(dispDOW == 5)
      Serial.print("FRI: ");
    if(dispDOW == 6)
      Serial.print("SAT: ");
     
      // RTD Date format D/M/TYYY   
    Serial.print(now.month());
    Serial.print('/');
    Serial.print(now.day());
    Serial.print('/');
    Serial.print(now.year() - 2000);
    Serial.print(' ');
   
     // RTC Time in 12 hour format with AM/PM
    Serial.print((dispHour > 12) ? dispHour - 12 : ((dispHour == 12) ? 12 : dispHour));
    Serial.print(":");
    if (now.minute() < 10)
         Serial.print("0");
    Serial.print(now.minute());
    Serial.print(':');
    if (now.second() < 10)
         Serial.print ("0");
    Serial.print(now.second());
    Serial.print((dispHour<12) ? " AM" : " PM");
    Serial.println();
   
              // Temp Display in Deg F
      Serial.print("  Temp:  "); // Temp will be in a format of
      Serial.println(degF);         // ##.#
     
   
       
   delay(1000);   // next step try to use the RTC to take readings every 5 min.

   
   
   
    if (degF <= setpoint)
  {
    digitalWrite(LED,HIGH);
  }
    if (degF > room)
  {
    digitalWrite(LED,LOW);
  }
   
}

float getVoltage(int pin)
{
  return (analogRead(pin) * 0.004882814);
}
KaGe_Diver
 
Posts: 1
Joined: Mon Feb 04, 2013 12:05 pm

Re: DS1307 RTC Useage

by adafruit_support_bill on Mon Feb 04, 2013 1:01 pm

Typically the RTC is used to set the system time at startup and periodically re-synchronized for continued accuracy. You can schedule periodic tasks by polling the RTC, but it is usually better to use one of the many libraries designed for the purpose. There are several over at http://playground.arduino.cc . The Simple Timer Library should work for your application: http://playground.arduino.cc//Code/SimpleTimer
User avatar
adafruit_support_bill
 
Posts: 30753
Joined: Sat Feb 07, 2009 10:11 am