Hi All
Thanks all of you for an excellent resource..WOW
I have been dabbling with the Arduino UNO for a little while and finally decided to use it as the base of grow timer to activate led's.
After combing the extensive amount of examples to setup the DS1307 clock and set the time etc. I am at wits end and could really use a little help hence the reason I am posting this.
I am having a problem finding a nice simple working example for a few things that are required to make this little contraption work.
Problem 1.
Could someone please help me with a working example as a guide to convert a human time to a unix timestamp.
I am using the following code which allows me to match two times (now() and a time I set in the code) and trigger an event which is working so far but I realize it's probably easier to do comparisons with unix timestamps.
Please excuse any bad code and syntax and silly questions I may ask as I really am new at this and battling along in the dark.
The code I am using is as follows :
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
int second = 0;
int minute = 0;
int hour = 0;
int day = 0;
int month = 0;
int year = 0;
int dayOfWeek = 0;
boolean aflag = false; // a flag to see if led should be on
void setup () {
Wire.begin();
RTC.begin();
Serial.begin(9600);
Serial.print("\nThe current date and time will be updated every one seconds - set delay in code to change:\n\n");
}
void loop() {
DateTime now = RTC.now();
Serial.print(now.dayOfWeek(), DEC);
Serial.print(" day of week\n");
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();
delay(1000);
Serial.print(" since 1970 = ");
Serial.print(now.unixtime());
/* compare dayOfWeek, year, month, day, hour, minute, or second */
if (now.year() == 2015 && now.month() == 4 && now.day() == 9 && now.hour()== 1 && now.minute()==55 && now.second()==0) {
Serial.print("\n Let the sunshine in \n");
boolean aflag=true;
Serial.println();
Serial.print(" the value of aflag is");
Serial.print(aflag);
Serial.println();
}
else {
Serial.print("\n Nothing is happening it's dark now! \n");
}
}
I did find this this conversion method
#include <Wire.h>
#include <Time.h>
//struct tm t;
time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss)
{
tmElements_t tmSet;
tmSet.Year = YYYY - 1970;
tmSet.Month = MM;
tmSet.Day = DD;
tmSet.Hour = hh;
tmSet.Minute = mm;
tmSet.Second = ss;
return makeTime(tmSet);
}
//and later on in main loop ...
time_t unix1 = tmConvert_t(2015,4,19,7,30,01);
Serial.print("converted time is :");
Serial.println(unix1);
which works fine if used with the Time.h library but does not work when I try to use it in the first bit of above code which uses the RTClib.h.
Where am I going wrong ? I would like to use RTClib.h
Problem 2
I would like to store each led number as well as two unix timestamps and the aflag value somewhere.
So for example I would like to store the following:
led_number, timestamp1, timestamp2, aflag_value
My first thought was an array but it's probably better to store the values in a struct which I believe from what I have read should be used when storing different data types (Correct me if I am wrong ?).
The reasons being :
A.
I don't know how many led's will be added on and would like to add them without having to change a max value of supported led's
Can this be done ? either in an array or struct ? can they be dynamically dimensioned ?
B.
The array will need to be parsed to check for a matching timestamp.
What method/action do I need to take to do this ?
C.
When a match is encountered I would like to match the timestamp with the led number for logging (for example : 'Led number 12 was switched on using timestamp X ')
I hope someone can shed some light here in time ( )
D