DS1307 Real Time Clock:

Hello everyone,

I am a beginner to coding with an Arduino (and this forum), and I am seeking help. I recently purchased a DS1307 real time clock from Adafruit.com and have been following the instructions for getting the clock to work. Here's the link to what instructions I have been following: Understanding the Code | DS1307 Real Time Clock Breakout Board Kit | Adafruit Learning System

What I am doing, is trying to build a clock using this device. I want to pull the date, the hours, and the minutes and have corresponding LED's light up depending on the time. I have the clock up and working, but I am unsure of what code to use to specify "if the month is November, then this group of LED's light up", or "if the hour is 1pm, then this LED should light up", and so on so forth.

Like I mentioned, I am using the DS1307 real time clock with an Arduino Mega 2560. So far the only code I have is for having the real time clock read the correct time which can be seen in the serial monitor:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

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


RTC_DS1307 rtc;

void setup () {
   
  Serial.begin(57600);
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    
    Serial.println();
    delay(1000);
}

If anyone can help, I would greatly appreciate it! Thanks!

My next step would be to get the LEDs to display some sort of number (eg. 42). That gets the LED part working. Then connect up the clock part to the display part.

Thanks for replying, Nick!

I actually am not using any sort of LED display part. I am using individual LED lights to represent certain times. Therefore, I cannot really get anything to display a number (such as 42). I am unsure of what code is needed to pull from the data from the real time clock to tell the LED to light up when I need it to.

In theory I want to say something like If the real time clock is at 3:00pm, I want the LED in pin 13 to light up. Does that make sense? I'm trying to find out how to code something along those lines but am unsure how to write the code for the DS1307 real time clock to understand.

Thanks!

Code like:

  if (now.hour() == 13 && now.minute() == 0)
    digitalWrite (13, HIGH) ;
  else
    digitalWrite (13, LOW) ;

You use conditions based on the values of the fields of the DateTime "now"...

Of course you want to run update this DateTime with a call to rtc.now() on a regular
basis, perhaps about once a second (which can be done using the sort of technique
in the BlinkWithoutDelay example.

Mega 2560 and DS1307 code in this post as attachment:
http://forum.arduino.cc/index.php?topic=184712.msg1367731#msg1367731

In my code, I'm using an old $1 universal control (Sony-ish) to set the clock, but you can do it the more traditional (and annoying way, too) by using variables in the set-time program and then load your code for interrogation ...

Ray

Thanks for replying guys!

  if (now.hour() == 13 && now.minute() == 0)

digitalWrite (13, HIGH) ;
  else
    digitalWrite (13, LOW) ;

MarkT this code worked perfectly! Fortunately, I am able to move forward with my project now! I really appreciate the help!

Alright everyone I have another question about the programming with the Real Time Clock.

The code I received earlier about programming a specific hour to turn on a specific LED light worked perfectly. Here's the code that worked:

  if (now.hour() == 13 && now.minute() == 0)
    digitalWrite (13, HIGH) ;
  else
    digitalWrite (13, LOW) ;

Now what I am unsure of is how to code is if I have a range. For example. I have a bunch of LED lights in the back of my clock that I want to be dim. However, when the minute time range of the current time is between 0min-5min (for example 5:00pm-5:05pm) I want a specific group of LED lights attached to pin 12 to become brighter than the LED lights that are attached to let's say pin 11.

I am unsure how to indicate a range with the Arduino. Any help is appreciated!

Thanks!

I want a specific group of LED lights attached to pin 12 to become brighter than the LED lights that are attached to let's say pin 11.

Varying the intensity of the LEDs requires using analogWrite() on PWM pins. Only the Mega supports PWM on pins 11 and 12.

if(now.hour() == 13 && (now.minute() >= 0 && now.minute() <= 5))
{
    // It's between 1:00PM and 1:05PM
}