Can't get virtual button to turn off after corresponding relay shuts off, Particle Photon with Relay Shield

As noted in the title, I’m trying to figure out how to turn off a virtual button after the corresponding relay turns off. Thanks to some help on timers from PeteKnight on this forum, I’m now able to press a virtual button and energize a relay and the relay then shuts off after the amount of time I insert into the program. The problem is I can’t get the virtual button to also turn off after the relay shuts off. I’ve tried numerous things, some resulting in the button going on and off, some making it turn off correctly but then coming back on and staying on.

The virtual button in question is V4 and the corresponding relay is D6.

Thanks in advance.

/*CURRENT CODE INCLUDES:

    1) FOUR RELAY-CONTROLLED OUTLETS: 
        3D PRINTER 
        HEATER 
        UNTIMED OUTLET 
        TIMED OUTLET
    2) IF HEATER (OUTLET/RELAY #2) IS ON, ALL OTHER RELAYS ARE LOCKED OUT
    3) CONVERSELY, IF ANY OTHER RELAY IS ON, HEATER IS LOCKED OUT
    4) IF HEATER IS OFF, ALL OTHER RELAYS CAN BE USED WITHOUT RESTRICTION
    5) IF HEATER IS ON OR LOCKED OUT, LED WARNING LIGHT BLINKS
    6) OUTLET 4 TIMED TO DEACTIVATE AFTER SET AMOUNT OF TIME */
 
    
//MUST MAKE SURE NEXT THREE(3) LINES ARE AT TOP OF CODE!!!!

#define BLYNK_TEMPLATE_ID           ""
#define BLYNK_DEVICE_NAME           "RELAY4TEST"
#define BLYNK_AUTH_TOKEN            ""


//MUST DELETE AND RELOAD FOLLOWING LIBRARY AND MOVE BELOW #define BLYNK STUFF ABOVE

// This #include statement was automatically added by the Particle IDE.

#include <blynk.h>


#define RelayPin1 D3 //3D Printer, pin D3 on relay shield
#define RelayPin2 D4 //Space Heater (750W Max!), pin D4
#define RelayPin3 D5 //Timed outlet, pin D5
#define RelayPin4 D6 //Timed outlet, pin D6


#define VPIN_BUTTON_1 V1 //Button to control 3D printer outlet
#define VPIN_BUTTON_2 V2 //Button to control 3D printer enclosure heater
#define VPIN_BUTTON_3 V3 //Button to activate unspecified relay-controlled outlet
#define VPIN_BUTTON_4 V4 //Button to activate timed outlet


bool toggleState_1 = HIGH; //Logic to track current state of relay
bool toggleState_2 = HIGH;
bool toggleState_3 = HIGH;
bool toggleState_4 = HIGH;


BlynkTimer timer;//For all timer needs, up to 16 timers


//FOR CONTROLLING LED THAT BLINKS WHEN HEATER RELAY IS ENERGIZED

WidgetLED led1(V7); //LED to warn relays locked out due to heater outlet activity
int LED_Widget_State; //Logic to determine if LED is on or off
void blinkLedWidget() //Timer for lock out LED

{
  if (toggleState_2 == LOW) { //Check to see if heater relay is energized
    LED_Widget_State = !LED_Widget_State; // invert the state
    Blynk.virtualWrite(V7, LED_Widget_State);
  }
}


//FOR CONTROLLING TIMED OUTLET (RELAY 4)

void relayTimer()
{
  if (toggleState_4 == LOW) {
    digitalWrite(D6, LOW);
    Blynk.virtualWrite(V4, LOW);
  }//TRYING TO GET V4 BUTTON TO TURN OFF - DOESN'T WORK!
}


char auth[] = BLYNK_AUTH_TOKEN;


BLYNK_WRITE(V1) //3D Printer outlet
{
  if (toggleState_2 == HIGH) {

    toggleState_1 = param.asInt(); //assigning incoming value from pin V1 to a variable

    digitalWrite(RelayPin1, !toggleState_1);
  }
}


BLYNK_WRITE(V3) //Untimed  outlet
{
  if (toggleState_2 == HIGH) {

    toggleState_3 = param.asInt();

    digitalWrite(RelayPin3, !toggleState_3);
  }
}


BLYNK_WRITE(V4) //Timed outlet
{
  if (toggleState_2 == HIGH) {

    toggleState_4 = param.asInt();

    Blynk.virtualWrite(V4, param.asInt());

    digitalWrite(RelayPin4, !toggleState_4);
  }

  if (toggleState_2 == LOW || toggleState_4 == HIGH) {
    digitalWrite(D6, LOW);
  }
}


BLYNK_WRITE(V2) //Heater outlet
{
  toggleState_2 = param.asInt();

  led1.off();

  if (toggleState_1 == HIGH && toggleState_3 == HIGH && toggleState_4 == HIGH) {

    digitalWrite(RelayPin2, !toggleState_2);
  }
}


void setup()
{

  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);

  delay(5000); //Allow board to settle

  Blynk.begin(auth);

  timer.setInterval(250L, blinkLedWidget); //Timer for locked LED warning light
  timer.setInterval(6000L, relayTimer); //Timer to shut off timed relay/outlet

}

void loop()

{

  Blynk.run();
  timer.run();


}```

It’s not really clear what exactly you are trying to achieve with this, could you explain the logic?

I realise from your previous post that this is something to do with 3D printing, but the application is irrelevant, it’s the operational logic you haven’t explained.

Your use of togglestate variables also doesn’t help, you’d be better giving thee meaningful names like heater_oulet so that the code becomes more self explanatory.

I think you’ve probably used the wrong type of timer, although it’s difficult to tell as I don’t understand the logic, but if you want one of the outlets to turn on for x amount of time then ho off, then a timeout timer is probably the best option.

Pete.

Thanks for your reply. After playing around with the program I’ve come to realize I actually prefer the way the program is currently working (the Blynk button stays “on” even though the associated relay has timed-out and is “off.”) The button remaining on will be a reminder that I have something plugged in that should be removed. The timer for the relay itself is working satisfactorily so I think I’ll leave it as-is.

And now that you’ve pointed it out, I can see where it would be much easier for someone else looking at the program if I had used meaningful names as you suggest. Much of my program is cut-and-paste and for my own use I didn’t find it necessary but certainly, if I’m asking someone to help me with a problem in coding, I should have the courtesy to do a better job in naming variables and in my documentation. Plus, I’m sure it would pay dividends if I ever have to refer back to what I’ve done. I appreciate your suggestion.

Thanks again for taking the time to reply!