Turn on LED (D13 port) with TimeLord.h and DS1302

I have succeeded in getting the sunrise and sunset time values using the DS1302 module and the TimeLord.h library.
But I failed to turn on the LED (D13 port) by substituting the value into a variable.
I put the code here. Could you please tell me how to fix the wrong part?

#define SCK_PIN 4
#define IO_PIN 3
#define RST_PIN 2
#define SUN 13

#include <TimeLord.h>
#include <DS1302.h>

// what is our longitude (west values negative) and latitude (south values negative)
float const LONGITUDE = xxx.xx;
float const LATITUDE = -xx.xx;

DS1302 rtc(RST_PIN, IO_PIN, SCK_PIN);  // Initialize rtc module
Time t;                  // Load time from rtc module.
TimeLord tardis; // Declare the timeload object.

void setup()
  /* Clear the 1302's halt flag */
  /* And disable write protection */
  /* Initialise the serial port */
  Serial.begin (9600);
  /* Used only when setting the actual time for the first time, and commented out afterwards */

  pinMode(SUN, OUTPUT);   
  digitalWrite(SUN, LOW); 

  tardis.TimeZone(10 * 60); // tell TimeLord what timezone your RTC is synchronized to. You can ignore DST

  // as long as the RTC never changes back and forth between DST and non-DST
  tardis.Position(LATITUDE, LONGITUDE); // tell TimeLord where in the world we are
  byte today[] = {  0, 0, 12, 27, 10, 2012    }; // store today's date (at noon) in an array for TimeLord to use

