2021-07-06 15:09:14 -04:00
|
|
|
#include <Servo.h>
|
|
|
|
#include <RTClib.h>
|
2021-07-08 21:50:59 -04:00
|
|
|
#include <EEPROM.h>
|
|
|
|
|
|
|
|
#define dirHoraON1 0
|
|
|
|
#define dirHoraOFF1 1
|
|
|
|
#define dirHoraON2 2
|
|
|
|
#define dirHoraOFF2 3
|
|
|
|
#define dirPosON1 4
|
|
|
|
#define dirPosON2 5
|
|
|
|
#define dirPosOFF1 6
|
|
|
|
#define dirPosOFF2 7
|
2021-07-06 15:09:14 -04:00
|
|
|
|
|
|
|
Servo myservo_X;
|
|
|
|
RTC_DS3231 reloj;
|
|
|
|
|
|
|
|
volatile DateTime fecha;
|
2021-07-09 03:57:01 -04:00
|
|
|
unsigned long intervalo_loop=2500;
|
2021-07-06 15:09:14 -04:00
|
|
|
unsigned long previoMillisTermo=0;
|
|
|
|
unsigned long previoMillisLoop=0;
|
|
|
|
unsigned long actualMillis;
|
2021-07-08 21:50:59 -04:00
|
|
|
byte horaON1;
|
|
|
|
byte horaOFF1;
|
|
|
|
byte horaON2;
|
|
|
|
byte horaOFF2;
|
|
|
|
byte posON1;
|
|
|
|
byte posON2;
|
|
|
|
byte posOFF1;
|
|
|
|
byte posOFF2;
|
2021-07-09 03:57:01 -04:00
|
|
|
byte posTEMP;
|
2021-07-08 21:50:59 -04:00
|
|
|
byte cont;
|
2021-07-06 15:09:14 -04:00
|
|
|
bool estado_termo;
|
|
|
|
bool espera;
|
2021-07-09 03:57:01 -04:00
|
|
|
bool espera_man;
|
2021-07-06 15:09:14 -04:00
|
|
|
bool start;
|
2021-07-09 03:57:01 -04:00
|
|
|
byte manual;
|
|
|
|
byte estado;
|
|
|
|
byte hr, mn, sg, di, me;
|
|
|
|
int an;
|
2021-07-06 15:09:14 -04:00
|
|
|
|
|
|
|
void setup() {
|
2021-07-07 16:01:19 -04:00
|
|
|
Serial.begin(9600);
|
|
|
|
myservo_X.attach(9);
|
|
|
|
reloj.begin();
|
2021-07-09 03:57:01 -04:00
|
|
|
estado=1;
|
2021-07-07 16:01:19 -04:00
|
|
|
estado_termo=0;
|
2021-07-08 21:50:59 -04:00
|
|
|
start=1;
|
2021-07-08 22:27:21 -04:00
|
|
|
ajustaReloj(2021, 7, 8, 4, 59, 50);
|
2021-07-08 21:50:59 -04:00
|
|
|
//reloj.adjust(DateTime(__DATE__, __TIME__));
|
2021-07-06 15:09:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2021-07-07 16:01:19 -04:00
|
|
|
if(start) {
|
|
|
|
myservo_X.write(30);
|
|
|
|
fecha=reloj.now();
|
2021-07-09 03:57:01 -04:00
|
|
|
cargarVarsControl();
|
|
|
|
start=0;
|
2021-07-07 16:01:19 -04:00
|
|
|
}
|
|
|
|
actualMillis = millis();
|
|
|
|
if ((unsigned long)(actualMillis - previoMillisLoop) >= intervalo_loop) {
|
2021-07-08 21:50:59 -04:00
|
|
|
printFecha();
|
2021-07-08 22:27:21 -04:00
|
|
|
printVars();
|
2021-07-07 16:01:19 -04:00
|
|
|
fecha = reloj.now();
|
2021-07-09 03:57:01 -04:00
|
|
|
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;
|
2021-07-07 16:01:19 -04:00
|
|
|
}
|
2021-07-09 03:57:01 -04:00
|
|
|
leerDatos();
|
2021-07-07 16:01:19 -04:00
|
|
|
previoMillisLoop = millis();
|
2021-07-06 15:09:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 03:57:01 -04:00
|
|
|
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]) : "";
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:50:59 -04:00
|
|
|
void termoACC(byte pos1, byte pos2) {
|
|
|
|
myservo_X.write(pos1);
|
2021-07-07 16:01:19 -04:00
|
|
|
espera=0;
|
|
|
|
cont=0;
|
|
|
|
previoMillisTermo=millis();
|
|
|
|
do{
|
|
|
|
actualMillis=millis();
|
|
|
|
if(espera==0) {
|
|
|
|
if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) {
|
2021-07-08 21:50:59 -04:00
|
|
|
myservo_X.write(pos2);
|
2021-07-07 16:01:19 -04:00
|
|
|
previoMillisTermo=millis();
|
|
|
|
espera=1;
|
|
|
|
cont++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(espera==1) {
|
|
|
|
if ((unsigned long)(actualMillis - previoMillisTermo) >= 500) {
|
2021-07-08 21:50:59 -04:00
|
|
|
myservo_X.write(pos1);
|
2021-07-07 16:01:19 -04:00
|
|
|
previoMillisTermo=millis();
|
|
|
|
espera=0;
|
|
|
|
cont++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (cont<4);
|
2021-07-08 21:50:59 -04:00
|
|
|
estado_termo=!estado_termo;
|
2021-07-06 15:09:14 -04:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:50:59 -04:00
|
|
|
|
2021-07-09 03:57:01 -04:00
|
|
|
void cargarVarsControl() {
|
2021-07-08 21:50:59 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2021-07-07 16:01:19 -04:00
|
|
|
|
2021-07-06 15:09:14 -04:00
|
|
|
//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(":");
|
2021-07-08 22:27:21 -04:00
|
|
|
Serial.print(getHora('s'));
|
|
|
|
Serial.print(" Día de la semana: ");
|
2021-07-06 15:09:14 -04:00
|
|
|
Serial.println((String)fecha.dayOfTheWeek());
|
|
|
|
}
|
2021-07-08 22:27:21 -04:00
|
|
|
|
|
|
|
void printVars() {
|
|
|
|
Serial.println("Variables: "+(String)horaON1+", "+(String)horaOFF1+", "+
|
|
|
|
(String)horaON2+", "+(String)horaOFF2+", "+
|
|
|
|
(String)posON1+", "+(String)posON2+", "+
|
|
|
|
(String)posOFF1+", "+(String)posOFF2);
|
|
|
|
}
|