Per la cronaca, ho ricominciato a smanettare con Time.h e TimeAlarm.h (11k di codice per un semplice orologio su lcd... ). Debbo capire come manipolare l'elenco degli allarmi e salvarlo su eeprom.
Ho messo insieme un orologio con lo shield lcd+keypad di Nuelectronics e il modulo RTC con batteria di sparkfun.
Per impostare data e ora, basta inviare tramite il serial monitor una stringa formattata così:
AAAA-MM-DD hh:mm:ss
seguita da CR e/o LF
Una volta impostate data e ora, l'orologio va avanti anche ad arduino spento, grazie alla pila al litio.
Alla successiva accensione (o reset) l'aggeggio mostra ancora l'ora esatta!
// Orologio con DS1307 su modulo RTC di Sparkfun e Nuelectronics lcd+analog keypad shield.
//
// Per impostare l'ora, inviare tramite seriale una stringa con il seguente formato:
// AAAA-MM-GG hh:mm:ss
// seguita da cr e/o lf
//
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // lcd pins for nuelectronics lcd+keypad shield
const short LCD_ROWS = 2;
const short LCD_COLS = 16;
void displayDateTime(int weekday, int d, int m, int y, int h, int mn, int s) {
char buf[20];
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(dayShortStr(weekday));
sprintf(buf, "%02d-%02d-%04d", d, m, y);
lcd.setCursor(6, 0);
lcd.print(buf);
sprintf(buf, "%02d:%02d:%02d", h, mn, s);
lcd.setCursor(4, 1);
lcd.print(buf);
}
void setDateTime(const char* buf) {
int t_hr;
int t_min;
int t_sec;
int t_day;
int t_mon;
int t_year;
sscanf(buf, "%4d-%2d-%2d %2d:%2d:%2d", &t_year, &t_mon, &t_day, &t_hr, &t_min, &t_sec);
setTime(t_hr, t_min, t_sec, t_day, t_mon, t_year);
RTC.set(now());
}
const byte SERIAL_BUFLEN = 20;
char serialBuffer[SERIAL_BUFLEN] = { 0 };
byte serialCnt = 0;
void processChar(char ch) {
if (ch == '\r' || ch == '\n') {
serialBuffer[serialCnt] = 0;
if (serialCnt == 19) {
setDateTime(serialBuffer);
}
serialCnt = 0;
}
else if (serialCnt < SERIAL_BUFLEN - 1) {
serialBuffer[serialCnt] = ch;
serialCnt++;
}
}
void splashScreen() {
lcd.clear();
lcd.print(" LCD CLOCK");
delay(1000);
}
void setup() {
lcd.begin(LCD_COLS, LCD_ROWS);
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
splashScreen();
}
void loop() {
static unsigned long prevMillis;
// ogni secondo visualizziamo data e ora
if (millis() - prevMillis >= 1000) {
prevMillis = millis();
time_t t = now();
displayDateTime(weekday(t), day(t), month(t), year(t), hour(t), minute(t), second(t));
}
if (Serial.available() > 0) {
char ch = Serial.read();
processChar(ch);
}
}