Go Down

Topic: Combining RTCzero and SolarLib (Read 966 times) previous topic - next topic

frangiblex30

I am trying to use the inbuilt RTC on the Zero to feed the unix time to the SolarLib code. This should allow me to write the solar elevation to my servo.

However I am such a newbie that I do not know how include the code so that I can set the RTCzero and then call the unix time from it to feed into the SolarLib. Can anyone help?

This is the solalLib example code:

Code: [Select]
/* Solarlib_example
  **WARNING**
  This will likely only work on 32-bit ARM based Arduino variants,
  such as Teensy 3.0 and the Due, not on older 8-bit ATMEGA-based
  boards like the UNO or MEGA series.
  **********
  This sketch illustrates the basic use of the Solarlib library.
  You must also have the Teensyduino-modified Time library installed.
  https://www.pjrc.com/teensy/td_download.html
  This sketch will output a series of values and times to the
  Serial monitor to demonstrate the usage of the solar
  calculator. A full list of extractor functions can be found
  in the Solarlib.h and Solarlib.cpp library files.
 
  The solar calculator needs to be initialized using the
  initSolarCalc() function found in the setup loop.
 
  The output can be checked against the NOAA calculator here:
  http://www.esrl.noaa.gov/gmd/grad/solcalc/
*/

#include <math.h>
#include "TimeLib.h"
#include "Solarlib.h"
#include <Servo.h>
// Time zone offset (hr), zones west of GMT are negative
#define timeZoneOffset 0
// A fixed time used for testing: 2012-11-04 12:00
#define myUnixDate 1352030400
// Latitude, values north of equator are positive
#define mylat 52.19
// Longitude, values west of GMT are negative
#define mylon 0.147
// Create time variable
time_t t;
Servo myservo;
int pos = 0;

void setup () {
    Serial.begin(115200); // Set serial port speed
    // While the serial stream is not open, do nothing:
    while (!Serial) ;
    // Initialize the Solar Calculator with the time zone
    // offset, latitude, and longitude for the site.
    initSolarCalc(timeZoneOffset, mylat, mylon);
    // Initialize the Time library
    setTime(myUnixDate);
    // Initialize the time variable t
    t = now();
    myservo.attach(9);
}

void loop() {
  Serial.print("Current time: ");
  printDateTime(t);
  Serial.print("Solar Elevation, degrees: ");
  Serial.println(getSEC_Corr(t),4);
  pos = getSEC_Corr(t);
  myservo.write(pos+90);
  Serial.println();
  t = t + 3600; // Increment time by 1 hour
  delay(1000);
}


// Utility function to print time and date nicely
void printDateTime(time_t t){
  Serial.print(year(t));
  printDateDigits(month(t));
  printDateDigits(day(t));
  Serial.print(" ");
  Serial.print(hour(t));
  printDigits(minute(t));
  printDigits(second(t));
  Serial.println();
}
// Utility function to print month/day digits nicely
void printDateDigits(int digits){
  Serial.print("-");
  if(digits < 10) Serial.print("0");
  Serial.print(digits);
}
// Utility function for time value printing
void printDigits(int digits){
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

hzrnbgy

The Arduino Zero encodes the calendar date and time into a 32 bit register. I highly doubt it is compatible with the SolarLib if it's using unix time (number of seconds passed since some date in the past). There's going to be a little bit of Math involve.

frangiblex30

Thank you very much.  So presumably I need to convert the RTC to unix by adding up the seconds, minutes, hours, etc...although I guess with leap years etc it gets very very difficult...

frangiblex30

although RTCzero has a getEpoch function so does that mean it can convert between the two?

hzrnbgy

Looks like someone already did the work.

To use the library, I think you have to copy the two files

RTCZero.cpp
RTCZero.h

somewhere in your Arduino folder

And on your sketch, you just need to include

#include <RTCZero.h>

To make the RTC functions available.

I don't program in Arduino IDE so I can be wrong.

Go Up