Toggle Button Timeout Using Millis()

cattledog:
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;
  }
}

This is very helpful, I am so thankful for you and for your time and effort helping me out. While this code works perfect for the toggle switch and momentary switch, I ran into another problem. The toggle switch in my case is NOT a physical momentay switch, but rather is a relay itself. Here is the situation: I am adding a LUTRON Wireless light switch where I can control from a dedicated app the system. I am planning to use a 120V coil relay connected to the lutron wireless light switch, and use the normally open and the normally closed sides of that relay as my toggle buttons for the UP and DOWN. when I turn the wireless switch on, it would trigger the attached relay, thus closing the toggle circuit, initiating the toggle the Roller UP function, when I turn the wireless off, the relay will set back to the other position, where it will also toggle the ROLLER DOWN function.

The problem that I am running into, is how to make the toggle part of the sketch expire and not continuously run when the toggle button or function is still pressed (a relay in the normal open position for roller up, or normal closed position for the roller down)?

In other words, I want the toggle button function to timeout in the specified interval even if the toggle button continues to be pushed....

too picky I am I guess .... My wife says that I am too OCD and crazy enough. I think she is right....