Hi,
How to use the TimeAlarms library?
The code below is a spin off from the Time Check example. The idea behind it is to set an alarm once a day. The date and time is read from the linino and saved as integers.
My code (which doesn’t work – seems to be related with time.h) is consuming too many resources. Is there a way that the linino can push to the board the date and time every so often as integers?
TIA
/*
Time Check
Gets the time from Linux via Bridge then parses out hours,
minutes and seconds for the Arduino using an Arduino Yún.
created 27 May 2013
modified 21 June 2013
By Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/TimeCheck
*/
#include <Process.h>
#include <Time.h>
#include <TimeAlarms.h>
Process date; // process used to get the datetime
int days, months, years, hours, minutes, seconds; // for the results
int lastSecond = -1; // need an impossible value for comparison
void readDateTime() {
if (!date.running()) {
date.begin("date");
date.addParameter("+%d/%m/%Y-%T");
date.run();
}
}
void setDateTime() {
//if there's a result from the date process, parse it:
while (date.available() > 0) {
// get the result of the date process (should be hh:mm:ss):
String timeString = date.readString();
timeString.trim();
// find the slashes /
int firstSlash = timeString.indexOf("/");
int secondSlash = timeString.lastIndexOf("/");
int firstDash = timeString.indexOf("-");
// find the colons:
int firstColon = timeString.indexOf(":");
int secondColon = timeString.lastIndexOf(":");
// get the substrings for date and time
String dayString = timeString.substring(0, firstSlash);
String monthString = timeString.substring(firstSlash + 1, secondSlash);
String yearString = timeString.substring(secondSlash + 1, firstDash);
String hourString = timeString.substring(firstDash + 1, firstColon);
String minString = timeString.substring(firstColon + 1, secondColon);
String secString = timeString.substring(secondColon + 1);
// convert to ints,saving the previous seconds
days = dayString.toInt();
months = monthString.toInt();
years = yearString.toInt();
hours = hourString.toInt();
minutes = minString.toInt();
lastSecond = seconds; // save to do a time comparison
seconds = secString.toInt();
}
}
void setup() {
Bridge.begin(); // initialize Bridge
Console.begin(); // initialize Console
while (!Console); // wait for Console Monitor to open
// run an initial date process. Should return:
// m/d/yyyy - hh:mm:ss European format
readDateTime();
setDateTime();
delay(250);
}
void loop() {
setTime(hours, minutes, seconds, days, months, years);
displayDateTime();
Alarm.alarmRepeat(20, 22 , 0, sayHello); // hours, minutes, seconds, function
}
void displayDateTime() {
// display date and time
if (lastSecond != seconds) { // if a second has passed
// print the datetime:
Console.print("The actual date & time is: ");
Console.print(days);
Console.print("/");
Console.print(months);
Console.print("/");
Console.print(years);
Console.print("-");
if (hours <= 9) {
Console.print("0"); // adjust for 0-9
}
Console.print(hours);
Console.print(":");
if (minutes <= 9) {
Console.print("0"); // adjust for 0-9
}
Console.print(minutes);
Console.print(":");
if (seconds <= 9) {
Console.print("0"); // adjust for 0-9
}
Console.println(seconds);
// restart the date process:
readDateTime();
}
setDateTime();
}
void sayHello() {
Console.println("Hello World");
}