Turn on every hour for 80 seconds.

Hi everyone.
I have this for turn on 3 lights at certain hours, and put off at certain hours (this is working)... now i want to add a device that i need is on every hour (for example at 13pm, at 14pm until 23pm) for 80 seconds
each time.

Can somebody give me a hand??
Thanks!

#include <Time.h>
#include <DS1307RTC.h>
#include <TimeAlarms.h>

int in1=4;
int in2=6;
int in3=2;

void setup () {  Serial.begin(9600);
  pinMode(in1, OUTPUT);
  digitalWrite(in1, HIGH);
  pinMode(in2, OUTPUT);
  digitalWrite(in2, HIGH);
  pinMode(in3, OUTPUT);
  digitalWrite(in3, HIGH);
  
 
  setSyncProvider(RTC.get);
  if (timeStatus() != timeSet)
    Serial.println("Fallo de RTC");
  else
    Serial.println("Sincronizado con RTC");

  Alarm.alarmRepeat(13,30,0, EventoEnciendeLuz1); //led
  Alarm.alarmRepeat(14,00,0, EventoEnciendeLuz2); //tubo largo
  Alarm.alarmRepeat(14,15,0, EventoEnciendeLuz3); //pll
  
  Alarm.alarmRepeat(00,00,00, EventoApagaLuz1); //
  Alarm.alarmRepeat(23,30,00, EventoApagaLuz2); //
  Alarm.alarmRepeat(23,10,00, EventoApagaLuz3); //
}


void loop() {
  digitalClockDisplay();
  Alarm.delay(1000);
}


void EventoEnciendeLuz1()  
{
  Serial.println("Encendiendo Luz 1!!!");
  digitalWrite(in1, LOW);
}

void EventoApagaLuz1()
{
  Serial.println("Apagando Luz 1!!!");
  digitalWrite(in1, HIGH);
}

void EventoEnciendeLuz2() 
{
  Serial.println("Encendiendo Luz 2!!!");
  digitalWrite(in2, LOW);
}

void EventoApagaLuz2()
{
  Serial.println("Apagando Luz 2!!!");
  digitalWrite(in2, HIGH);
}

void EventoEnciendeLuz3() 
{
  Serial.println("Encendiendo Luz 3!!!");
  digitalWrite(in3, LOW);
}

void EventoApagaLuz3()
{
  Serial.println("Apagando Luz 3!!!");
  digitalWrite(in3, HIGH);
}



void digitalClockDisplay() {
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}
 
void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

When you create one function to turn it on and one function to turn it off, then you can create all the timers for all the hours.

Alarm.alarmRepeat( 13, 0, 0, turn_it_on() ); 
Alarm.alarmRepeat( 13, 1, 20, turn_it_off() ); 
Alarm.alarmRepeat( 14, 0, 0, turn_it_on() ); 
Alarm.alarmRepeat( 14, 1, 20, turn_it_off() ); 
Alarm.alarmRepeat( 15, 0, 0, turn_it_on() ); 
Alarm.alarmRepeat( 15, 1, 20, turn_it_off() ); 
and so on

When the moment that it turns on can be any time, then I think that you can also use Alarm.timerRepeat() for every hour and inside the function to turn it on, call Alarm.timeOnce() to call a function to turn it off after 80 seconds.
You could also use a millis() timer.

Koepel:

Alarm.alarmRepeat( 13, 0, 0, turn_it_on() ); 

Alarm.alarmRepeat( 13, 1, 20, turn_it_off() );
Alarm.alarmRepeat( 14, 0, 0, turn_it_on() );
Alarm.alarmRepeat( 14, 1, 20, turn_it_off() );
Alarm.alarmRepeat( 15, 0, 0, turn_it_on() );
Alarm.alarmRepeat( 15, 1, 20, turn_it_off() );
and so on

doesn't the above approach require 2 entries for every event. Shouldn't there just be some two calls: when to turn on and when to turn off, in setup() and some alarmer call in loop()?

consider the following

// turn something on for limited time periodically

#define ON   LOW
#define OFF  HIGH

byte pin = 10;

#if 1
# define PERIOD      (10*1000L)
# define ON_PERIOD   (1*1000L)
#else
# define PERIOD      (3600*1000L)
# define ON_PERIOD   (80*1000L)
#endif

unsigned long msecLst;

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (9600);
    digitalWrite (pin, OFF);
    pinMode      (pin, OUTPUT);

    msecLst = millis();
}

// -----------------------------------------------------------------------------
void loop (void)
{
    unsigned long msec = millis();

    if (msec - msecLst > PERIOD)  {
        msecLst = msec;
        digitalWrite (pin, ON);
    }

    if (msec - msecLst > ON_PERIOD)  {
        digitalWrite (pin, OFF);
    }
}

Koepel:
When you create one function to turn it on and one function to turn it off, then you can create all the timers for all the hours.

Alarm.alarmRepeat( 13, 0, 0, turn_it_on() ); 

