[SOLVED] Can't get LED to blink (rather than just "on") on Particle Photon when a specific relay is energized

I’m using a Particle Photon connected to a Particle Relay Shield (V3.1), Blynk 2.0 on Android. I’ll ask you to go easy on me since I haven’t programmed in about 4 years and I don’t have a lot of experience!

All I need help with right now is a nudge in the right direction on how to make my Virtual LED blink rather than stay lit when a specific relay is energized. Everything else is working well (even though I’m sure there are many ways to improve on my coding) but I can’t get the LED to blink! I’ve tried moving code around, I’ve looked at a bunch of tutorials (most of which are based on the old Blynk) and modified my coding but I can’t make the stupid LED blink! My next task will be to make a couple of the relays time-out so I feel I need to get the BlynkTimer figured out before proceeding.

Not that any of the following necessarily matters but, essentially, I’m creating an outlet for my 3D printer that I can deactivate remotely if necessary. As part of that, there will be a separately controlled outlet for a small heater that’s used to heat the printer enclosure prior to turning on the printer. Finally, there will be two (2) outlets that will deactivate after a specified amount of time so that whatever is plugged into them will not stay energized beyond the specified time (e.g., for charging stuff that shouldn’t stay plugged in after charging is complete.) If the heater is on (i.e., the relay to the heater outlet is energized) I’ve made it so that no other outlet can be turned on so as to keep the total current draw below 10 amps. Conversely, if any other outlet (relay) is energized, the heater can’t be turned on. If the heater is off, any other outlet can be used separately or simultaneously. If the heater is on, I want an LED on my Blynk app to blink continuously to remind me that I can’t use any other outlet until the heater is turned off. I have it programmed so that the LED lights up but I’d rather have it blink. Picky, picky, right?

Thanks in advance.

/*CURRENT CODE INCLUDES:
    1) IF HEATER (OUTLET/RELAY #2) IS ON, ALL OTHER RELAYS ARE LOCKED OUT
    2) CONVERSELY, IF ANY OTHER RELAY IS ON, HEATER IS LOCKED OUT
    3) IF HEATER IS OFF, ALL OTHER RELAYS CAN BE USED WITHOUT RESTRICTION
    4) IF HEATER IS ON OR LOCKED OUT, LED WARNING LIGHTS-UP (TRYING TO MAKE BLINK)
    5) FUTURE CODING TO MAKE OUTLETS 3 & 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 RE-ADD blynk.h FROM LIBRARY IF COPYING CODE!!!!
#include <blynk.h>

#define RelayPin1 D3
#define RelayPin2 D4
#define RelayPin3 D5
#define RelayPin4 D6

#define VPIN_BUTTON_1 V1
#define VPIN_BUTTON_2 V2
#define VPIN_BUTTON_3 V3
#define VPIN_BUTTON_4 V4

bool toggleState_1 = HIGH;
bool toggleState_2 = HIGH;
bool toggleState_3 = HIGH;
bool toggleState_4 = HIGH;

WidgetLED led1(V7);//LED to warn relays locked out due to heater outlet activity

BlynkTimer timer;

void blinkLedWidget()
{
  if (toggleState_2 == LOW) {
    led1.on();
  }
}

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) //Timed charge outlet
{
  if (toggleState_2 == HIGH) {

    toggleState_3 = param.asInt();

    digitalWrite(RelayPin3, !toggleState_3);
  }
}

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

    toggleState_4 = param.asInt();

    digitalWrite(RelayPin4, !toggleState_4);
  }
}

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);

}

void loop()

{

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

}```

I’d declare a global variable to track the current state of the LED widget.
Then, when you call the blinkLedWidget function (I’d male that time more like 1000ms rather than 250), you write the inverse of the current LED widget state to the virtual pin.
You’ll also need to invert the state of the variable that tracks it’s state, so you keep track of it correctly.
Something like this…

int LED_Widget_State;

void blinkLedWidget()
{
  if (toggleState_2 == LOW) 
  {
    LED_Widget_State = !LED_Widget_State; // invert the state
    Blynk.virtualWrite(V7, LED_Widget_State;
  }
}

Pete.

Thank you so much! You solved in a few seconds what I was trying to do for about 5 hours! Everything is working perfectly. (Now to see if I can go mess it all up by trying to time two (2) of the relays!)

Thanks again.

A little bedtime reading…

Pete.

Excellent write-up. I’m only a bit through it but I can see it should help me figure out some stuff I’m struggling with. Thanks for the link.

1 Like