141 lines
3.4 KiB
C++
Executable File
141 lines
3.4 KiB
C++
Executable File
#include <RTClib.h>
|
|
#include <LiquidCrystal_I2C.h>
|
|
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //[0x27 o 0x3F]
|
|
RTC_DS3231 pi1;
|
|
RTC_DS3231 pi2;
|
|
RTC_DS3231 sn3;
|
|
RTC_DS3231 m4;
|
|
volatile DateTime fecha;
|
|
unsigned long intervalo=1000;
|
|
unsigned long previoMillis=0;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
lcd.begin(20,4);
|
|
if(!pi1.begin()) {
|
|
Serial.println("Pi1 no encontrado!!");
|
|
} else { Serial.println("1 ok");}
|
|
if(!pi2.begin()) {
|
|
Serial.println("Pi2 no encontrado!!");
|
|
} else { Serial.println("2 ok");}
|
|
if(!sn3.begin()) {
|
|
Serial.println("3231SN no encontrado!!");
|
|
} else { Serial.println("3 ok");}
|
|
if(!m4.begin()) {
|
|
Serial.println("3231M no encontrado!!");
|
|
} else { Serial.println("4 ok");}
|
|
//pi1.adjust(DateTime(__DATE__, __TIME__));
|
|
//pi2.adjust(DateTime(__DATE__, __TIME__));
|
|
//sn3.adjust(DateTime(__DATE__, __TIME__));
|
|
//m4.adjust(DateTime(__DATE__, __TIME__));
|
|
}
|
|
|
|
void loop() {
|
|
unsigned long actualMillis = millis();
|
|
if ((unsigned long)(actualMillis - previoMillis) >= intervalo) {
|
|
lcd.clear();
|
|
lcd.setCursor(0,0);
|
|
fecha = pi1.now();
|
|
horaLCD();
|
|
//Serial.print("1 : ");
|
|
//mostrarFechaSerie();
|
|
lcd.print(" Pi1");
|
|
fecha = pi2.now();
|
|
lcd.setCursor(0,1);
|
|
horaLCD();
|
|
//Serial.print("2 : ");
|
|
//mostrarFechaSerie();
|
|
lcd.print(" Pi2");
|
|
fecha = sn3.now();
|
|
lcd.setCursor(0,2);
|
|
horaLCD();
|
|
//Serial.print("3 : ");
|
|
//mostrarFechaSerie();
|
|
//lcd.setCursor(16,2);
|
|
lcd.print(" SN3");
|
|
fecha = m4.now();
|
|
lcd.setCursor(0,3);
|
|
horaLCD();
|
|
//Serial.print("4 : ");
|
|
//mostrarFechaSerie();
|
|
//lcd.setCursor(16,3);
|
|
lcd.print(" M 4");
|
|
previoMillis = millis();
|
|
}
|
|
}
|
|
|
|
//En caso de ser necesario mostrar hora, este codigo añade un 0 a los valores menores a 10
|
|
String getHora(char var) {
|
|
switch (var) {
|
|
case 'h':
|
|
if(fecha.hour() < 10) {
|
|
return ('0'+(String)fecha.hour()); break;
|
|
} else {
|
|
return (String)fecha.hour();break;
|
|
}
|
|
case 'm':
|
|
if(fecha.minute() <10) {
|
|
return ('0'+(String)fecha.minute()); break;
|
|
} else {
|
|
return (String)fecha.minute();break;
|
|
}
|
|
case 's':
|
|
if(fecha.second() <10) {
|
|
return ('0'+(String)fecha.second()); break;
|
|
} else {
|
|
return (String)fecha.second();break;
|
|
}
|
|
case 'd':
|
|
if(fecha.day() <10) {
|
|
return ('0'+(String)fecha.day()); break;
|
|
} else {
|
|
return (String)fecha.day();break;
|
|
}
|
|
case 'n':
|
|
if(fecha.month() <10) {
|
|
return ('0'+(String)fecha.month()); break;
|
|
} else {
|
|
return (String)fecha.month();break;
|
|
}
|
|
case 'a':
|
|
if(fecha.year() <10) {
|
|
return ('0'+(String)fecha.year()); break;
|
|
} else {
|
|
return (String)fecha.year();break;
|
|
}
|
|
}
|
|
}
|
|
|
|
//Hora en LCD
|
|
void horaLCD() {
|
|
lcd.print(getHora('h'));
|
|
lcd.print(":");
|
|
lcd.print(getHora('m'));
|
|
/*
|
|
lcd.print(":");
|
|
lcd.print(getHora('s'));
|
|
*/
|
|
lcd.print(' ');
|
|
lcd.print(getHora('d'));
|
|
lcd.print('-');
|
|
lcd.print(getHora('n'));
|
|
lcd.print('-');
|
|
lcd.print(getHora('a'));
|
|
}
|
|
|
|
// Enviar hora y fecha por puerto serie
|
|
void mostrarFechaSerie() {
|
|
Serial.print(getHora('d'));
|
|
Serial.print("/");
|
|
Serial.print(getHora('n'));
|
|
Serial.print("/");
|
|
Serial.print(getHora('a'));
|
|
Serial.print(" ");
|
|
Serial.print(getHora('h'));
|
|
Serial.print(":");
|
|
Serial.print(getHora('m'));
|
|
Serial.print(":");
|
|
Serial.println(getHora('s'));
|
|
}
|
|
|