Alarm.alarmRepeat( 13, 1, 20, turn_it_off() );
Alarm.alarmRepeat( 14, 0, 0, turn_it_on() );
Alarm.alarmRepeat( 14, 1, 20, turn_it_off() );
Alarm.alarmRepeat( 15, 0, 0, turn_it_on() );
Alarm.alarmRepeat( 15, 1, 20, turn_it_off() );
and so on




When the moment that it turns on can be any time, then I think that you can also use Alarm.timerRepeat() for every hour and inside the function to turn it on, call Alarm.timeOnce() to call a function to turn it off after 80 seconds.
You could also use a millis() timer.

Thanks. You mean for example call Alarm.alarmRepeat to turn on... put a delay(80000) inside and after that a turn off. for every hour i want??

(i dont speak english...)
Thanks.

I mean this:

#include <TimeAlarms.h>
#include <Time.h>
#include <DS1307RTC.h>


int in1=4;
int in2=6;
int in3=2;
int in4=8;

void setup () {  Serial.begin(9600);
  pinMode(in1, OUTPUT);
  digitalWrite(in1, HIGH);
  pinMode(in2, OUTPUT);
  digitalWrite(in2, HIGH);
  pinMode(in3, OUTPUT);
  digitalWrite(in3, HIGH);
  pinMode(in4, OUTPUT);
  digitalWrite(in4, HIGH);
 
  setSyncProvider(RTC.get);
  if (timeStatus() != timeSet)
    Serial.println("Fallo de RTC");
  else
    Serial.println("Sincronizado con RTC");

////TUBOS DE LUZ........... (ALAMOS DE GAS.......)///
  Alarm.alarmRepeat(13,30,0, EventoEnciendeLuz1); //led
  Alarm.alarmRepeat(14,00,0, EventoEnciendeLuz2); //tubo largo
  Alarm.alarmRepeat(14,15,0, EventoEnciendeLuz3); //pll
  
  Alarm.alarmRepeat(00,00,00, EventoApagaLuz1); //4
  Alarm.alarmRepeat(23,30,00, EventoApagaLuz2); //3
  Alarm.alarmRepeat(23,10,00, EventoApagaLuz3); //1

////////////////////////////////////////////////////////
  Alarm.alarmRepeat(13,00,0, Electrolisis);
  Alarm.alarmRepeat(14,00,0, Electrolisis);
  Alarm.alarmRepeat(15,00,0, Electrolisis);
  Alarm.alarmRepeat(16,00,0, Electrolisis);
  Alarm.alarmRepeat(17,00,0, Electrolisis);
  Alarm.alarmRepeat(18,00,0, Electrolisis);
  Alarm.alarmRepeat(19,00,0, Electrolisis);
  Alarm.alarmRepeat(20,00,0, Electrolisis);
  Alarm.alarmRepeat(21,00,0, Electrolisis);
  Alarm.alarmRepeat(22,00,0, Electrolisis);
  Alarm.alarmRepeat(23,00,0, Electrolisis);
}


void loop() {
  digitalClockDisplay();
  Alarm.delay(1000);
}


void EventoEnciendeLuz1()  
{
  Serial.println("Encendiendo Luz 1!!!");
  digitalWrite(in1, LOW);
}

void EventoApagaLuz1()
{
  Serial.println("Apagando Luz 1!!!");
  digitalWrite(in1, HIGH);
}

void EventoEnciendeLuz2() 
{
  Serial.println("Encendiendo Luz 2!!!");
  digitalWrite(in2, LOW);
}

void EventoApagaLuz2()
{
  Serial.println("Apagando Luz 2!!!");
  digitalWrite(in2, HIGH);
}

void EventoEnciendeLuz3() 
{
  Serial.println("Encendiendo Luz 3!!!");
  digitalWrite(in3, LOW);
}

void EventoApagaLuz3()
{
  Serial.println("Apagando Luz 3!!!");
  digitalWrite(in3, HIGH);
}

void Electrolisis()
{
  Serial.println("Hago electrolisis");
  digitalWrite(in4,LOW);
  delay(80000);
  digitalWrite(in4,HIGH);  
}


void digitalClockDisplay() {
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}
 
void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

With the Alarm.timerRepeat() and Alarm.timeOnce() combination I meant this:

void setup()
{
  Alarm.timerRepeat( 3600, turn_it_on());   // 3600 seconds is one hour
}

void loop()
{
  Alarm.delay(0);
  ...
}

void turn_it_on()
{
   digitalWrite( ...
   Alarm.timerOnce( 80, turn_it_off());   // 80 seconds
}

void turn_it_off()
{
  digitalWrite( ...
}

That will execute every hour.

In your second sketch you use a delay of 80 seconds. During those 80 seconds you can do nothing in the loop(). If that is no problem, then you should use Alarm.delay() as you can read here: TimeAlarms Library, Run Functions At Specific Times.

My first example was simple. If you want, you can put it in a loop()

for( int hour = 13; hour <= 23; hour++)
{
  Alarm.alarmRepeat( hour, 0, 0, turn_it_on() );
  Alarm.alarmRepeat( hour, 1, 20, turn_it_off() ); 
}