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();
}```