Send message every 5 minutes via the Arduino RTC module

0

I'm using an Arduino RTC module, which gives me date and time. I would like to know how I can send a message every 5 minutes. Below is the algorithm I'm using, the variable that prints the time is the variable ( rtc.getTimeStr() ). Note: The "delay" instruction should not be used. If it was set in the algorithm " rtc.setTime(23, 00, 0); " a message should be sent at 23:05:00

#include <DS1307.h>

DS1307 rtc(D1, D2);

void setup()
{
  //Aciona o relogio
  rtc.halt(false);

  //As linhas abaixo setam a data e hora do modulo
  //e podem ser comentada apos a primeira utilizacao

  rtc.setDOW(Segunda);      //Define o dia da semana

  rtc.setTime(23, 00, 0);     //Define o horario

  rtc.setDate(20, 07, 2017);   //Define o dia, mes e ano

  //Definicoes do pino SQW/Out

  rtc.setSQWRate(SQW_RATE_1);
  rtc.enableSQW(true);

  Serial.begin(2400);
}

void loop()
{
//  Mostra as informações no Serial Monitor

 Serial.print("Hora : ");

 Serial.print(rtc.getTimeStr());

  Serial.print(" ");

 Serial.print("Data : ");

  Serial.print(rtc.getDateStr(FORMAT_SHORT));

 Serial.print(" ");

  Serial.println(rtc.getDOWStr(FORMAT_SHORT));

  //Aguarda 1 segundo e repete o processo

  delay (1000);
}
    
asked by anonymous 20.07.2017 / 20:28

1 answer

0

To avoid using delays you can use interrupts, even for writing that is being done second by second.

As the maximum time of an interrupt is approximately 8.3 seconds, due to the way the library works, to simulate something every 5 minutes, some logic is needed to count the seconds passed.

You'll also need to use the TimerOne library listed in the Arduino

The code would look like the following:

#include <DS1307.h>
#include "TimerOne.h" //inclusão da biblioteca de timers

int segundos = 0; //para contabilização dos segundos passados

DS1307 rtc(D1, D2);

void setup()
{
  ... //o resto do código que estava no setup
  Timer1.initialize(1000000); //1 Milhão de microsegundos, logo 1 segundo
  Timer1.attachInterrupt(imprimeInformacoes); //a cada segundo chama esta função
}

void imprimeInformacoes(){
  segundos++; //aumenta a contagem de segundos

  if(segundos >= 300){ //se chegou a 5 minutos
     //envia mensagem
     segundos = 0; //reinicia a contabilização para a próxima mensagem
  }

  //Mostra as informações no Serial Monitor    
  Serial.print("Hora : ");    
  Serial.print(rtc.getTimeStr());    
  Serial.print(" ");    
  Serial.print("Data : ");    
  Serial.print(rtc.getDateStr(FORMAT_SHORT));    
  Serial.print(" ");    
  Serial.println(rtc.getDOWStr(FORMAT_SHORT));   
}

void loop()
{
  //agora vazio pois o código é chamado automaticamente pelo timer
}
It is especially important to mention that the arduino interrupt mechanism has quite considerable advantages over delays, because while a delay runs the arduino can not do anything else, such as reading or writing values on the pins. In an interrupt the same is no longer the case, the arduino is still able to do other processing that is needed while the time does not pass.

    
20.07.2017 / 22:46