In other words, I want the toggle button function to timeout in the specified interval even if the toggle button continues to be pushed....
I think there is a simple modification to the code which will allow for an unconditional time out of the togglebuttons. By making the switch between timing = true and timing = false dependent upon the switch release, we can keep the code from reentry into the block which turns the relays on. Calling the variable timing is not quite accurate for its new function, but I can't quite think of a better name.
I thought there were some issues with switch bounce on togglebutton release with this new code, so I added some debounce delay and repeated readings.
See if this version does what you want.
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 = 5000;
unsigned long runTime2 = 5000;
//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;
}
if (timing1 == true && (currentTime - startTime1 >= runTime1))//togglebutton1 timed out
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(13, HIGH);
if (digitalRead(togglebutton1) == HIGH) //button was released
{
delay(50);//debounce delay
if (digitalRead(togglebutton1) == HIGH) //reading is solid
{
timing1 = false; //reenables the togglebutton1
}
}
}
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);
digitalWrite(13, LOW);
timing2 = true;
startTime2 = currentTime;
}
if (timing2 == true && (currentTime - startTime2 >= runTime2))//togglebutton2 timed out
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(13, HIGH);
if (digitalRead(togglebutton2) == HIGH) //button was released
{
delay(50);//debounce delay
if (digitalRead(togglebutton2) == HIGH) //reading is solid
{
timing2 = false; //reenables the togglebutton2
}
}
}
}