Code for running multiple relays simultaneously


I'm an artist still relatively new to arduino programming and I need some assistance on a project I'm working on.

My project involves turning on three ceiling fan motors for a set amount of time then turning off through utilizing a 4-Channel Relay Module Board w/ Optocoupler Isolation. This seems fairly simple, and I've made multiple projects in the past utilizing variations of the "Blink" code and that I felt I had a good idea of where to go with this one. Unfortunately I'm realizing that the complexity of what I want to do requires the motors to all simultaneously cut on and off at their own respective different times. I've searched many forums and come across some different posts that were slightly helpful, but many of them utilized programs with a delay, and that seems to prevent the arduino from performing the simultaneous functions I desire and the flexibility I need with this program. I've also been messing with the "BlinkWithoutdDelay" code, which seems to be more of what I need, but I'm still not sure if this is the best answer, and how to adapt it.

If anyone has any suggestions for what would be the best code for the situation, or even know of a code I could experiment with and adapt, I would be greatly appreciative.

You can use millis() to time events. As a very quickly written example, this code will blink two relays at different rates with programmable on and off times.

#define RELAY1_ON_TIME  100
#define RELAY1_OFF_TIME 200
#define RELAY2_ON_TIME  300
#define RELAY2_OFF_TIME 400

byte relay1_state = LOW;
byte relay2_state = LOW;
unsigned long lastMillis1 = 0;
unsigned long lastMillis2 = 0;
byte relay1_pin = 2;
byte relay2_pin = 3;

void setup()
  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);
  digitalWrite(relay1_pin, LOW);
  digitalWrite(relay2_pin, LOW);

void loop()
  if (millis() - lastMillis1 >= ((relay1_state == HIGH) ? RELAY1_OFF_TIME : RELAY1_ON_TIME)) {
    lastMillis1 = millis();
    relay1_state = !relay1_state;
    digitalWrite(relay1_pin, relay1_state);
  if (millis() - lastMillis2 >= ((relay2_state == HIGH) ? RELAY2_OFF_TIME : RELAY2_ON_TIME)) {
    lastMillis1 = millis();
    relay2_state = !relay2_state;
    digitalWrite(relay2_pin, relay2_state);

This is great. Just the explanation I needed. Thank you!