Sunrise - Date from RTC

Hey everybody,

I am struggling with a programming problem. I think for an arduino expert, it's nothing, but I am still a beginner.

I have an Arduino UNO, with a TinyRTC (DS1307 based/RTClib.h) connected to it.

I want to calculate sunrise time (to display it on an LCD. I use the sunrise.h library, which works fine.)

Instead of entering month and day manually every day (which works fine), I would like the Arduino to pull this information from the RTC, so that it is always up-to-date and I do not have to manually change the date.

So, in the code, I tried to just replace month and day (6,11) with ((now.month(), DEC),(now.day(), DEC)). However, it does not work. I always get 7:24 as sunrise time, which is not correct.
I should add that the (now.month(), DEC) and (now.day(), DEC) commands work fine elsewhere in the same code, when it comes to reading the time from the RTC.

I have been trying for a couple of hours now and am desperate at this point. Any help would be much appreciated!

This is my code. It may look much, but it is only the part at the end that does not work.

#include <Wire.h>
#include <LiquidCrystal.h>
#include <Adafruit_BMP085.h>
#include <RTClib.h>
#include <Sunrise.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

Adafruit_BMP085 bmp;
RTC_DS1307 RTC;

Sunrise mySunrise(49.44,11.86,+2);
  
void setup() {
  Wire.begin();
  lcd.begin(16, 2);
  bmp.begin();
  RTC.begin();
  //RTC.adjust(DateTime(__DATE__, __TIME__));
  mySunrise.Actual(); //Actual, Civil, Nautical, Astronomical
  delay(10);
  }
  
void loop() 
{
    lcd.print("Temperatur:");
    lcd.setCursor(0, 1);
    lcd.print(bmp.readTemperature());
    lcd.print(" ");
    lcd.print((char)223);
    lcd.print("C");    
    delay(2000);
    lcd.clear();
    
    lcd.print("Helligkeit:");
    lcd.setCursor(0, 1);
    lcd.print(analogRead(A0));
    lcd.print("/1023");    
    delay(2000);
    lcd.clear();
    
    lcd.print("Luftdruck:");
    lcd.setCursor(0, 1);
    lcd.print(((bmp.readPressure() + 4060)/100.0),1); //4050 ist Korrekturfaktor
    lcd.print(" hPa"); 
    delay(2000);
    lcd.clear();
 
    DateTime now = RTC.now();
    
    lcd.print("Datum:");
    lcd.setCursor(0, 1);
    lcd.print(now.day(), DEC);
    lcd.print('.');
    lcd.print(now.month(), DEC);
    lcd.print('.');
    lcd.print(now.year(), DEC);
    delay(2000);
    lcd.clear();
    
    lcd.print("Uhrzeit:");
    lcd.setCursor(0, 1);
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    lcd.print(now.second(), DEC);
    lcd.print(" Uhr");
    delay(2000);
    lcd.clear();
      
    byte h,m;
    int t;
    t=mySunrise.Rise((now.month(), DEC),(now.day(), DEC));// (month,day) - january=1
    h=mySunrise.Hour();
    m=mySunrise.Minute();
    lcd.print("Sunrise:");
    lcd.setCursor(0, 1);
    lcd.print(h, DEC);
    lcd.print(":");
    lcd.print(m, DEC);
    delay(2000);
    lcd.clear();
    
}

Many thanks again for any suggestions!

First get your RTC working as a RTC - perhaps you haven't set it running?
Perhaps you're always resetting it to a given data and time? These are the common
mistakes, you need to add debugging statements to see what's happening.

My ideas it's the same of MarkT. Did the RTC works fine, or not?
I have one of those and was a little "triky" to get if work.

t=mySunrise.Rise((now.month(), DEC),(now.day(), DEC))

One problem is the ",DEC" which is a special command to the print routines. Remove it.
A second problem might be the type of argument in the call to Sunrise.Rise. I believe "unsigned char" is expected for the month and day

