Blynk timer+relay

Hi,
I want to trigger a relay for 10 seconds when i push a button in blynk.
I couldn’t find something like this on internet, then i tried to make one with

[code]
timer.setInterval(8000, RelayOn);

[/code] and [code]
BLYNK_WRITE(3)
{
if (param.asInt()) {

if((currentMillis - previousMillis >= OnTime))
{
led1.on();
previousMillis = currentMillis; // Remember the time
}
else led1.off();
}
}

[/code] but i didn’t got it to work.

Does someting like this exist?

Thanks!

Hi. What do you mean by “trigger for 10 seconds”?

Thanks for your reply,

when i push the button, relay is on for 10 sec and turns off after that.

Pseudo code, but you should get the idea

int startTime = 0;
bool startRelay = false;

BLYNK_WRITE(3) {
    startRelay = true;
    startTime = currentMilis;
}

void loop() {
Blynk.run();
relayAction();
}

void relayAction() {
    if (startRelay && currentMilis  < startTime + 10sec ) {
        //do any action with relay.
    } else {
        //do any action to stop relay.
       startRelay = false;
    }
}
1 Like

Using SimpleTimer library:

#include <SimpleTimer.h>
SimpleTimer timer;
#define RELAY_PIN 10;

void turnRelayOff() {
    digitalWrite(RELAY_PIN, LOW);
    Blynk.virtualWrite(V1, LOW);
    Serial.println("Relay disabled");
}

BLYNK_WRITE(V1) { 
  Serial.println("Relay enabled");
  digitalWrite(RELAY_PIN, HIGH);
  timer.setTimeout(1000, turnRelayOff);
}

void loop() {
  timer.run();
}
4 Likes

thank you!!!

will try this out, will solve several aspects!

can i put this setTimeout in any function? anywhere?

Yes, but the definition of function which you running using setTimeout must be before setTimeout

1 Like