Push button HIGH for 2 seconds

I have added a Wemos D1 mini to the remote of my window blinds to make it an IOT device. It works with hard coded code and new I want to do the same via Blynk.

The problem is that when I push the button (D1), then the D1 output has to stay high for 2 seconds for it to work.

I have added this code, but without any success

BLYNK_WRITE(V5)
{
  int pinData = param.asInt(); 

  if (pinData == D1) 
  {
    digitalWrite(D1, HIGH);
    delay(2000);
    digitalWrite(D1, LOW);
  }
}

Nope, nope nope…

Search, Read and Study on how to use non-blocking timers…

https://docs.blynk.cc/#blynk-firmware-blynktimer

@Den_Olli please edit your post using the pencil icon at the bottom and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

there are a number of examples of timers being used to do similar things. take a look at these it should not take too much digging.
https://community.blynk.cc/t/smartled-controller-esp-fastled-blynk-video/7333

@Den_Olli I think I’d use a Lambda timer.

timer.setTimeout(2000L, []()
  {
    Digital write low 
  });
1 Like

What’s a Lambda timer? I see it posted occasionally.
What’s the advantage over simple timer / Blynk Timer?

Thanks

Beats me :rofl: I’m just a noob. Maybe that’s not the correct name… I use it now and then and it works… maybe @PeteKnight can explain it from a C++ stand point but in layman’s terms I think it just uses the Millis() timer and does the calculations for you.

1 Like

Here’s how it would be used here.

BLYNK_WRITE(V5)
{
  int pinData = param.asInt(); 

  if (pinData == D1) 
  {
    digitalWrite(D1, HIGH);
    timer.setTimeout(2000L, []()
       {
        digitalWrite(D1, LOW); 
       });
  }
}

So after 2 seconds it would write LOW.

The lambda timer was one of @Gunner’s pet functions.
Here is one of his code examples:

As it happens, there is another thread about the subject running at the moment:

where @khoih provides a link to what lambda functions are all about, and a few tips on issues when passing variables into a lambda function.

Pete.

1 Like

It is actually just a more compact way of writing code… at least that is all I use it for, particularly with my timer codes. I am sure there must be many more reasons for its use/benefits with actual real programers (AKA not me :stuck_out_tongue: )

Basically this…

void setup() {
// setup stuff
// more setup stuff

// Timed Lambda Function - UpTime counter
timer.setInterval(1000L, []() {  // Run every second
    Blynk.virtualWrite(V0, millis() / 60000);  // Display the UpTime in Minutes
});  // END Timer Function
}

…is just a ‘compact’ way of doing this…

void setup() {
// setup stuff
// more setup stuff

// Timed Lambda Function - UpTime counter
timer.setInterval(1000L, UpTime);  // Run the function UpTime very second
}



void UpTime() {
Blynk.virtualWrite(V0, millis() / 60000);  // Display the UpTime in Minutes
}

1 Like

This is one of the solutions I had already found, but it doesn’t work in my case :frowning:
when I push the button for a half second, then the led is also only on for a half second.
D1 is GPIO pin 5, so I guess that V5 is correct?

There is no connection or correlation between physical digital pins and virtual pins - they are totally separate entities.

In your code, the BLYNK_WRITE(V5) callback function is triggered automatically whenever the value of a widget connected to V5 changes.
If your button widget is connected to D5 rather than V5 then this function will never be executed.

Pete.

@Den_Olli Ok This is happening because in button mode your button sends a 1 when you push and zero when you release. Change your button setting to switch in the app then in the timer function write a 0 to change the button back to off.

1 Like

I found the sollution
pinData isn’t the pin number, but is just the pin value (1 or 0)

It is easy. Just use own delay.

BLYNK_WRITE(V5)
{
  bool pinData = param.asInt(); 

  if (pinData) 
  {
      digitalWrite(D1, HIGH);
      delayMod(2000);
      digitalWrite(D1, LOW);
  }
}
void delayMod(unsigned long delayTime) {
	unsigned long startTime = millis();
	while (millis() - startTime < delayTime)
	{
		continue;
	}
	yield();
}

That while loop will block the rest of the code??

Absolutely!

Pete.

1 Like

A lambda timer would work better

2 Likes

no it works fine.