t=mySunrise.Rise((now.month(), DEC),(now.day(), DEC));// (month,day) - january=1

This is wrong and actually sets the month and day to 10 (which is the value of DEC). You just need:

t=mySunrise.Rise(now.month(), now.day()); // (month,day) - january=1

I suggest you check whether the Sunrise library and RTC library both agree what number month January is - you might need to add/subtract 1 if they don't follow the same convention.

Works like a charm now!

Many thanks everybody, especially jremington and PeterH! The problem was the DEC which set month and day to 10.

That is why it always showed 7:24 as sunrise time - because on October 10, the sun in fact rises at 7:24 here :slight_smile:

I incorrectly thought that the DEC command was needed to convert the time data from the RTC from binary to decimal format.

Many thanks again.

This is the revised (and tested) code for a Breadboard Weather Station. Please feel free to use and modify:

#include <Wire.h>
#include <LiquidCrystal.h>
#include <Adafruit_BMP085.h>
#include <RTClib.h>
#include <Sunrise.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

Adafruit_BMP085 bmp;
RTC_DS1307 RTC;

Sunrise mySunrise(49.44,11.84,+2);
  
void setup() {
  Wire.begin();
  lcd.begin(16, 2);
  bmp.begin();
  RTC.begin();
  //RTC.adjust(DateTime(__DATE__, __TIME__));
  mySunrise.Actual(); //Actual, Civil, Nautical, Astronomical
  delay(10);
  }
  
void loop() 
{
    lcd.print("Temperatur:");
    lcd.setCursor(0, 1);
    lcd.print(bmp.readTemperature());
    lcd.print(" ");
    lcd.print((char)223);
    lcd.print("C");    
    delay(2000);
    lcd.clear();
    
    lcd.print("Helligkeit:");
    lcd.setCursor(0, 1);
    lcd.print(analogRead(A0));
    lcd.print("/1023");    
    delay(2000);
    lcd.clear();
    
    lcd.print("Luftdruck:");
    lcd.setCursor(0, 1);
    lcd.print(((bmp.readPressure() + 4060)/100.0),1); //4050 ist Korrekturfaktor
    lcd.print(" hPa"); 
    delay(2000);
    lcd.clear();
 
    DateTime now = RTC.now();
    
    lcd.print("Datum:");
    lcd.setCursor(0, 1);
    if ((now.day()<10))
    {
    lcd.print(0);
    }
    lcd.print (now.day());
    lcd.print('.');
    if ((now.month()<10))
    {
    lcd.print(0);
    }
    lcd.print(now.month());
    lcd.print('.');
    lcd.print(now.year());
    delay(2000);
    lcd.clear();
    
    lcd.print("Uhrzeit:");
    lcd.setCursor(0, 1);
    if ((now.hour()<10))
    {
    lcd.print(0);
    }
    lcd.print(now.hour());
    lcd.print(':');
    if ((now.minute()<10))
    {
    lcd.print(0);
    }
    lcd.print(now.minute());
    lcd.print(':');
    if ((now.second()<10))
    {
    lcd.print(0);
    }
    lcd.print(now.second());
    lcd.print(" Uhr");
    delay(2000);
    lcd.clear();
    
    byte h,m;
    int t;
    t=mySunrise.Rise(now.month(),now.day());// (month,day) - january=1
    h=mySunrise.Hour();
    m=mySunrise.Minute();
    lcd.print("Sonnenaufgang:");
    lcd.setCursor(0, 1);
    if (h<10)
    {
    lcd.print(0);
    }
    lcd.print(h);
    lcd.print(":");
    if (m<10)
    {
    lcd.print(0);
    }
    lcd.print(m);
    delay(2000);
    lcd.clear();
    
}

a bit late but maybe interesting thread - http://forum.arduino.cc/index.php/topic,66426.0.html - about the calculations.

BTW do you have a link to the sunrise/set library?