You can use a so-called struct to group all information for a pin.
struct PININFO
{
byte pinNo; // pin number
byte currentState; // current state of pin
unsigned long durationLow; // how long it must be LOW
unsigned long durationHigh; // how long it must be high
unsigned long startTime; // start time of delay
};
And create an array of those structs (two in below example)
// information about the output pins
PININFO pininfo[] =
{
{13, LOW, 1000, 3000, 0}, // pin 13, initial state LOW, high 1000ms, low 3000ms, starttime not set
{12, LOW, 7000, 10000, 0},
};
The initialisation can be done in a loop in setup()
void setup()
{
Serial.begin(9600);
Serial.print("Number of pins in array: "); Serial.println(sizeof(pininfo) / sizeof(PININFO));
unsigned long currentTime = millis();
// set pin initial state and set pin to output
for (int cnt = 0; cnt < sizeof(pininfo) / sizeof(PININFO); cnt++)
{
// writte initial state
digitalWrite(pininfo[cnt].pinNo, pininfo[cnt].currentState);
// set pin mode
pinMode(pininfo[cnt].pinNo, OUTPUT);
// set the start time for of the initial state
pininfo[cnt].startTime = currentTime;
}
}
And in loop(), you can loop
through the array elements
void loop()
{
// just for serial debug output
char txtBuffer[41];
// current pin to process
static byte pinToProcess = 0;
// current time
unsigned long currentTime = millis();
// if current state is HIGH
if (pininfo[pinToProcess].currentState == HIGH)
{
// and HIGH duration lapsed
if (currentTime - pininfo[pinToProcess].startTime >= pininfo[pinToProcess].durationHigh)
{
// switch to LOW
pininfo[pinToProcess].currentState = LOW;
digitalWrite(pininfo[pinToProcess].pinNo, pininfo[pinToProcess].currentState);
// set startTime
pininfo[pinToProcess].startTime = currentTime;
// some debug info
sprintf(txtBuffer, ": pin %d, state %s", pininfo[pinToProcess].pinNo, pininfo[pinToProcess].currentState == LOW ? "LOW" : "HIGH");
Serial.print(currentTime); Serial.println(txtBuffer);
}
}
// if current state is LOW
else
{
// and LOW duration lapsed
if (currentTime - pininfo[pinToProcess].startTime >= pininfo[pinToProcess].durationLow)
{
// switch to HIGH
pininfo[pinToProcess].currentState = HIGH;
digitalWrite(pininfo[pinToProcess].pinNo, pininfo[pinToProcess].currentState);
// set startTime
pininfo[pinToProcess].startTime = currentTime;
// some debug info
sprintf(txtBuffer, ": pin %d, state %s", pininfo[pinToProcess].pinNo, pininfo[pinToProcess].currentState == LOW ? "LOW" : "HIGH");
Serial.print(currentTime); Serial.println(txtBuffer);
}
}
// next pin
pinToProcess++;
// limit to number of pins in PININFO array
pinToProcess %= sizeof(pininfo) / sizeof(PININFO);
}
The above code will independently switch any of the relays on and off.