Hola, estoy haciendo un trabajo escolar y necesito que cierto código se ejecute a determinadas horas, para estoy estoy utilizando las librearías de TimeAlarms y Time, el problema viene cuando llega la hora de ejecutar el código, ya que este solo esta activo durante 1 segundo y lo que necesito es que este funcionando durante todo el intervalo de tiempo; dejo el código aquí por si alguien es tan amable de ayudarme.
También quería decir que soy nuevo en el mundo de arduino y la programación y lo que tengo lo he hecho investigando aquí en el foro y con ayuda de google
Codigo:
// LIBRERIAS A UTILIZAR
#include <infrarrojo.h>
#include <Wire.h>
#include "RTClib.h"
#include <Time.h>
#include <TimeAlarms.h>
RTC_DS1307 RTC; //Ininializar el RTC
// DEFINICION DE PINES A UTILIZAR
infrarrojo estado(12); //DEFINICION DEL PIN DEL ARDUINO A USAR
int VALOR; // VARIBLE QUE RECIBE EL DATO
int led_estado; // VARIABLE
const int pingTrigger = 8; // Trigger para los 2 sensores
const int Sensor_1 = 7; // Echo para sensor 1
const int Sensor_2 = 6; // Echo para sensor 2
const int rele = 9; // Salida para el relevador
//Setup Function
void setup() {
Serial.begin(9600);
Wire.begin();
RTC.begin();
pinMode(pingTrigger, OUTPUT);
pinMode(Sensor_1, INPUT);
pinMode(Sensor_2, INPUT);
pinMode(rele, OUTPUT);
if (! RTC.isrunning()) {
Serial.println(F("RTC is NOT running!"));
}
else {
Serial.println(F("RTC is running!"));
}
//Sincronizar hora de la computadora con el RTC cada vez que es compilado el programa (Hacer comentario < // > una vez sincronizada la hora)
RTC.adjust(DateTime(__DATE__, __TIME__));
setSyncProvider( syncProvider );
//SET ALARMS:
Alarm.alarmRepeat(14, 16, 30, Activar_Sensores); //
Alarm.alarmRepeat(14, 17, 30, SensoresOff); //
Alarm.alarmRepeat(15, 16, 30, Activar_Sensores); //
Alarm.alarmRepeat(15, 17, 30, SensoresOff); //
Serial.println(F("Startup Completed"));
}
void loop() {
Serial.print(F("Time: "));
printCurrentTime();
Alarm.delay(1000);
}
//FUNCIONES
//Funcion para sincronizar arduino con el RTC
time_t syncProvider() {
return RTC.now().unixtime();
}
//Funcion para Desactivar los Sensores
void SensoresOff() {
Serial.println(F(" - Sensores Desaactivados"));
}
//Funncion para impirmir la hora
void printCurrentTime() {
DateTime now = RTC.now();
if ((now.hour()) < 10) Serial.print('0');
Serial.print(now.hour(), DEC);
Serial.print(':');
if ((now.minute()) < 10) Serial.print('0');
Serial.print(now.minute(), DEC);
Serial.print(':');
if ((now.second()) < 10) Serial.print('0');
Serial.print(now.second(), DEC);
Serial.println();
}
//Funcion para Activar todos los Sensores
void Activar_Sensores() {
// SENSOR INFRARROJO
Serial.println("\n leyendo estado sensor: \n");//IMPRIME MENSAJE EN PC
Serial.println(estado.lectura(VALOR));//IMPRIME EL ESTADO DEL Vo DEL SENSOR MEDIANTE LA VARIABLE VALOR
led_estado = estado.lectura(VALOR);//LED QUE RECOGE EL ESTADO DEL SENSOR
//// DAR VALORES EN cm
long duracion, cm, cm2;
/////
// SENSOR ULTRASONICO 1
digitalWrite(pingTrigger, LOW);
delayMicroseconds(2);
digitalWrite(pingTrigger, HIGH);
delayMicroseconds(5);
digitalWrite(pingTrigger, LOW);
duracion = pulseIn(Sensor_1, HIGH);
cm = microsecondsToCentimeters(duracion);
Serial.println("Sensor 1");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
// SENSOR ULTRASONICO 2
digitalWrite(pingTrigger, LOW);
delayMicroseconds(2);
digitalWrite(pingTrigger, HIGH);
delayMicroseconds(5);
digitalWrite(pingTrigger, LOW);
duracion = pulseIn(Sensor_2, HIGH);
cm2 = microsecondsToCentimeters(duracion);
Serial.println("Sensor 2");
Serial.print(cm2);
Serial.print("cm");
Serial.println();
delay(100);
delay(1000);
/////////////////// MODIFICACIONES PARA ACTIVAR EL RELÊ
if (cm >= 10 || cm2 >= 10 || led_estado == 0) {
delay(10000);
digitalWrite (rele, LOW);
Serial.println("Sensor Activado");
}
else {
digitalWrite(rele, HIGH);
}
} ///final
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}