/*The following parts do not work
byte Now[] = {} ;     
     Now[0] = t.sec;    
     Now[1] = t.min;    
     Now[2] = t.hour;   
     Now[3] = t.date;   
     Now[4] = t.mon;    
     Now[5] = t.year;
  byte sunSet[] = {};
     sunSet[0] = tl_second;
     sunSet[1] = tl_minute;
     sunSet[2] = tl_hour; 
     sunSet[3] = tl_day;
     sunSet[4] = tl_month;
     sunSet[5] = tl_year; 
   byte sunRise[] = {};
     sunRise[0] = tl_second;
     sunRise[1] = tl_minute;
     sunRise[2] = tl_hour;
     sunRise[3] = tl_day;
     sunRise[4] = tl_month;
     sunRise[5] = tl_year;
 if(Now > sunRise && Now < sunSet ){
      digitalWrite(SUN, LOW);
      Serial.println("  Sunrise !!!  "); 
      else {
        digitalWrite(SUN, HIGH);
        Serial.println("  Sunset !!!  ");
if (tardis.SunRise(today)) // if the sun will rise today (it might not, in the [ant]arctic)
   Serial.print("Sunrise: ");
   Serial.print((int) today[tl_hour]);
   Serial.println((int) today[tl_minute]);

   if (tardis.SunSet(today)) // if the sun will set today (it might not, in the [ant]arctic)
   Serial.print("Sunset: ");
   Serial.print((int) today[tl_hour]);
   Serial.println((int) today[tl_minute]);

void loop() 

Des the Blink example work if you substitute a variable for LED_BUILTIN in the code ?

I’m a beginner, so it’s hard to understand you’re saying, but it works fine except for the grayed out parts of the code.
However, the part that turns on the D13 LED does not work by comparing the sunrise sunset result value with the current time.

That part is what I wrote.

You have three arrays with zero size: Now, sunSet and sunRise. But you try to store six items in them.

I also tried an array of 3 elements, but it didn’t work.

You have a number of issues and way too much code for something that doesn’t work.

I suggest that you just display the time from the RTC to start with.

I’ve tried a lot, but I think it’s a matter of data format. It seems that the time type of ds1302.h and that of timelord.h are not the same.

  DS1302.h - Arduino library support for the DS1302 Trickle Charge Timekeeping Chip
  Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
  This library has been made to easily interface and use the DS1302 RTC with
  the Arduino.

  You can find the latest version of the library at 

  This library is free software; you can redistribute it and/or
  modify it under the terms of the CC BY-NC-SA 3.0 license.
  Please see the included documents for further information.

  Commercial use of this library requires you to buy a license that
  will allow commercial use. This includes using the library,
  modified or not, as a tool to sell products.

  The license applies to all part of the library including the 
  examples and tools supplied with the library.
#ifndef DS1302_h
#define DS1302_h

#if defined(ARDUINO) && ARDUINO >= 100
	#include "Arduino.h"
	#include "WProgram.h"

#define FORMAT_SHORT	1
#define FORMAT_LONG	2


#define MONDAY		1
#define TUESDAY		2
#define WEDNESDAY	3
#define THURSDAY	4
#define FRIDAY		5
#define SATURDAY	6
#define SUNDAY		7

#define TCR_D1R2K	165
#define TCR_D1R4K	166
#define TCR_D1R8K	167
#define TCR_D2R2K	169
#define TCR_D2R4K	170
#define TCR_D2R8K	171
#define TCR_OFF		92

class Time
	uint8_t		hour;
	uint8_t		min;
	uint8_t		sec;
	uint8_t		date;
	uint8_t		mon;
	uint16_t	year;
	uint8_t		dow;


class DS1302_RAM
	byte	cell[31];


class DS1302
		DS1302(uint8_t ce_pin, uint8_t data_pin, uint8_t sclk_pin);
	Time	getTime();
	void	setTime(uint8_t hour, uint8_t min, uint8_t sec);
	void	setDate(uint8_t date, uint8_t mon, uint16_t year);
	void	setDOW(uint8_t dow);

	char	*getTimeStr(uint8_t format=FORMAT_LONG);
	char	*getDateStr(uint8_t slformat=FORMAT_LONG, uint8_t eformat=FORMAT_LITTLEENDIAN, char divider='.');
	char	*getDOWStr(uint8_t format=FORMAT_LONG);
	char	*getMonthStr(uint8_t format=FORMAT_LONG);

	void	halt(bool value);
	void	writeProtect(bool enable);
	void	setTCR(uint8_t value);

	void		writeBuffer(DS1302_RAM r);
	DS1302_RAM	readBuffer();
	void		poke(uint8_t addr, uint8_t value);
	uint8_t		peek(uint8_t addr);

	uint8_t _ce_pin;
	uint8_t _data_pin;
	uint8_t _sclk_pin;
	uint8_t _burstArray[8];

	uint8_t	_readByte();
	void	_writeByte(uint8_t value);
	uint8_t	_readRegister(uint8_t reg);
	void 	_writeRegister(uint8_t reg, uint8_t value);
	void	_burstRead();
	uint8_t	_decode(uint8_t value);
	uint8_t	_decodeH(uint8_t value);
	uint8_t	_decodeY(uint8_t value);
	uint8_t	_encode(uint8_t vaule);

extern "C" {

	#include <inttypes.h>
	#include <Math.h>

#define tl_second 0
#define tl_minute 1
#define tl_hour 2
#define tl_day 3
#define tl_month 4
#define tl_year 5

class TimeLord{
		// configuration
		bool Position(float, float);
		bool TimeZone(int);
		bool DstRules(uint8_t,uint8_t,uint8_t,uint8_t,uint8_t);
		// Political
		void GMT(uint8_t *);
		void DST(uint8_t *);
		//Solar & Astronomical
		bool SunRise(uint8_t *);
		bool SunSet(uint8_t *);
		float MoonPhase(uint8_t *);
		void Sidereal(uint8_t *, bool);
		uint8_t Season(uint8_t *);
		// Utility
		uint8_t DayOfWeek(uint8_t *);
		uint8_t LengthOfMonth(uint8_t *);
		bool IsLeapYear(int);
                // these were private
		void Adjust(uint8_t *, long);
		long DayNumber(uint16_t, uint8_t, uint8_t);
		bool InDst(uint8_t *);
		float latitude, longitude;
		int timezone;
		uint8_t dstm1, dstw1, dstm2, dstw2, dstadv;
		bool ComputeSun(uint8_t *, bool);
		char Signum(int);
		int Absolute(int);
		uint8_t _season(uint8_t *);

As long as you know what those formats are, you can deal with it. Consider the advice you got earlier regarding calculating the time of day in minutes from midnight.

You can do that for time now, sunset and sunrise and use those results in your if statements controlling the LED.

Thank you.
I wasted 3 days on this issue.
I am now thinking about whether to buy a commercialized product.
Wouldn’t it be nice if I could understand you well?
I think I need to study more.

Wasted is a bit harsh. Programming is hard, especially to start with. That was just three days learning :wink:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.