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);
}
How would a function be written to print a integer value so that I can do math with the time and date?
vague example:
void loop() { int seconds = t.sec; seconds=seconds+10; }
Can you describe what exactly you want to do? What you have written in your trivial example should be fine. Show some code. What did you expect to happen vs. what really happened?
Delta_G:
Can you describe what exactly you want to do? What you have written in your trivial example should be fine. Show some code. What did you expect to happen vs. what really happened?
Thanks for your response!!!!!
#include <stdio.h>
#include <DS1302.h>
namespace {
const int kCePin = 5; // Chip Enable
const int kIoPin = 6; // Input/Output
const int kSclkPin = 4; // Serial Clock
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
void setup() {
Serial.begin(9600);
rtc.writeProtect(false);
rtc.halt(false);
// Sunday, September 22, 2013 at 01:38:50.
Time t(2013, 9, 22, 1, 38, 50, Time::kSunday);
// Set the time and date on the chip.
rtc.time(t);
}
void loop() {
printTime();
delay(1000);
}
The code works. the function "printTime();" prints "Monday 2015-5-26 01:23:45" every time its called.
What I ask for is to get only the seconds part so that I can do math, like add 'x' to the variable 'seconds'.
Pringles:
The code works. the function "printTime();" prints "Monday 2015-5-26 01:23:45" every time its called.
What I ask for is to get only the seconds part so that I can do math, like add 'x' to the variable 'seconds'.
That's the part I was wanting you to describe.
Add this at the end of your printTime function and see if it doesn't add 10 to the variable called seconds.
int seconds = t.sec;
Serial.print("Seconds before addition : ");
Serial.println(seconds);
seconds = seconds + 10;
Serial.print("Seconds after addition : ");
Serial.println(seconds);
Or are you wanting to advance the time by some number of seconds? Or are you trying to figure out what time it will be in 10 seconds? Instead of just saying, "I want to do math", describe what you are trying to accomplish.
Delta_G:
Or are you wanting to advance the time by some number of seconds? Or are you trying to figure out what time it will be in 10 seconds? Instead of just saying, "I want to do math", describe what you are trying to accomplish.
void loop() {
int minutes = minutes_from_rtc_function; // ?
int seconds = seconds_from_rtc_function; // ?
if(minutes >= 30) {
if(seconds == 14) {
Serial.print("Taco Bell");
delay(1000*60);
}
}
// LCD commands and LDR sensors among other stuff
delay(333);
}
I will try your code asap an get back with results, I'm not home right now.
I guess that I do not know how to get the 'int variables digits' from the function without serial.print.
Thank you so much for answering, I didn't think I would get this much help. Also, if I haven't answered what you're ask, ask me again xD
This isn't the same rtc or time library I'm used to, so I'm working off the assumption that the code you have with sprintf works as described. You just use the same code to get the time that you did there.
That's the part I'm not getting. You have that code, so you obviously understand how to do this, but then you ask as though you don't. So I'm a little unclear on what exactly it is that you don't get.
void loop() {
Time t = rtc.time();
int minutes = t.min;
int seconds = t.sec;
if(minutes >= 30) {
if(seconds == 14) {
Serial.print("Taco Bell");
delay(1000*60);
}
}
// LCD commands and LDR sensors among other stuff
delay(333);
}
Unnamed namespace is something you might use on very large programs with lots and lots of libraries that are written by someone else. It prevents name collisions if, for instance, you declare a function with the same name as one declared in one of the other header files. Personally, I think it would be better to see the error and change the name. That way you avoid confusion for anyone else reading the code. But that hack does work as well and sometimes gets used when several people are working together on different headers for the same piece of code.
Delta_G:
Unnamed namespace is something you might use on very large programs with lots and lots of libraries that are written by someone else. It prevents name collisions if, for instance, you declare a function with the same name as one declared in one of the other header files. Personally, I think it would be better to see the error and change the name. That way you avoid confusion for anyone else reading the code. But that hack does work as well and sometimes gets used when several people are working together on different headers for the same piece of code.