You will probably want a real-time-clock module. This can be done with millis(), but it won't be accurate unless you measure how accurate millis() is on your particular board and make adjustments.
The basic logic is:
setup() {
make a note of the sketch start time;
start relay 1;
start relay 2;
}
loop() {
deal with relay 1;
deal with relay 2;
}
start_relay_1() {
turn relay 1 on;
note that we turned relay 1 on at the sketch start time
}
deal_with_relay_1() {
if we have finished with relay 1, return.
if the time since the sketch start time is 48 hours, then
turn relay 1 off
note that we have finished with relay 1
return
if relay 1 is off, and it's half hour since we turned it off
turn relay 1 on
note that we turned relay 1 on at the current time
return
if relay 1 is on, and its an hour since we turned it on
turn relay 1 off
note that we turned relay 1 off at the current time
return
otherwise
do nothing
}
const int ledPin = 4; // the number of the LED pin
const int ledPin2 = 5;
// Variables will change :
int ledState = LOW; // ledState used to set the LED
int ledState2 = LOW;
int counter=0;
int counter2=0;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long previousMillis2 = 0;
// constants won't change :
const long interval1 = 1000; // interval at which to blink (milliseconds)
const long interval12 = 1000;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();
if(currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH ;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
if (ledState2 == LOW)
ledState2 = HIGH;
else
ledState2 = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin2, ledState2);
unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();
Why do you need two copies of now?
Meaningful names ARE important. previousMillis doesn't tell us WHAT happened then. lastRelay1ToggleTime does.
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH ;
else
ledState = LOW;
or
ledState = !ledState;
Though why the hell the variable is called ledState when it contains the state of a relay is a damned mystery. Same with ledPin that holds the number of a pin that a relay is attached to.