every hour turn on/off

Hello ,
i need to run 2 device by relay with arduino nano every one hour.

device 1 turn on and working for 30 seconds then turn off
after that device 2 turn on and work for 30 seconds and then turn off

i read many document about it but still no idea what to do using timer or delay and which one is better.

any advice or help much appreciate

How accurate do you require the timing to be over a period of days ?

The Arduino itself is not very good at keeping time so if accuracy is required then you will need an accurate source of time such as a Real Time Clock

Your requirements for the program are actually very simple if that is all the program is required to do. Even a series of delay()s would do what you want.

What have you tried so far ?

If that is all that you want to do, using delay() would be simple as long as nothing needs to happen during the delay.

It also depends on the accuracy of the 1 hour and 30 second timings. If you want precision, you will need an external source of accurate time like a Read Time Clock (RTC) or access to a NTP server.

Doing the timing, millis() (or micros()) is always preferred over delay() in any program that needs to be responsive. Here is a good beginners guide to millis() for timing.

I'd recommend learning about state machines. You requirements are easily expressed as a state machine.

MarkT:
I'd recommend learning about state machines. You requirements are easily expressed as a state machine.

Why complicate things if all that is required is a series of pauses and pin state changes, but we are waiting to hear whether the requirement is as simple as originally posted so all bets are off

thanks for all reply,
at first i do not need accuracy.
Blew the code im using

the problem is as soon as i turn on system both relay is on mode

#define RELAY1  7
#define RELAY2  8
#define an_hour 3600000UL // 1 hous 
unsigned long startTime;                       
void setup()

{    


  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT); 
  startTime = millis();      

}

  void loop()
 

{
    if (millis() - startTime > an_hour)
    {
   digitalWrite(RELAY1,0);           // Turns ON Relays 1
   delay(10000);                                      // Wait 5 seconds

   digitalWrite(RELAY1,1);          // Turns Relay Off
      delay(2000);

    digitalWrite(RELAY2,0);           // Turns ON Relays 1
      delay(5000);                                      // Wait 2 seconds

   digitalWrite(RELAY2,1);          // Turns Relay Off
   delay(2000);
    }
}

Is it enough to turn the relays off in setup immediately after your pinmode statements?

If you are going to use delay() for some of your timing I would not bother with using millis() for any of it.
Just use a series of delay()s with the actions needed at the end of each period.

wildbill:
Is it enough to turn the relays off in setup immediately after your pinmode statements?

Yes it can be after Relay 1 turn off then Relay 2 turn on .
but as soon as i turn on the arduino both relay become turn on and noting happen . i think it should be some problem in code im using .

i can to use delay but the point im using this method is to learn something that useful for other project

When you turn the arduino on and set pinmode for output, those two pins will default to low, which is what turns your relays on. digitalWrite them both to HIGH in setup.

digitalWrite them both to HIGH in setup.

BEFORE pinMode(OUTPUT);

  digitalWrite(RELAY1,HIGH);
  digitalWrite(RELAY2,HIGH);
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  startTime = millis();

Sadly, digitalWrite HIGH to an input (the default) enables the pullup. I don't see how to have an output start with high.

outsider:
BEFORE pinMode(OUTPUT);

  digitalWrite(RELAY1,HIGH);

digitalWrite(RELAY2,HIGH);
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  startTime = millis();

thank you so much for your help

here final code that working very well

#define RELAY1  8 //Relay1
#define RELAY2  7 //Relay2
#define an_hour 60000UL // 1 second = 1000 milliseconds,,1 minute = 60 seconds,,1 hour = 60 minutes,,12 hours = 12 * 60 * 60 * 1000 = 43,200,000
unsigned long startTime;                       
void setup()

{    
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT); 
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  startTime = millis();      

}

  void loop()
 

{
   if (millis() - startTime > an_hour)
    {
   digitalWrite(RELAY2,LOW);           // Turns ON Relays 1
   delay(10000);                                      // Wait 2 seconds

   digitalWrite(RELAY1,LOW);           // Turns ON Relays 1
   delay(20000);                                      // Wait 2 seconds

   digitalWrite(RELAY2,HIGH);          // Turns Relay Off
   delay(5000);
   
   digitalWrite(RELAY1,HIGH);          // Turns Relay Off
   //delay(2000);

  startTime = millis();
    }
}

Even simpler

void loop()
{
  delay(an_hour);
  digitalWrite(RELAY2, LOW);          // Turns ON Relays 1
  delay(10000);                             // Wait 2 seconds (comment or code is wrong)
  digitalWrite(RELAY1, LOW);          // Turns ON Relays 1
  delay(20000);                             // Wait 2 seconds (comment or code is wrong)
  digitalWrite(RELAY2, HIGH);         // Turns Relay Off
  delay(5000);
  digitalWrite(RELAY1, HIGH);         // Turns Relay Off
  //delay(2000);
}