// Juan A. Villalpando.
// Marzo 2021. KIO4.COM
#include <Wire.h>
#include <Keypad_I2C.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// Reloj RTC
#include <RTClib.h>
RTC_DS1307 RTC;
// EEPROM
#include <EEPROM.h>
boolean reloj = false;
const byte ROWS = 4;
const byte COLS = 4;
String maximo = "0";
String minimo = "0";
float maximof = 0.0;
float minimof = 0.0;
char key = ' ';
String ddmmaaaa = "01012021";
String hhmtss = "123456";
int dd;
int mm;
int aaaa;
int hh;
int mt;
int ss;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {0,1,2,3};
byte colPins[COLS] = {4,5,6,7};
int i2caddress = 0x20; // Direccion I2C teclado.
Keypad_I2C kpd = Keypad_I2C( makeKeymap(keys), rowPins, colPins, ROWS, COLS, i2caddress );
void setup(){
Serial.begin(9600);
kpd.begin();
lcd.begin(16,2);// Columnas y filas de LCD
// Reloj RTC
Wire.begin();
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__));
// 22 de marzo de 2021 a las 7:14:32
// rtc.adjust(DateTime(2021, 3, 22, 7, 14, 32));
}
void loop(){
key = kpd.getKey();
if (key){
if (key == 'A'){ // Introducir temperaturas.
introducir_temperaturas();}
if (key == 'B'){ // Ver temperaturas.
ver_temperaturas();}
if (key == 'C'){ // Introducir tiempos.
introducir_tiempos();}
if (key == 'D'){ // Ver tiempos.
ver_tiempos();}
if (key == '1'){ // Guardar temperaturas en EEPROM.
guardar_EEPROM();}
if (key == '2'){ // Leer temperaturas de EEPROM.
leer_EEPROM();}
}
}
///////////////////////////////////////
///// Introducir temperaturas ////////
void introducir_temperaturas() {
// TEMPERATURA MAXIMA
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("Temp. maxima:");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(maximo);
maximo = "";
while (key != '#' ) { // Introduce el maximo.
key = kpd.getKey();
if (key){
maximo = maximo + key;
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("Temp. maxima:");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(maximo);
delay(100);
}
}
maximo = maximo.substring(0,maximo.length()-1); // Quitar #
key = ' ';
// TEMPERATURA MINIMA
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("Temp. minima:");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(minimo);
minimo = "";
while (key != '#' ) { // Introduce el minimo.
key = kpd.getKey();
if (key){
minimo = minimo + key;
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("Temp. minima:");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(minimo);
delay(100);
}
}
minimo = minimo.substring(0,minimo.length()-1); // Quitar #
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(minimo);
lcd.print(" "); // Quitar #
key = ' ';
maximof = maximo.toFloat() / 10.0;
minimof = minimo.toFloat() / 10.0;
ver_temperaturas();
}
////////////////////////////////////////////////////
///// Ver Temperaturas en LCD /////////////////////
void ver_temperaturas() {
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("Temp. max: ");
lcd.print(maximof);
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print("Temp. min: ");
lcd.print(minimof);
delay(50);
}
////////////////////////////////////////
///// Introducir tiempos Reloj RTC ////
void introducir_tiempos(){
// DIA/MES/AÑO
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("DD/MM/AAAA:");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(ddmmaaaa);
ddmmaaaa = "";
while (key != '#' ) { // Introduce el maximo.
key = kpd.getKey();
if (key){
ddmmaaaa = ddmmaaaa + key;
if (key == '*'){ddmmaaaa = ddmmaaaa.substring(0,ddmmaaaa.length()-2);}
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("DD/MM/AAAA:");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(ddmmaaaa);
delay(100);
}
}
ddmmaaaa = ddmmaaaa.substring(0,ddmmaaaa.length()-1); // Quitar #
key = ' ';
// HORA/MINUTO/SEGUNDO
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("HH/MM/SS:");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(hhmtss);
hhmtss = "";
while (key != '#' ) { // Introduce el minimo.
key = kpd.getKey();
if (key){
hhmtss = hhmtss + key;
if (key == '*'){hhmtss = hhmtss.substring(0,hhmtss.length()-2);}
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("HH/MM/SS:");
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(hhmtss);
delay(100);
}
}
hhmtss = hhmtss.substring(0,hhmtss.length()-1); // Quitar #
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print(hhmtss);
lcd.print(" "); // Quitar #
key = ' ';
// rtc.adjust(DateTime(aaaa, mm, dd, hh, mm, ss));
ver_tiempos();
}
///////////////////////////////////////////////////
///// Ver Tiempos Reloj RTC en LCD ///////////////
void ver_tiempos() {
DateTime now = RTC.now();
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.setCursor(0,1); // Siguiente renglón.;
lcd.print(now.hour(), DEC);
lcd.print("/");
lcd.print(now.minute(), DEC);
lcd.print("/");
lcd.print(now.second(), DEC);
delay(200);
}
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
///// GUARDAR EN EEPROM
void guardar_EEPROM(){
char caracter[23];
maximo = String(maximof);
minimo = String(minimof);
String todo = maximo + "," + minimo + "," + ddmmaaaa + "," + hhmtss;
todo.toCharArray(caracter, 23);
for (int i = 0; i<23; i++) {
EEPROM.write(i,todo[i]);
}
}
///// LEER EN EEPROM
void leer_EEPROM(){
char caracter[23];
for (int i = 0; i<23; i++) {
caracter[i] = EEPROM.read(i);
}
Serial.println(caracter);
String todo = caracter;
maximo = getValue(todo,',',0);
minimo = getValue(todo,',',1);
ddmmaaaa = getValue(todo,',',2);
hhmtss = getValue(todo,',',3);
maximof = maximo.toFloat();
minimof = minimo.toFloat();
Serial.println(maximo);
Serial.println(minimo);
Serial.println(ddmmaaaa);
Serial.println(hhmtss);
ver_temperaturas();
}
///////////////// Function Split by char ////////////////
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
|