Toggle Button Timeout Using Millis()

Here's some example code using millis() for run time out after the togglebutton press.

Your code is perhaps best rewritten as a state machine, but I have used your conditional logic structure and added boolean control variables to the "if" conditional statements.

There are control variables and timing variables to start/finish the time out period after a togglebutton press. There are also control variables to turn the relays off when the untimed buttons are released. You cannot do the untimed turn offs with a simple "else" conditions because relays 1 and 2 are controlled by all 4 buttons.

I used the indicator led on pin 13 to help show the timing.

int relay1 = 0;
int relay2 = 1;
int relay3 = 2;
int relay4 = 3;
int button1 = 4;
int togglebutton1 = 5;
int button2 = 6;
int togglebutton2 = 7;

//variables to control state and timeout after toggle button press
unsigned long currentTime;
unsigned long startTime1;
unsigned long startTime2;
boolean timing1 = false;
boolean timing2 = false;
unsigned long runTime1 = 10000;
unsigned long runTime2 = 10000;

//variables to control on/off after button press and hold
boolean holding1 = false;
boolean holding2 = false;


void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode (button1, INPUT_PULLUP);
  pinMode (togglebutton1, INPUT_PULLUP);
  pinMode (button2, INPUT_PULLUP);
  pinMode (togglebutton2, INPUT_PULLUP);
  //Test led for timing check
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

}

void loop() {

  currentTime = millis();

  if ((digitalRead(button1) == LOW) && (holding1 == false)) //button1 pressed
  {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite (13, LOW);
    holding1 = true;
  }
  if (holding1 == true && digitalRead(button1) == HIGH) //button1 released
  {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite (13, HIGH);
    holding1 = false;
  }

  if ((digitalRead(togglebutton1) == LOW) && (timing1 == false) ) //togglebutton1 pressed
  {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(13, LOW);
    timing1 = true;
    startTime1 = currentTime;
    // delay(10000);
  }

  if (timing1 == true && (currentTime - startTime1 >= runTime1))//togglebutton1 timed out
  {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(13, HIGH);
    timing1 = false;
  }

  if ((digitalRead(button2) == LOW) && (holding2 == false)) //button2 pressed
  {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
    digitalWrite(13, LOW);
    holding2 = true;
  }
  if ((digitalRead(button2) == HIGH) && (holding2 == true)) //button2 released
  {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    digitalWrite(13, HIGH);
    holding2 = false;
  }

  if ((digitalRead(togglebutton2) == LOW) && (timing2 == false)) //togglebutton2 pressed
  {
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
    timing2 = true;
    startTime2 = currentTime;
    digitalWrite(13, LOW);
    //delay(10000);
  }

  if (timing2 == true && (currentTime - startTime2 >= runTime2))//togglebutton2 timed out
  {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    digitalWrite(13, HIGH);
    timing2 = false;
  }
}