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()); }