//From the RTC
int minutes = 0;
int previous_minutes = 0;
void setup() {}
void loop() {
minutes = rtc.min(); // this range goes from 0 to 59.
if(minutes - previous_minutes >= 10) { // every 10 minutes interval is true
// do stuff
previous_minutes=minutes;
}
delay(123);
}
This is the same as the BlinkWithoutDelay example from the Arduino software.
I'm using the minutes from an RTC instead of millis();
The Problem is that the seconds go back to 0 to 59 and I'm left with the variable 'previous_minutes' stuck in an unreachable number by minutes.
The Problem is that the seconds go back to 0 to 59 and I'm left with the variable 'previous_minutes' stuck in an unreachable number by minutes.
The problem is that you should be converting hours, minutes, and seconds to a number of seconds since midnight (or year, month, day, hour, minute and seconds to seconds since 1/1/1970) and using that value for the current and previous time.
PaulS:
The problem is that you should be converting hours, minutes, and seconds to a number of seconds since midnight (or year, month, day, hour, minute and seconds to seconds since 1/1/1970) and using that value for the current and previous time.
Hey PaulS!
How can I append two or more integers without adding them? Cuz if I add rtc.minutes+rtc.seconds = the sum of both, instead of both side by side. ex, 1+2=3 VS 1+2=12
YYYY MM DD HR MIN SEC
2015 05 27 10 55 02
I would also encounter the same thing I don't understand, ex. millis(); goes from 0 to 999999 but the time goes from 00:00:00 to 00:00:59 and 00:01:00. there is no 00:00:60-61-etc.
How can I append two or more integers without adding them? Cuz if I add rtc.minutes+rtc.seconds = the sum of both, instead of both side by side. ex, 1+2=3 VS 1+2=12
How many seconds after midnight is 1:15:45? One hour is 60 minutes, so that is 75 minutes and 45 seconds, or 4545 seconds.
Actually, since time won't roll over until "the big crunch" causes the universe to collapse into a singularity again (well actually, a bit sooner with Unix time but even our distant descendants will be dead), with regular time you can use future time stamps and simply compare now() to them. It's more straightforward than using past time stamps and unsigned comparisons.
Pseudocode:
alarm = now() + delay_interval
loop
{
...other stuff...
if now() >= alarm do something
}
But you need to use "Unix time" or something like that that represents time in seconds, as @Paul suggested.
mistergreen:
Isn't bad to poll a i2c device like RTC so fast? You'd get errors on some polls from my experience.
It would be better if you use the Time.h library + RTC. You can get time from the Time.h instead of directly from the RTC.
This RTC works on the 3 wire thing, idk if that is the same as I2C.
I figure out anyways. Here you go for future people.
// This is only Day, Hour, Minutes and Seconds. Include month and year if necessary.
long current_time_in_seconds = t.date*86400 + t.hr*3600 + t.min*60 + t.sec;
mistergreen:
Isn't bad to poll a i2c device like RTC so fast? You'd get errors on some polls from my experience.
AFAIK, there is no restriction on the frequency of polling an RTC. But it is a silly waste of CPU resources. That's why the RTC hardware designers provided a 1 PPS output. You can use that to trigger one poll per second.
Pringles:
This RTC works on the 3 wire thing, idk if that is the same as I2C.
I figure out anyways. Here you go for future people.
// This is only Day, Hour, Minutes and Seconds. Include month and year if necessary.
long current_time_in_seconds = t.date86400 + t.hr3600 + t.min*60 + t.sec;
You still have a rollover issue. The millis() method works because the arithmetic is mod 32. For yours to work, the arithmetic would have to be mod <number_of_seconds_in_the_current_month>.
I believe it is safe to assume you haven't implemented that. Make your life easy and use the seconds since 1970 type in the time library. It's designed for things like this. Time element structs aren't.
aarg:
You still have a rollover issue. The millis() method works because the arithmetic is mod 32. For yours to work, the arithmetic would have to be mod <number_of_seconds_in_the_current_month>.
I believe it is safe to assume you haven't implemented that. Make your life easy and use the seconds since 1970 type in the time library. It's designed for things like this. Time element structs aren't.
I did implemented the following code, but it seems to have a flaw. Can you check the numbers that I use to convert everything to seconds to then add everything to get the total seconds of current date and time? EDIT: I don't know if the numbers are right specifically on the month, because some have 28, 30 and 31.
It is necessary to use this method for this project. No epoch?.
void loop() {
Time t = rtc.time();
int y = t.yr-2000; // multiplying for 2000 is bigger than a long var can handle so we take 2000 and we're left with 15
timestamp = y*31556952+t.mon*2592000+t.date*86400+t.hr*36000+t.min*60+t.sec;
Serial.println(timestamp); // 489476544 = 15-05-27 23:24:24
//printTime();
delay(1000);
}
This is the full code with the urls for the libraries if you all want to test it.
// Example sketch for interfacing with the DS1302 timekeeping chip. Copyright (c) 2009, Matt Sparks. All rights reserved.
// http://quadpoint.org/projects/arduino-ds1302
#include <stdio.h>
#include <DS1302.h>
namespace {
// Set the appropriate digital I/O pin connections. These are the pin assignments for the Arduino as well for as the DS1302 chip. See the DS1302 datasheet:
// http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
const int kCePin = 5; // Chip Enable
const int kIoPin = 6; // Input/Output
const int kSclkPin = 4; // Serial Clock
// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);
String dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: return "Sunday";
case Time::kMonday: return "Monday";
case Time::kTuesday: return "Tuesday";
case Time::kWednesday: return "Wednesday";
case Time::kThursday: return "Thursday";
case Time::kFriday: return "Friday";
case Time::kSaturday: return "Saturday";
}
return "(unknown day)";
}
void printTime() {
// Get the current time and date from the chip.
Time t = rtc.time();
// Name the day of the week.
const String day = dayAsString(t.day);
// Format the time and date and insert into the temporary buffer.
char buf[50];
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
day.c_str(),
t.yr, t.mon, t.date,
t.hr, t.min, t.sec);
// Print the formatted string to serial so we can see the time.
Serial.println(buf);
}
} // namespace
long timestamp = 0L;
int y;
void setup() {
Serial.begin(9600);
}
void loop() {
Time t = rtc.time();
int y = t.yr-2000; // multiplying for 2000 is bigger than a long var can handle so we take 2000 and we're left with 15
timestamp = y*31556952+t.mon*2592000+t.date*86400+t.hr*36000+t.min*60+t.sec;
Serial.println(timestamp); // 489476544 = 15-05-27 23:24:24
//printTime();
delay(1000);
}
EDIT: I changed by a little some of the values for more accuracy: timestamp = y*31556926+t.mon*2628000+t.date*86400+t.hr*36000+t.min*60+t.sec; Can someone check this numbers? I think it's about 2-4 days off point.
About every day or so, in the mornings I go to my work bench to find a not working arduino that I set the day before.
It does work but I suspect that sometimes the "timestamp" variable goes lower than it was which isn't suppose o happen, ever. For that to get fixed I would have to wait certain amonut of time for "timestamp" to level up with "previous_seconds" + 30 seconds to enter the if statement.
This library is quite similar to the DS1307 library, and integrates will with the Time library if you want to use
setSyncProvider(RTC.get); for syncronization of the internal millis clock with the rtc.
Pringles:
EDIT: I changed by a little some of the values for more accuracy: timestamp = y*31556926+t.mon*2628000+t.date*86400+t.hr*36000+t.min*60+t.sec; Can someone check this numbers? I think it's about 2-4 days off point.
Don't ever do this. It can't handle the end of a month, nor the end of a year. (Try it.)
Also, there are only 3600 seconds in an hour, not 36000.
What I see here is that you (plural) have been killing an ant with a howitzer.
My solution to the OP's problem:
//From the RTC
int minutes = 0;
int previous_minutes = -9999; // initialize to large negative garbage
int minutes_difference;
void setup() {}
void loop() {
minutes = rtc.min(); // this range goes from 0 to 59.
minutes_difference = minutes - previous_minutes;
if (minutes_difference < 0) { // this will happen if we've started a new hour
minutes_difference += 60; // this corrects for the missing hour
}
if(minutes_difference >= 10) { // every 10 minutes interval is true
// do stuff
previous_minutes=minutes;
}
delay(123);
}
If your RTC have an SQW output and if you can set this output to 1Hz, you could add atach an interrupt to this pin and have your arduino increase a variable, and each time the variable reach 10 (at 1Hz that would mean 10 seconds elapsed), do your stuff, and don't forget to reset that variable to 0.
I know the DS1307, 3231, 3232, 3234 are capable of doing this, probably many others but check with the datasheet of your RTC.
guix:
If your RTC have an SQW output and if you can set this output to 1Hz, you could add atach an interrupt to this pin and have your arduino increase a variable, and each time the variable reach 10 (at 1Hz that would mean 10 seconds elapsed), do your stuff, and don't forget to reset that variable to 0.
I know the DS1307, 3231, 3232, 3234 are capable of doing this, probably many others but check with the datasheet of your RTC.
odometer:
What I see here is that you (plural) have been killing an ant with a howitzer.
My solution to the OP's problem:
//From the RTC
int minutes = 0;
int previous_minutes = -9999; // initialize to large negative garbage
int minutes_difference;
void setup() {}
void loop() {
minutes = rtc.min(); // this range goes from 0 to 59.
minutes_difference = minutes - previous_minutes;
if (minutes_difference < 0) { // this will happen if we've started a new hour
minutes_difference += 60; // this corrects for the missing hour
}
if(minutes_difference >= 10) { // every 10 minutes interval is true
// do stuff
previous_minutes=minutes;
}
delay(123);
}
HAHAHAHAHAH I laugh when I Google what a howitzer was.
You sir, have solved my problem. This is amazingly simple and it works. Thank you!