Led to confirm output

Hi !
Somebody can help me ?

I want blink Led widget on app when output is on. For exemple :

D4 ( wich’s driving relais ) have to blink led widget when is high.
I think i’ve just to program D4 to virtual pin but i don’t know how…
Same for current sensor.

Thanks

I’d use a timer to call a function. The timer is because you want the LED widget to blink, and the frequency will be dictated by the timer.
You’ll need a global variable to track the state of the LED widget, so you can flip it.
You’ll also need a global variable to check the state of the relay, and only do the flipping if the relay is on.

The pseudocode for the timed function would be something like this…

void flash_led()
{
  if (relay_is_on)
  {
    if(LED_state = 255)
    {
      blynk.virttualWrite (LED_Vpin, 0);
      LED_state = 0;
    }
    else
    {
      blynk.virttualWrite (LED_Vpin, 255);
      LED_state = 255; 
    {
  }
}

You’ll also want to turn the LED widget off and set LED_state to 0 when you turn your relay off, but this would be done in the function where you relay on/off via a virtual pin…

Pete.

It’s can work like this ?

void flash_led()
 {
   if (value) {
         blynk.digitalWrite(1, HIGH);
         blynk.virtualWrite(V1, 255);
    }
    else
    {
         blynk.digitalWrite(1, LOW);
         blynk.virtualWrite(V1, 0);
    }

}

With relais on D1 (GPIO5) and LED widget on V1

I don’t think so.

Pete.

the relay will flash too :joy:

I’m trying since 2 hours, can’t find solution…

First just trying to read output and print it on serial :

if (relay_is_on) → i’ve to call digitalRead ? (digitalRead(5)) for D5 pin

void pinstate
{
  if (digitalRead(5) == 1)
 {
Serial.println("D5 = HIGH");
}
else
{ 
Serial.println("D5 =LOW");
}
}

Can you explain me the way to go ?

Would you like to blink led widget or a physical led ?

Hi John

I want blink led widget when d5 is HIGH, but first i’m trying to catch pin state…

Check this example

As I said to you in one of your other topics, you should use virtual pins for your app widgets, not digital pins. It makes problems like this much simpler to solve, and is a much more reliable approach.
It also makes code much more portable between different types of boards.

If you insist on using digital pins then take the pseudo code I provided earlier and change the line that says:

to one which digitalReads the state of your relay pin and evaluates to true if your relay is activated.

Pete.

This is for physical button i’m using app button

So you said better way it’s to use VirtualPin on app and i’ve to define on sketch witch VP is attach to physical pin ? I can do this

What I said was…

Pete.

This is a useful example