From 21239552c884d8c83723f030e3d2f8c708469add Mon Sep 17 00:00:00 2001 From: "jp.av.dev" Date: Wed, 7 Jul 2021 19:21:36 -0400 Subject: [PATCH 1/9] opt func termACC(a,b) --- caldera_rata/caldera_rata.ino | 45 +++++++---------------------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/caldera_rata/caldera_rata.ino b/caldera_rata/caldera_rata.ino index 002156b..338519a 100644 --- a/caldera_rata/caldera_rata.ino +++ b/caldera_rata/caldera_rata.ino @@ -23,8 +23,8 @@ void setup() { reloj.begin(); start=1; estado_termo=0; + //reloj.adjust(DateTime(__DATE__, __TIME__)); } -//reloj.adjust(DateTime(__DATE__, __TIME__)); void loop() { if(start) { @@ -37,45 +37,17 @@ void loop() { fecha = reloj.now(); if((((fecha.hour() >= horaON) && (fecha.hour() < horaOFF)) || ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { - termoON(); + termoACC(120, 110); } else if((((fecha.hour() < horaON) || (fecha.hour() >= horaOFF)) && ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { - termoOFF(); + termoACC(45, 62); } previoMillisLoop = millis(); } } - -// Secuencias de encendido y apagado de Caldera -void termoON() { - myservo_X.write(120); - espera=0; - cont=0; - previoMillisTermo=millis(); - do{ - actualMillis=millis(); - if(espera==0) { - if((unsigned long)(actualMillis - previoMillisTermo) >= 500) { - myservo_X.write(110); - previoMillisTermo=millis(); - espera=1; - cont++; - } - } - if(espera==1) { - if((unsigned long)(actualMillis - previoMillisTermo) >= 500) { - myservo_X.write(120); - previoMillisTermo=millis(); - espera=0; - cont++; - } - } - } while(cont<4); - estado_termo=1; -} -void termoOFF() { - myservo_X.write(45); +void termoACC(byte pos1, byte pos2) { + myservo_X.write(pos1); espera=0; cont=0; previoMillisTermo=millis(); @@ -83,7 +55,7 @@ void termoOFF() { actualMillis=millis(); if(espera==0) { if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) { - myservo_X.write(62); + myservo_X.write(pos2); previoMillisTermo=millis(); espera=1; cont++; @@ -91,12 +63,13 @@ void termoOFF() { } if(espera==1) { if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) { - myservo_X.write(45); + myservo_X.write(pos1); previoMillisTermo=millis(); espera=0; cont++; } } } while (cont<4); - estado_termo=0; + estado_termo=!estado_termo; } + From 3389c25f66bd9fc8c382479b2a3250a78318e9be Mon Sep 17 00:00:00 2001 From: "jp.av.dev" Date: Thu, 8 Jul 2021 02:05:10 -0400 Subject: [PATCH 2/9] add EEPROM y variables de control --- caldera_rata/caldera_rata.ino | 64 ++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/caldera_rata/caldera_rata.ino b/caldera_rata/caldera_rata.ino index 338519a..f381992 100644 --- a/caldera_rata/caldera_rata.ino +++ b/caldera_rata/caldera_rata.ino @@ -1,5 +1,15 @@ #include #include +#include + +#define dirHoraON1 0 +#define dirHoraOFF1 1 +#define dirHoraON2 2 +#define dirHoraOFF2 3 +#define dirPosON1 4 +#define dirPosON2 5 +#define dirPosOFF1 6 +#define dirPosOFF2 7 Servo myservo_X; RTC_DS3231 reloj; @@ -9,10 +19,14 @@ unsigned long intervalo_loop=5000; unsigned long previoMillisTermo=0; unsigned long previoMillisLoop=0; unsigned long actualMillis; -byte horaON=5; -byte horaOFF=7; -byte horaON2=16; -byte horaOFF2=18; +byte horaON1; +byte horaOFF1; +byte horaON2; +byte horaOFF2; +byte posON1; +byte posON2; +byte posOFF1; +byte posOFF2; byte cont=0; bool estado_termo; bool espera; @@ -21,8 +35,10 @@ bool start; void setup() { myservo_X.attach(9); reloj.begin(); - start=1; + setVarControl(); // Provisorio, a dividir segun control horario, posiciones. futuras vars estado_termo=0; + variablesControl(); + start=1; //reloj.adjust(DateTime(__DATE__, __TIME__)); } @@ -35,12 +51,12 @@ void loop() { actualMillis = millis(); if ((unsigned long)(actualMillis - previoMillisLoop) >= intervalo_loop) { fecha = reloj.now(); - if((((fecha.hour() >= horaON) && (fecha.hour() < horaOFF)) || + if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { - termoACC(120, 110); - } else if((((fecha.hour() < horaON) || (fecha.hour() >= horaOFF)) && + termoACC(posON1, posON2); + } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { - termoACC(45, 62); + termoACC(posOFF1, posOFF2); } previoMillisLoop = millis(); } @@ -73,3 +89,33 @@ void termoACC(byte pos1, byte pos2) { estado_termo=!estado_termo; } +// provisorio, case config para hora, horas o dias, posiciones, etc. +void setVarControl() { + horaON1 = 5; + horaOFF2 = 7; + horaON2 = 16; + horaOFF2 = 18; + posON1 = 120; + posON2 = 110; + posOFF1 = 45; + posOFF2 = 62; + EEPROM.put(dirHoraON1, horaON1); + EEPROM.put(dirHoraOFF1, horaOFF1); + EEPROM.put(dirHoraON2, horaON2); + EEPROM.put(dirHoraOFF2, horaOFF2); + EEPROM.put(dirPosON1,posON1); + EEPROM.put(dirPosON2, posON2); + EEPROM.put(dirPosOFF1, posOFF1); + EEPROM.put(dirPosOFF2, posOFF2); +} + +void variablesControl() { + horaON1 = EEPROM.read(dirHoraON1); + horaOFF1 = EEPROM.read(dirHoraOFF2); + horaON2 = EEPROM.read(dirHoraON2); + horaOFF2 = EEPROM.read(dirHoraOFF2); + posON1 = EEPROM.read(dirPosON1); + posON2 = EEPROM.read(dirPosON2); + posOFF1 = EEPROM.read(dirPosOFF1); + posOFF2 = EEPROM.read(dirPosOFF2); +} From 76d3ebfafe424a80f5785c0550b80077194a6921 Mon Sep 17 00:00:00 2001 From: "jp.av.dev" Date: Thu, 8 Jul 2021 21:50:59 -0400 Subject: [PATCH 3/9] EEPROM cargada --- caldera_rata/caldera_rata.ino | 118 ++++++++++++++++++---- caldera_rata_deb/caldera_rata_deb.ino | 140 +++++++++++++++----------- 2 files changed, 182 insertions(+), 76 deletions(-) diff --git a/caldera_rata/caldera_rata.ino b/caldera_rata/caldera_rata.ino index f381992..9061b83 100644 --- a/caldera_rata/caldera_rata.ino +++ b/caldera_rata/caldera_rata.ino @@ -27,18 +27,21 @@ byte posON1; byte posON2; byte posOFF1; byte posOFF2; -byte cont=0; +byte cont; bool estado_termo; bool espera; bool start; void setup() { + Serial.begin(9600); myservo_X.attach(9); reloj.begin(); - setVarControl(); // Provisorio, a dividir segun control horario, posiciones. futuras vars + //setVarControl(); // Provisorio, a dividir segun control horario, posiciones. futuras vars estado_termo=0; + setVarControl(); variablesControl(); start=1; + ajustaReloj(2021, 7, 8, 5, 30, 0); //reloj.adjust(DateTime(__DATE__, __TIME__)); } @@ -50,13 +53,16 @@ void loop() { } actualMillis = millis(); if ((unsigned long)(actualMillis - previoMillisLoop) >= intervalo_loop) { + printFecha(); fecha = reloj.now(); if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { termoACC(posON1, posON2); + Serial.println("TERMO ON"); } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { termoACC(posOFF1, posOFF2); + Serial.println("TERMO OFF"); } previoMillisLoop = millis(); } @@ -91,31 +97,105 @@ void termoACC(byte pos1, byte pos2) { // provisorio, case config para hora, horas o dias, posiciones, etc. void setVarControl() { - horaON1 = 5; - horaOFF2 = 7; - horaON2 = 16; - horaOFF2 = 18; - posON1 = 120; - posON2 = 110; - posOFF1 = 45; - posOFF2 = 62; - EEPROM.put(dirHoraON1, horaON1); - EEPROM.put(dirHoraOFF1, horaOFF1); - EEPROM.put(dirHoraON2, horaON2); - EEPROM.put(dirHoraOFF2, horaOFF2); - EEPROM.put(dirPosON1,posON1); - EEPROM.put(dirPosON2, posON2); - EEPROM.put(dirPosOFF1, posOFF1); - EEPROM.put(dirPosOFF2, posOFF2); + //horaON1 = 5; + //horaOFF1 = 7; + //horaON2 = 16; + //horaOFF2 = 18; + //posON1 = 120; + //posON2 = 110; + //posOFF1 = 45; + //posOFF2 = 62; + //EEPROM.put(dirHoraON1, horaON1); + //EEPROM.put(dirHoraOFF1, horaOFF1); + //EEPROM.put(dirHoraON2, horaON2); + //EEPROM.put(dirHoraOFF2, horaOFF2); + //EEPROM.put(dirPosON1,posON1); + //EEPROM.put(dirPosON2, posON2); + //EEPROM.put(dirPosOFF1, posOFF1); + //EEPROM.put(dirPosOFF2, posOFF2); } void variablesControl() { horaON1 = EEPROM.read(dirHoraON1); - horaOFF1 = EEPROM.read(dirHoraOFF2); + horaOFF1 = EEPROM.read(dirHoraOFF1); horaON2 = EEPROM.read(dirHoraON2); horaOFF2 = EEPROM.read(dirHoraOFF2); posON1 = EEPROM.read(dirPosON1); posON2 = EEPROM.read(dirPosON2); posOFF1 = EEPROM.read(dirPosOFF1); posOFF2 = EEPROM.read(dirPosOFF2); + Serial.println("Variables: "); + Serial.println((String)horaON1); + Serial.println((String)horaOFF1); + Serial.println((String)horaON2); + Serial.println((String)horaOFF2); + Serial.println((String)posON1); + Serial.println((String)posON2); + Serial.println((String)posOFF1); + Serial.println((String)posOFF2); + +} + +void ajustaReloj(int ano, byte mes, byte dia, byte hra, byte mins, byte segs ) { + DateTime dt(ano, mes, dia, hra, mins, segs); + reloj.adjust(dt); +} + +//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; + } + } +} + +// Enviar hora y fecha por puerto serie +void printFecha() { + 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')); + Serial.println("Día de la semana: "); + Serial.println((String)fecha.dayOfTheWeek()); } diff --git a/caldera_rata_deb/caldera_rata_deb.ino b/caldera_rata_deb/caldera_rata_deb.ino index 5b9a11f..9061b83 100644 --- a/caldera_rata_deb/caldera_rata_deb.ino +++ b/caldera_rata_deb/caldera_rata_deb.ino @@ -1,5 +1,15 @@ #include #include +#include + +#define dirHoraON1 0 +#define dirHoraOFF1 1 +#define dirHoraON2 2 +#define dirHoraOFF2 3 +#define dirPosON1 4 +#define dirPosON2 5 +#define dirPosOFF1 6 +#define dirPosOFF2 7 Servo myservo_X; RTC_DS3231 reloj; @@ -9,11 +19,15 @@ unsigned long intervalo_loop=5000; unsigned long previoMillisTermo=0; unsigned long previoMillisLoop=0; unsigned long actualMillis; -byte horaON=5; -byte horaOFF=7; -byte horaON2=16; -byte horaOFF2=18; -byte cont=0; +byte horaON1; +byte horaOFF1; +byte horaON2; +byte horaOFF2; +byte posON1; +byte posON2; +byte posOFF1; +byte posOFF2; +byte cont; bool estado_termo; bool espera; bool start; @@ -22,73 +36,40 @@ void setup() { Serial.begin(9600); myservo_X.attach(9); reloj.begin(); - start=1; + //setVarControl(); // Provisorio, a dividir segun control horario, posiciones. futuras vars estado_termo=0; - DateTime dt(2021, 7, 5, 5, 32, 0); - reloj.adjust(dt); + setVarControl(); + variablesControl(); + start=1; + ajustaReloj(2021, 7, 8, 5, 30, 0); + //reloj.adjust(DateTime(__DATE__, __TIME__)); } -//reloj.adjust(DateTime(__DATE__, __TIME__)); - void loop() { if(start) { - Serial.println("Inicio"); myservo_X.write(30); start=0; fecha=reloj.now(); } actualMillis = millis(); if ((unsigned long)(actualMillis - previoMillisLoop) >= intervalo_loop) { + printFecha(); fecha = reloj.now(); - if((((fecha.hour() >= horaON) && (fecha.hour() < horaOFF)) || + if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { - Serial.print("pre ON : "); - Serial.println((String)estado_termo); - termoON(); - Serial.print("post ON : "); - Serial.println((String)estado_termo); - } else if((((fecha.hour() < horaON) || (fecha.hour() >= horaOFF)) && - ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { - Serial.print("pre OFF : "); - Serial.println((String)estado_termo); - termoOFF(); - Serial.print("post OFF : "); - Serial.println((String)estado_termo); + termoACC(posON1, posON2); + Serial.println("TERMO ON"); + } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && + ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { + termoACC(posOFF1, posOFF2); + Serial.println("TERMO OFF"); } previoMillisLoop = millis(); } } - -// Secuencias de encendido y apagado de Caldera -void termoON() { - myservo_X.write(120); - espera=0; - cont=0; - previoMillisTermo=millis(); - do{ - actualMillis=millis(); - if(espera==0) { - if((unsigned long)(actualMillis - previoMillisTermo) >= 500) { - myservo_X.write(110); - previoMillisTermo=millis(); - espera=1; - cont++; - } - } - if(espera==1) { - if((unsigned long)(actualMillis - previoMillisTermo) >= 500) { - myservo_X.write(120); - previoMillisTermo=millis(); - espera=0; - cont++; - } - } - } while(cont<4); - estado_termo=1; -} -void termoOFF() { - myservo_X.write(45); +void termoACC(byte pos1, byte pos2) { + myservo_X.write(pos1); espera=0; cont=0; previoMillisTermo=millis(); @@ -96,7 +77,7 @@ void termoOFF() { actualMillis=millis(); if(espera==0) { if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) { - myservo_X.write(62); + myservo_X.write(pos2); previoMillisTermo=millis(); espera=1; cont++; @@ -104,16 +85,61 @@ void termoOFF() { } if(espera==1) { if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) { - myservo_X.write(45); + myservo_X.write(pos1); previoMillisTermo=millis(); espera=0; cont++; } } } while (cont<4); - estado_termo=0; + estado_termo=!estado_termo; } +// provisorio, case config para hora, horas o dias, posiciones, etc. +void setVarControl() { + //horaON1 = 5; + //horaOFF1 = 7; + //horaON2 = 16; + //horaOFF2 = 18; + //posON1 = 120; + //posON2 = 110; + //posOFF1 = 45; + //posOFF2 = 62; + //EEPROM.put(dirHoraON1, horaON1); + //EEPROM.put(dirHoraOFF1, horaOFF1); + //EEPROM.put(dirHoraON2, horaON2); + //EEPROM.put(dirHoraOFF2, horaOFF2); + //EEPROM.put(dirPosON1,posON1); + //EEPROM.put(dirPosON2, posON2); + //EEPROM.put(dirPosOFF1, posOFF1); + //EEPROM.put(dirPosOFF2, posOFF2); +} + +void variablesControl() { + horaON1 = EEPROM.read(dirHoraON1); + horaOFF1 = EEPROM.read(dirHoraOFF1); + horaON2 = EEPROM.read(dirHoraON2); + horaOFF2 = EEPROM.read(dirHoraOFF2); + posON1 = EEPROM.read(dirPosON1); + posON2 = EEPROM.read(dirPosON2); + posOFF1 = EEPROM.read(dirPosOFF1); + posOFF2 = EEPROM.read(dirPosOFF2); + Serial.println("Variables: "); + Serial.println((String)horaON1); + Serial.println((String)horaOFF1); + Serial.println((String)horaON2); + Serial.println((String)horaOFF2); + Serial.println((String)posON1); + Serial.println((String)posON2); + Serial.println((String)posOFF1); + Serial.println((String)posOFF2); + +} + +void ajustaReloj(int ano, byte mes, byte dia, byte hra, byte mins, byte segs ) { + DateTime dt(ano, mes, dia, hra, mins, segs); + reloj.adjust(dt); +} //En caso de ser necesario mostrar hora, este codigo añade un 0 a los valores menores a 10 String getHora(char var) { From 45e5c53a751534c9337fcec74a8fccf568883c63 Mon Sep 17 00:00:00 2001 From: "jp.av.dev" Date: Thu, 8 Jul 2021 22:27:21 -0400 Subject: [PATCH 4/9] tests: eeprom ok, condiciones ok, rtc ok --- caldera_rata/caldera_rata.ino | 25 ++++++++++++------------- caldera_rata_deb/caldera_rata_deb.ino | 25 ++++++++++++------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/caldera_rata/caldera_rata.ino b/caldera_rata/caldera_rata.ino index 9061b83..dc561ea 100644 --- a/caldera_rata/caldera_rata.ino +++ b/caldera_rata/caldera_rata.ino @@ -38,10 +38,9 @@ void setup() { reloj.begin(); //setVarControl(); // Provisorio, a dividir segun control horario, posiciones. futuras vars estado_termo=0; - setVarControl(); variablesControl(); start=1; - ajustaReloj(2021, 7, 8, 5, 30, 0); + ajustaReloj(2021, 7, 8, 4, 59, 50); //reloj.adjust(DateTime(__DATE__, __TIME__)); } @@ -54,6 +53,7 @@ void loop() { actualMillis = millis(); if ((unsigned long)(actualMillis - previoMillisLoop) >= intervalo_loop) { printFecha(); + printVars(); fecha = reloj.now(); if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { @@ -124,15 +124,7 @@ void variablesControl() { posON2 = EEPROM.read(dirPosON2); posOFF1 = EEPROM.read(dirPosOFF1); posOFF2 = EEPROM.read(dirPosOFF2); - Serial.println("Variables: "); - Serial.println((String)horaON1); - Serial.println((String)horaOFF1); - Serial.println((String)horaON2); - Serial.println((String)horaOFF2); - Serial.println((String)posON1); - Serial.println((String)posON2); - Serial.println((String)posOFF1); - Serial.println((String)posOFF2); + } @@ -195,7 +187,14 @@ void printFecha() { Serial.print(":"); Serial.print(getHora('m')); Serial.print(":"); - Serial.println(getHora('s')); - Serial.println("Día de la semana: "); + Serial.print(getHora('s')); + Serial.print(" Día de la semana: "); Serial.println((String)fecha.dayOfTheWeek()); } + +void printVars() { + Serial.println("Variables: "+(String)horaON1+", "+(String)horaOFF1+", "+ + (String)horaON2+", "+(String)horaOFF2+", "+ + (String)posON1+", "+(String)posON2+", "+ + (String)posOFF1+", "+(String)posOFF2); +} diff --git a/caldera_rata_deb/caldera_rata_deb.ino b/caldera_rata_deb/caldera_rata_deb.ino index 9061b83..dc561ea 100644 --- a/caldera_rata_deb/caldera_rata_deb.ino +++ b/caldera_rata_deb/caldera_rata_deb.ino @@ -38,10 +38,9 @@ void setup() { reloj.begin(); //setVarControl(); // Provisorio, a dividir segun control horario, posiciones. futuras vars estado_termo=0; - setVarControl(); variablesControl(); start=1; - ajustaReloj(2021, 7, 8, 5, 30, 0); + ajustaReloj(2021, 7, 8, 4, 59, 50); //reloj.adjust(DateTime(__DATE__, __TIME__)); } @@ -54,6 +53,7 @@ void loop() { actualMillis = millis(); if ((unsigned long)(actualMillis - previoMillisLoop) >= intervalo_loop) { printFecha(); + printVars(); fecha = reloj.now(); if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { @@ -124,15 +124,7 @@ void variablesControl() { posON2 = EEPROM.read(dirPosON2); posOFF1 = EEPROM.read(dirPosOFF1); posOFF2 = EEPROM.read(dirPosOFF2); - Serial.println("Variables: "); - Serial.println((String)horaON1); - Serial.println((String)horaOFF1); - Serial.println((String)horaON2); - Serial.println((String)horaOFF2); - Serial.println((String)posON1); - Serial.println((String)posON2); - Serial.println((String)posOFF1); - Serial.println((String)posOFF2); + } @@ -195,7 +187,14 @@ void printFecha() { Serial.print(":"); Serial.print(getHora('m')); Serial.print(":"); - Serial.println(getHora('s')); - Serial.println("Día de la semana: "); + Serial.print(getHora('s')); + Serial.print(" Día de la semana: "); Serial.println((String)fecha.dayOfTheWeek()); } + +void printVars() { + Serial.println("Variables: "+(String)horaON1+", "+(String)horaOFF1+", "+ + (String)horaON2+", "+(String)horaOFF2+", "+ + (String)posON1+", "+(String)posON2+", "+ + (String)posOFF1+", "+(String)posOFF2); +} From 764ea6a6f2a3be8b3b9909090f1b284068539b77 Mon Sep 17 00:00:00 2001 From: "jp.av.dev" Date: Fri, 9 Jul 2021 03:57:01 -0400 Subject: [PATCH 5/9] interaccion Serie OK, logica pre ESP, testeada --- caldera_rata_deb/caldera_rata_deb.ino | 178 +++++++++++++++++++++----- 1 file changed, 144 insertions(+), 34 deletions(-) diff --git a/caldera_rata_deb/caldera_rata_deb.ino b/caldera_rata_deb/caldera_rata_deb.ino index dc561ea..75a0547 100644 --- a/caldera_rata_deb/caldera_rata_deb.ino +++ b/caldera_rata_deb/caldera_rata_deb.ino @@ -15,7 +15,7 @@ Servo myservo_X; RTC_DS3231 reloj; volatile DateTime fecha; -unsigned long intervalo_loop=5000; +unsigned long intervalo_loop=2500; unsigned long previoMillisTermo=0; unsigned long previoMillisLoop=0; unsigned long actualMillis; @@ -27,18 +27,23 @@ byte posON1; byte posON2; byte posOFF1; byte posOFF2; +byte posTEMP; byte cont; bool estado_termo; bool espera; +bool espera_man; bool start; +byte manual; +byte estado; +byte hr, mn, sg, di, me; +int an; void setup() { Serial.begin(9600); myservo_X.attach(9); reloj.begin(); - //setVarControl(); // Provisorio, a dividir segun control horario, posiciones. futuras vars + estado=1; estado_termo=0; - variablesControl(); start=1; ajustaReloj(2021, 7, 8, 4, 59, 50); //reloj.adjust(DateTime(__DATE__, __TIME__)); @@ -47,27 +52,153 @@ void setup() { void loop() { if(start) { myservo_X.write(30); - start=0; fecha=reloj.now(); + cargarVarsControl(); + start=0; } actualMillis = millis(); if ((unsigned long)(actualMillis - previoMillisLoop) >= intervalo_loop) { printFecha(); printVars(); fecha = reloj.now(); - if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || - ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { - termoACC(posON1, posON2); - Serial.println("TERMO ON"); - } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && - ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { - termoACC(posOFF1, posOFF2); - Serial.println("TERMO OFF"); + switch(estado) { + case 1: + Serial.println(estado); + if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || + ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { + termoACC(posON1, posON2); + Serial.println("TERMO ON"); + } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && + ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { + termoACC(posOFF1, posOFF2); + Serial.println("TERMO OFF"); + } + //previoMillisLoop = millis(); + break; + case 2: + // ajustaReloj + //validar + ajustaReloj(an, me, di, hr, mn, sg); + estado = 1; + break; + case 3: + // ajuste horas de funcionamiento + // validar + EEPROM.put(dirHoraON1, horaON1); + EEPROM.put(dirHoraOFF1, horaOFF1); + EEPROM.put(dirHoraON2, horaON2); + EEPROM.put(dirHoraOFF2, horaOFF2); + cargarVarsControl(); + estado = 1; + break; + case 4: + // ajustaPosServo + // validar + EEPROM.put(dirPosON1, posON1); + EEPROM.put(dirPosON2, posON2); + EEPROM.put(dirPosOFF1, posOFF1); + EEPROM.put(dirPosOFF2, posOFF2); + cargarVarsControl(); + estado = 1; + break; + case 5: + if(espera_man) { + if(manual == 1) { + termoACC(posON1, posON2); + } else if (manual == 0){ + termoACC(posOFF1, posOFF2); + } + espera_man=0; + } + break; + case 6: + myservo_X.write(posTEMP); + // funcionamiento 'externo' + // debe reaccionar a los parametros enviados por Serial + // movimiento libre, termoACC, ajustaReloj, configs, etc. + //previoMillisLoop = millis(); + estado_termo=0; + break; + default: + estado=1; } + leerDatos(); previoMillisLoop = millis(); } } +void leerDatos() { + if (Serial.available() > 0) { + byte temp_var=0; + temp_var = Serial.readStringUntil(',').toInt(); + if(temp_var < 7 && temp_var > 0){ + estado = temp_var; + } else { + estado = 1; + } + switch (estado) { + case 1: + limpiaSerial(); + break; + case 2: + di = Serial.readStringUntil(',').toInt(); + me = Serial.readStringUntil(',').toInt(); + an = Serial.readStringUntil(',').toInt(); + hr = Serial.readStringUntil(',').toInt(); + mn = Serial.readStringUntil(',').toInt(); + sg = Serial.readStringUntil(',').toInt(); + break; + case 3: + horaON1 = Serial.readStringUntil(',').toInt(); + horaOFF1 = Serial.readStringUntil(',').toInt(); + horaON2 = Serial.readStringUntil(',').toInt(); + horaOFF2 = Serial.readStringUntil(',').toInt(); + break; + case 4: + posON1 = Serial.readStringUntil(',').toInt(); + posON2 = Serial.readStringUntil(',').toInt(); + posOFF1 = Serial.readStringUntil(',').toInt(); + posOFF2 = Serial.readStringUntil(',').toInt(); + break; + case 5: + manual = Serial.readStringUntil(',').toInt(); + espera_man = 1; + break; + case 6: + posTEMP = Serial.readStringUntil(',').toInt(); + break; + default: + limpiaSerial(); + } + limpiaSerial(); + } else { + Serial.println("Nada q hacer"); + } +} + + + +void limpiaSerial() { + while(Serial.available() > 0) { + char temp = Serial.read(); + } +} + +//como bash: cut d',' -f 0,1,.. file +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]) : ""; +} + void termoACC(byte pos1, byte pos2) { myservo_X.write(pos1); espera=0; @@ -95,27 +226,8 @@ void termoACC(byte pos1, byte pos2) { estado_termo=!estado_termo; } -// provisorio, case config para hora, horas o dias, posiciones, etc. -void setVarControl() { - //horaON1 = 5; - //horaOFF1 = 7; - //horaON2 = 16; - //horaOFF2 = 18; - //posON1 = 120; - //posON2 = 110; - //posOFF1 = 45; - //posOFF2 = 62; - //EEPROM.put(dirHoraON1, horaON1); - //EEPROM.put(dirHoraOFF1, horaOFF1); - //EEPROM.put(dirHoraON2, horaON2); - //EEPROM.put(dirHoraOFF2, horaOFF2); - //EEPROM.put(dirPosON1,posON1); - //EEPROM.put(dirPosON2, posON2); - //EEPROM.put(dirPosOFF1, posOFF1); - //EEPROM.put(dirPosOFF2, posOFF2); -} -void variablesControl() { +void cargarVarsControl() { horaON1 = EEPROM.read(dirHoraON1); horaOFF1 = EEPROM.read(dirHoraOFF1); horaON2 = EEPROM.read(dirHoraON2); @@ -124,8 +236,6 @@ void variablesControl() { posON2 = EEPROM.read(dirPosON2); posOFF1 = EEPROM.read(dirPosOFF1); posOFF2 = EEPROM.read(dirPosOFF2); - - } void ajustaReloj(int ano, byte mes, byte dia, byte hra, byte mins, byte segs ) { From d1bfc4b934d6b738b9fb08080b6611147cb593ad Mon Sep 17 00:00:00 2001 From: "jp.av.dev" Date: Fri, 9 Jul 2021 21:55:06 -0400 Subject: [PATCH 6/9] validaciones OK --- caldera_rata/caldera_rata.ino | 233 ++++++++++++++++++++++---- caldera_rata_deb/caldera_rata_deb.ino | 117 +++++++++---- 2 files changed, 281 insertions(+), 69 deletions(-) diff --git a/caldera_rata/caldera_rata.ino b/caldera_rata/caldera_rata.ino index dc561ea..020df53 100644 --- a/caldera_rata/caldera_rata.ino +++ b/caldera_rata/caldera_rata.ino @@ -15,7 +15,7 @@ Servo myservo_X; RTC_DS3231 reloj; volatile DateTime fecha; -unsigned long intervalo_loop=5000; +unsigned long intervalo_loop=2500; unsigned long previoMillisTermo=0; unsigned long previoMillisLoop=0; unsigned long actualMillis; @@ -27,47 +27,170 @@ byte posON1; byte posON2; byte posOFF1; byte posOFF2; +byte posTEMP; byte cont; bool estado_termo; bool espera; +bool espera_man; bool start; +byte manual; +byte estado; +byte hr, mn, sg, di, me; +int an; void setup() { Serial.begin(9600); myservo_X.attach(9); reloj.begin(); - //setVarControl(); // Provisorio, a dividir segun control horario, posiciones. futuras vars + estado=1; estado_termo=0; - variablesControl(); start=1; - ajustaReloj(2021, 7, 8, 4, 59, 50); + //ajustaReloj(2021, 7, 8, 4, 59, 50); //reloj.adjust(DateTime(__DATE__, __TIME__)); } void loop() { if(start) { myservo_X.write(30); - start=0; fecha=reloj.now(); + cargarVarsControl(); + start=0; } actualMillis = millis(); if ((unsigned long)(actualMillis - previoMillisLoop) >= intervalo_loop) { printFecha(); printVars(); fecha = reloj.now(); - if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || - ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { - termoACC(posON1, posON2); - Serial.println("TERMO ON"); - } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && - ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { - termoACC(posOFF1, posOFF2); - Serial.println("TERMO OFF"); + switch(estado) { + case 1: + Serial.println(estado); + if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || + ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { + termoACC(posON1, posON2); + Serial.println("TERMO ON"); + } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && + ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { + termoACC(posOFF1, posOFF2); + Serial.println("TERMO OFF"); + } + //previoMillisLoop = millis(); + break; + case 2: + // ajustaReloj + setVarsEP(); + estado = 1; + break; + case 3: + // ajuste horas de funcionamiento + setVarsEP(); + cargarVarsControl(); + estado = 1; + break; + case 4: + // ajustaPosServo + setVarsEP(); + cargarVarsControl(); + estado = 1; + break; + case 5: + if(espera_man) { + if(manual == 1) { + termoACC(posON1, posON2); + } else if (manual == 0){ + termoACC(posOFF1, posOFF2); + } + espera_man=0; + } + break; + case 6: + if(validaPOS(posTEMP)) { + myservo_X.write(posTEMP); + estado_termo=0; + } + // debe reaccionar a los parametros enviados por Serial + // movimiento libre, termoACC, ajustaReloj, configs, etc. + break; + default: + estado=1; } + leerDatos(); previoMillisLoop = millis(); } } +void leerDatos() { + if (Serial.available() > 0) { + byte temp_var=0; + temp_var = Serial.readStringUntil(',').toInt(); + if(temp_var < 7 && temp_var > 0){ + estado = temp_var; + } else { + estado = 1; + } + switch (estado) { + case 1: + limpiaSerial(); + break; + case 2: + di = Serial.readStringUntil(',').toInt(); + me = Serial.readStringUntil(',').toInt(); + an = Serial.readStringUntil(',').toInt(); + hr = Serial.readStringUntil(',').toInt(); + mn = Serial.readStringUntil(',').toInt(); + sg = Serial.readStringUntil(',').toInt(); + Serial.print("Datos leidos :"+(String)di+"/"+(String)me+"/"+(String)an+" "+(String)hr+":"+(String)mn+":"+(String)sg); + break; + case 3: + horaON1 = Serial.readStringUntil(',').toInt(); + horaOFF1 = Serial.readStringUntil(',').toInt(); + horaON2 = Serial.readStringUntil(',').toInt(); + horaOFF2 = Serial.readStringUntil(',').toInt(); + break; + case 4: + posON1 = Serial.readStringUntil(',').toInt(); + posON2 = Serial.readStringUntil(',').toInt(); + posOFF1 = Serial.readStringUntil(',').toInt(); + posOFF2 = Serial.readStringUntil(',').toInt(); + break; + case 5: + manual = Serial.readStringUntil(',').toInt(); + espera_man = 1; + break; + case 6: + posTEMP = Serial.readStringUntil(',').toInt(); + break; + default: + limpiaSerial(); + } + limpiaSerial(); + } else { + Serial.println("Nada q hacer"); + } +} + + + +void limpiaSerial() { + while(Serial.available() > 0) { + char temp = Serial.read(); + } +} + +//como bash: cut -d +//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]) : ""; +//} + void termoACC(byte pos1, byte pos2) { myservo_X.write(pos1); espera=0; @@ -95,27 +218,57 @@ void termoACC(byte pos1, byte pos2) { estado_termo=!estado_termo; } -// provisorio, case config para hora, horas o dias, posiciones, etc. -void setVarControl() { - //horaON1 = 5; - //horaOFF1 = 7; - //horaON2 = 16; - //horaOFF2 = 18; - //posON1 = 120; - //posON2 = 110; - //posOFF1 = 45; - //posOFF2 = 62; - //EEPROM.put(dirHoraON1, horaON1); - //EEPROM.put(dirHoraOFF1, horaOFF1); - //EEPROM.put(dirHoraON2, horaON2); - //EEPROM.put(dirHoraOFF2, horaOFF2); - //EEPROM.put(dirPosON1,posON1); - //EEPROM.put(dirPosON2, posON2); - //EEPROM.put(dirPosOFF1, posOFF1); - //EEPROM.put(dirPosOFF2, posOFF2); +void setVarsEP(){ + switch (estado) { + case 2: + ajustaReloj(an, me, di, hr, mn, sg); + break; + case 3: + if(validaHR(horaON1)) { + EEPROM.put(dirHoraON1, horaON1); + } + if(validaHR(horaOFF1)) { + EEPROM.put(dirHoraOFF1, horaOFF1); + } + if(validaHR(horaON2) && horaON2 > horaOFF1) { + EEPROM.put(dirHoraON2, horaON2); + } else if(validaHR(horaON1)) { + EEPROM.put(dirHoraON2, horaON1); + } // validacion incompleta + if(validaHR(horaOFF2) && horaOFF2 > horaON2 && horaOFF2 > horaOFF1) { + EEPROM.put(dirHoraOFF2, horaOFF2); + } else if(validaHR(horaOFF1)) { + EEPROM.put(dirHoraOFF2, horaOFF1); + } + break; + case 4: + if(validaPOS(posON1)) { + EEPROM.put(dirPosON1, posON1); + } + if(validaPOS(posON2)) { + EEPROM.put(dirPosON2, posON2); + } + if(validaPOS(posOFF1)) { + EEPROM.put(dirPosOFF1, posOFF1); + } + if(validaPOS(posOFF2)) { + EEPROM.put(dirPosOFF2, posOFF2); + } + break; + default: + Serial.print("default setVarsEP"); + } } -void variablesControl() { +bool validaHR(byte hr) { + return (hr < 24 && hr >= 0); +} + +bool validaPOS(byte pos) { + return (pos < 165) && (pos >15); +} + +void cargarVarsControl() { horaON1 = EEPROM.read(dirHoraON1); horaOFF1 = EEPROM.read(dirHoraOFF1); horaON2 = EEPROM.read(dirHoraON2); @@ -124,13 +277,21 @@ void variablesControl() { posON2 = EEPROM.read(dirPosON2); posOFF1 = EEPROM.read(dirPosOFF1); posOFF2 = EEPROM.read(dirPosOFF2); - - } void ajustaReloj(int ano, byte mes, byte dia, byte hra, byte mins, byte segs ) { - DateTime dt(ano, mes, dia, hra, mins, segs); - reloj.adjust(dt); + if((ano > 2000) && + (mes > 0 && mes < 13) && + (dia > 0 && dia < 32) && + (hra >= 0 && hra < 24) && + (mins >= 0 && mins < 60) && + (segs >= 0 && segs < 60)) { + DateTime dt(ano, mes, dia, hra, mins, segs); + reloj.adjust(dt); + Serial.println("Set RTC: valido"); + } else { + Serial.println("Set RTC: invalido"); + } } //En caso de ser necesario mostrar hora, este codigo añade un 0 a los valores menores a 10 diff --git a/caldera_rata_deb/caldera_rata_deb.ino b/caldera_rata_deb/caldera_rata_deb.ino index 75a0547..020df53 100644 --- a/caldera_rata_deb/caldera_rata_deb.ino +++ b/caldera_rata_deb/caldera_rata_deb.ino @@ -45,7 +45,7 @@ void setup() { estado=1; estado_termo=0; start=1; - ajustaReloj(2021, 7, 8, 4, 59, 50); + //ajustaReloj(2021, 7, 8, 4, 59, 50); //reloj.adjust(DateTime(__DATE__, __TIME__)); } @@ -77,27 +77,18 @@ void loop() { break; case 2: // ajustaReloj - //validar - ajustaReloj(an, me, di, hr, mn, sg); + setVarsEP(); estado = 1; break; case 3: // ajuste horas de funcionamiento - // validar - EEPROM.put(dirHoraON1, horaON1); - EEPROM.put(dirHoraOFF1, horaOFF1); - EEPROM.put(dirHoraON2, horaON2); - EEPROM.put(dirHoraOFF2, horaOFF2); + setVarsEP(); cargarVarsControl(); estado = 1; break; case 4: // ajustaPosServo - // validar - EEPROM.put(dirPosON1, posON1); - EEPROM.put(dirPosON2, posON2); - EEPROM.put(dirPosOFF1, posOFF1); - EEPROM.put(dirPosOFF2, posOFF2); + setVarsEP(); cargarVarsControl(); estado = 1; break; @@ -112,12 +103,12 @@ void loop() { } break; case 6: - myservo_X.write(posTEMP); - // funcionamiento 'externo' + if(validaPOS(posTEMP)) { + myservo_X.write(posTEMP); + estado_termo=0; + } // debe reaccionar a los parametros enviados por Serial // movimiento libre, termoACC, ajustaReloj, configs, etc. - //previoMillisLoop = millis(); - estado_termo=0; break; default: estado=1; @@ -147,6 +138,7 @@ void leerDatos() { hr = Serial.readStringUntil(',').toInt(); mn = Serial.readStringUntil(',').toInt(); sg = Serial.readStringUntil(',').toInt(); + Serial.print("Datos leidos :"+(String)di+"/"+(String)me+"/"+(String)an+" "+(String)hr+":"+(String)mn+":"+(String)sg); break; case 3: horaON1 = Serial.readStringUntil(',').toInt(); @@ -184,20 +176,20 @@ void limpiaSerial() { } } -//como bash: cut d',' -f 0,1,.. file -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]) : ""; -} +//como bash: cut -d +//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]) : ""; +//} void termoACC(byte pos1, byte pos2) { myservo_X.write(pos1); @@ -226,6 +218,55 @@ void termoACC(byte pos1, byte pos2) { estado_termo=!estado_termo; } +void setVarsEP(){ + switch (estado) { + case 2: + ajustaReloj(an, me, di, hr, mn, sg); + break; + case 3: + if(validaHR(horaON1)) { + EEPROM.put(dirHoraON1, horaON1); + } + if(validaHR(horaOFF1)) { + EEPROM.put(dirHoraOFF1, horaOFF1); + } + if(validaHR(horaON2) && horaON2 > horaOFF1) { + EEPROM.put(dirHoraON2, horaON2); + } else if(validaHR(horaON1)) { + EEPROM.put(dirHoraON2, horaON1); + } // validacion incompleta + if(validaHR(horaOFF2) && horaOFF2 > horaON2 && horaOFF2 > horaOFF1) { + EEPROM.put(dirHoraOFF2, horaOFF2); + } else if(validaHR(horaOFF1)) { + EEPROM.put(dirHoraOFF2, horaOFF1); + } + break; + case 4: + if(validaPOS(posON1)) { + EEPROM.put(dirPosON1, posON1); + } + if(validaPOS(posON2)) { + EEPROM.put(dirPosON2, posON2); + } + if(validaPOS(posOFF1)) { + EEPROM.put(dirPosOFF1, posOFF1); + } + if(validaPOS(posOFF2)) { + EEPROM.put(dirPosOFF2, posOFF2); + } + break; + default: + Serial.print("default setVarsEP"); + } +} + +bool validaHR(byte hr) { + return (hr < 24 && hr >= 0); +} + +bool validaPOS(byte pos) { + return (pos < 165) && (pos >15); +} void cargarVarsControl() { horaON1 = EEPROM.read(dirHoraON1); @@ -239,8 +280,18 @@ void cargarVarsControl() { } void ajustaReloj(int ano, byte mes, byte dia, byte hra, byte mins, byte segs ) { - DateTime dt(ano, mes, dia, hra, mins, segs); - reloj.adjust(dt); + if((ano > 2000) && + (mes > 0 && mes < 13) && + (dia > 0 && dia < 32) && + (hra >= 0 && hra < 24) && + (mins >= 0 && mins < 60) && + (segs >= 0 && segs < 60)) { + DateTime dt(ano, mes, dia, hra, mins, segs); + reloj.adjust(dt); + Serial.println("Set RTC: valido"); + } else { + Serial.println("Set RTC: invalido"); + } } //En caso de ser necesario mostrar hora, este codigo añade un 0 a los valores menores a 10 From 89014ff8d704e331976da286e499d229cb65b587 Mon Sep 17 00:00:00 2001 From: "jp.av.dev" Date: Sat, 10 Jul 2021 13:42:14 -0400 Subject: [PATCH 7/9] mejora en validacion horas de funcionamiento --- caldera_rata_deb/caldera_rata_deb.ino | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/caldera_rata_deb/caldera_rata_deb.ino b/caldera_rata_deb/caldera_rata_deb.ino index 020df53..a659e1f 100644 --- a/caldera_rata_deb/caldera_rata_deb.ino +++ b/caldera_rata_deb/caldera_rata_deb.ino @@ -226,18 +226,27 @@ void setVarsEP(){ case 3: if(validaHR(horaON1)) { EEPROM.put(dirHoraON1, horaON1); + } else { + horaON1 = EEPROM.read(dirHoraON1); } - if(validaHR(horaOFF1)) { + if(validaHR(horaOFF1) && horaOFF1 > horaON1) { EEPROM.put(dirHoraOFF1, horaOFF1); + } else { + EEPROM.put(dirHoraOFF1, horaON1 + 1); + horaOFF1 = EEPROM.read(dirHoraOFF1); } if(validaHR(horaON2) && horaON2 > horaOFF1) { EEPROM.put(dirHoraON2, horaON2); } else if(validaHR(horaON1)) { EEPROM.put(dirHoraON2, horaON1); - } // validacion incompleta - if(validaHR(horaOFF2) && horaOFF2 > horaON2 && horaOFF2 > horaOFF1) { + EEPROM.put(dirHoraOFF2, horaOFF1); + horaON2 = EEPROM.read(dirHoraON2); + horaOFF2 = EEPROM.read(dirHoraOFF2); + } + if(validaHR(horaOFF2) && horaOFF2 > horaON2) { EEPROM.put(dirHoraOFF2, horaOFF2); - } else if(validaHR(horaOFF1)) { + } else { + EEPROM.put(dirHoraON2, horaON1); EEPROM.put(dirHoraOFF2, horaOFF1); } break; From 2e9735d3661cfc8b80e0be152dc7c3c91c66c1ba Mon Sep 17 00:00:00 2001 From: "jp.av.dev" Date: Sun, 11 Jul 2021 03:57:01 -0400 Subject: [PATCH 8/9] nuevo intervalo del estado 1(AUTO) > intervalo loop --- caldera_rata_deb/caldera_rata_deb.ino | 34 +++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/caldera_rata_deb/caldera_rata_deb.ino b/caldera_rata_deb/caldera_rata_deb.ino index a659e1f..1c4fb17 100644 --- a/caldera_rata_deb/caldera_rata_deb.ino +++ b/caldera_rata_deb/caldera_rata_deb.ino @@ -15,9 +15,10 @@ Servo myservo_X; RTC_DS3231 reloj; volatile DateTime fecha; -unsigned long intervalo_loop=2500; -unsigned long previoMillisTermo=0; +unsigned long intervalo_loop=100; unsigned long previoMillisLoop=0; +unsigned long previoMillisTermo=0; +unsigned long previoMillisAUTO=0; unsigned long actualMillis; byte horaON1; byte horaOFF1; @@ -63,17 +64,19 @@ void loop() { fecha = reloj.now(); switch(estado) { case 1: - Serial.println(estado); - if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || - ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { - termoACC(posON1, posON2); - Serial.println("TERMO ON"); - } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && - ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { - termoACC(posOFF1, posOFF2); - Serial.println("TERMO OFF"); + if ((unsigned long)(actualMillis - previoMillisAUTO) >= 2000) { + Serial.println(estado); + if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || + ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { + termoACC(posON1, posON2); + Serial.println("TERMO ON"); + } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && + ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { + termoACC(posOFF1, posOFF2); + Serial.println("TERMO OFF"); + } } - //previoMillisLoop = millis(); + previoMillisAUTO = millis(); break; case 2: // ajustaReloj @@ -201,17 +204,18 @@ void termoACC(byte pos1, byte pos2) { if(espera==0) { if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) { myservo_X.write(pos2); - previoMillisTermo=millis(); espera=1; cont++; + previoMillisTermo=millis(); } } + actualMillis=millis(); if(espera==1) { if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) { myservo_X.write(pos1); - previoMillisTermo=millis(); espera=0; cont++; + previoMillisTermo=millis(); } } } while (cont<4); @@ -243,7 +247,7 @@ void setVarsEP(){ horaON2 = EEPROM.read(dirHoraON2); horaOFF2 = EEPROM.read(dirHoraOFF2); } - if(validaHR(horaOFF2) && horaOFF2 > horaON2) { + if(validaHR(horaOFF2) && (horaOFF2 > horaON2) && (horaOFF2 > horaOFF1)) { EEPROM.put(dirHoraOFF2, horaOFF2); } else { EEPROM.put(dirHoraON2, horaON1); From ebe226340c1a2d17aa9bdcc06b7f51cb08f630fa Mon Sep 17 00:00:00 2001 From: "jp.av.dev" Date: Sun, 11 Jul 2021 20:18:47 -0400 Subject: [PATCH 9/9] OK para funcionamiento autonomo y/o comandado por puerto Serie --- caldera_rata/caldera_rata.ino | 143 +++++++++------------------------- 1 file changed, 35 insertions(+), 108 deletions(-) diff --git a/caldera_rata/caldera_rata.ino b/caldera_rata/caldera_rata.ino index 020df53..7aeeb84 100644 --- a/caldera_rata/caldera_rata.ino +++ b/caldera_rata/caldera_rata.ino @@ -15,9 +15,10 @@ Servo myservo_X; RTC_DS3231 reloj; volatile DateTime fecha; -unsigned long intervalo_loop=2500; -unsigned long previoMillisTermo=0; +unsigned long intervalo_loop=500; unsigned long previoMillisLoop=0; +unsigned long previoMillisTermo=0; +unsigned long previoMillisAUTO=0; unsigned long actualMillis; byte horaON1; byte horaOFF1; @@ -45,8 +46,6 @@ void setup() { estado=1; estado_termo=0; start=1; - //ajustaReloj(2021, 7, 8, 4, 59, 50); - //reloj.adjust(DateTime(__DATE__, __TIME__)); } void loop() { @@ -58,22 +57,21 @@ void loop() { } actualMillis = millis(); if ((unsigned long)(actualMillis - previoMillisLoop) >= intervalo_loop) { - printFecha(); printVars(); fecha = reloj.now(); switch(estado) { case 1: - Serial.println(estado); - if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || - ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { - termoACC(posON1, posON2); - Serial.println("TERMO ON"); - } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && - ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { - termoACC(posOFF1, posOFF2); - Serial.println("TERMO OFF"); + // funcionamiento autonomo, por defecto + if ((unsigned long)(actualMillis - previoMillisAUTO) >= 2000) { + if((((fecha.hour() >= horaON1) && (fecha.hour() < horaOFF1)) || + ((fecha.hour() >= horaON2) && (fecha.hour() < horaOFF2))) && (estado_termo==0)) { + termoACC(posON1, posON2); + } else if((((fecha.hour() < horaON1) || (fecha.hour() >= horaOFF1)) && + ((fecha.hour() < horaON2) || (fecha.hour() >= horaOFF2))) && (estado_termo==1)) { + termoACC(posOFF1, posOFF2); + } } - //previoMillisLoop = millis(); + previoMillisAUTO = millis(); break; case 2: // ajustaReloj @@ -93,6 +91,7 @@ void loop() { estado = 1; break; case 5: + // ON y OFF manual if(espera_man) { if(manual == 1) { termoACC(posON1, posON2); @@ -103,12 +102,11 @@ void loop() { } break; case 6: + // movimiento 'libre' if(validaPOS(posTEMP)) { myservo_X.write(posTEMP); estado_termo=0; } - // debe reaccionar a los parametros enviados por Serial - // movimiento libre, termoACC, ajustaReloj, configs, etc. break; default: estado=1; @@ -138,7 +136,6 @@ void leerDatos() { hr = Serial.readStringUntil(',').toInt(); mn = Serial.readStringUntil(',').toInt(); sg = Serial.readStringUntil(',').toInt(); - Serial.print("Datos leidos :"+(String)di+"/"+(String)me+"/"+(String)an+" "+(String)hr+":"+(String)mn+":"+(String)sg); break; case 3: horaON1 = Serial.readStringUntil(',').toInt(); @@ -163,8 +160,6 @@ void leerDatos() { limpiaSerial(); } limpiaSerial(); - } else { - Serial.println("Nada q hacer"); } } @@ -176,20 +171,6 @@ void limpiaSerial() { } } -//como bash: cut -d -//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]) : ""; -//} void termoACC(byte pos1, byte pos2) { myservo_X.write(pos1); @@ -201,17 +182,18 @@ void termoACC(byte pos1, byte pos2) { if(espera==0) { if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) { myservo_X.write(pos2); - previoMillisTermo=millis(); espera=1; cont++; + previoMillisTermo=millis(); } } + actualMillis=millis(); if(espera==1) { if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) { myservo_X.write(pos1); - previoMillisTermo=millis(); espera=0; cont++; + previoMillisTermo=millis(); } } } while (cont<4); @@ -226,18 +208,27 @@ void setVarsEP(){ case 3: if(validaHR(horaON1)) { EEPROM.put(dirHoraON1, horaON1); + } else { + horaON1 = EEPROM.read(dirHoraON1); } - if(validaHR(horaOFF1)) { + if(validaHR(horaOFF1) && horaOFF1 > horaON1) { EEPROM.put(dirHoraOFF1, horaOFF1); + } else { + EEPROM.put(dirHoraOFF1, horaON1 + 1); + horaOFF1 = EEPROM.read(dirHoraOFF1); } if(validaHR(horaON2) && horaON2 > horaOFF1) { EEPROM.put(dirHoraON2, horaON2); } else if(validaHR(horaON1)) { EEPROM.put(dirHoraON2, horaON1); - } // validacion incompleta - if(validaHR(horaOFF2) && horaOFF2 > horaON2 && horaOFF2 > horaOFF1) { + EEPROM.put(dirHoraOFF2, horaOFF1); + horaON2 = EEPROM.read(dirHoraON2); + horaOFF2 = EEPROM.read(dirHoraOFF2); + } + if(validaHR(horaOFF2) && (horaOFF2 > horaON2) && (horaOFF2 > horaOFF1)) { EEPROM.put(dirHoraOFF2, horaOFF2); - } else if(validaHR(horaOFF1)) { + } else { + EEPROM.put(dirHoraON2, horaON1); EEPROM.put(dirHoraOFF2, horaOFF1); } break; @@ -255,8 +246,6 @@ void setVarsEP(){ EEPROM.put(dirPosOFF2, posOFF2); } break; - default: - Serial.print("default setVarsEP"); } } @@ -288,74 +277,12 @@ void ajustaReloj(int ano, byte mes, byte dia, byte hra, byte mins, byte segs ) { (segs >= 0 && segs < 60)) { DateTime dt(ano, mes, dia, hra, mins, segs); reloj.adjust(dt); - Serial.println("Set RTC: valido"); - } else { - Serial.println("Set RTC: invalido"); } } -//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; - } - } -} - -// Enviar hora y fecha por puerto serie -void printFecha() { - 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.print(getHora('s')); - Serial.print(" Día de la semana: "); - Serial.println((String)fecha.dayOfTheWeek()); -} - void printVars() { - Serial.println("Variables: "+(String)horaON1+", "+(String)horaOFF1+", "+ - (String)horaON2+", "+(String)horaOFF2+", "+ - (String)posON1+", "+(String)posON2+", "+ - (String)posOFF1+", "+(String)posOFF2); + Serial.println((String)estado+","+(String)estado_termo+","+(String)horaON1+","+(String)horaOFF1+","+(String)horaON2+","+ + (String)horaOFF2+","+(String)posON1+","+(String)posON2+","+(String)posOFF1+","+(String)posOFF2+","+ + (String)fecha.hour()+","+(String)fecha.minute()+","+(String)fecha.second()+","+(String)fecha.day()+","+ + (String)fecha.month()+","+(String)fecha.year()); }