Set timer with slider for relay

Hello,

I am a bit fighting with my project. I want to control a relay and use the slider for time that the relay is HIGH.
I used the code which I read in the forum.
What I not understand: mytimer.setTimeout(1000L, ActionOFF); What means the L behind the milliseconds?
How can use a slider with the seconds in this function?
How can reduce BLYNK_WRITE that there is not so much data use. We use this aplication a few times a day.
Enclosed a schreenshot of the app.

Thanks a lot

Jack

• Lilygo SIM800L
• Smartphone Android 8.0
• Blynk server

{
  // Set console baud rate
  SerialMon.begin(115200);
  // Set to true to define Relay as Normally Open (NO)

  delay(10);

  // Keep power when running from battery
  Wire.begin(I2C_SDA, I2C_SCL);
  bool   isOk = setPowerBoostKeepOn(1);
  SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));

  // Set-up modem reset, enable, power pins
  pinMode(MODEM_PWKEY, OUTPUT);
  pinMode(MODEM_RST, OUTPUT);
  pinMode(MODEM_POWER_ON, OUTPUT);

  digitalWrite(MODEM_PWKEY, LOW);
  digitalWrite(MODEM_RST, HIGH);
  digitalWrite(MODEM_POWER_ON, HIGH);

  // Set GSM module baud rate and UART pins
  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  delay(3000);

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  SerialMon.println("Initializing modem...");
  modem.restart();

  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem: ");
  SerialMon.println(modemInfo);

  // Unlock your SIM card with a PIN
  //modem.simUnlock("1234");

   SerialMon.print("Waiting for network...");
  if (!modem.waitForNetwork(240000L)) {
    SerialMon.println(" fail");
    delay(10000);
    return;
  }
  SerialMon.println(" OK");

  if (modem.isNetworkConnected()) {
    SerialMon.println("Network connected");
  }

  SerialMon.print(F("Connecting to APN: "));
  SerialMon.print(apn);
  if (!modem.gprsConnect(apn, user, pass)) {
    SerialMon.println(" fail");
    delay(10000);
    return;
  }
  SerialMon.println(" OK");

  Blynk.begin(auth, modem, apn, user, pass);
}
BLYNK_WRITE(V4)  
{
  SLID = param.asInt();
  }
  
BLYNK_WRITE(V1)  // Virtual button on Vx to activate action
{
  int BTN = param.asInt();
     if (BTN == 1) {
      digitalWrite(relayGPIOs[1], HIGH);  // Set pin high
     mytimer.setTimeout(5000L, ActionOFF);  // Run ActionOFF function in 5 seconds
     }
}
void ActionOFF()
{
 digitalWrite(relayGPIOs[1], LOW);  // Set pin Low
}

 

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

Long Integer.

I don’t really understand the question, but the Blynk timer works in milliseconds, so you would need to multiply the slider value by 1000.

The BLYNK_WRITE(vPin) function only triggers when the value of the virtual pin changes.

Pete.

Thanks Peter for your answer.

Long integer? Means calculated to seconds?

I don’t really understand the question, but the Blynk timer works in milliseconds, so you would need to multiply the slider value by 1000.

I want the slider value*1000 use in this fuction:
mytimer.setTimeout(1000L, ActionOFF);

So replace the the 100"0L with the value from the slider.

Thanks

If you don’t know what a Long Integer is in C++ then you should google it.

The value from the slider multiplied by 1000

Pete.

Also, I recommend you to read this very useful article

If you want to reduce GSM data traffic caused by the Blynk handshake and ping processes then you should read this…

Pete.

1 Like

[/quote]

know what a Long Integer is in C++ then you should google it.

The value from the slider multiplied by 1000

[/quote]

Can I use this:

mytimer.setTimeout(slider*1000), ActionOFF);

And is the long integer needed then?

Why not declare slider as a Long variable type?

Pete.

How to do? Sorry just a beginner :sweat_smile:

Check the article I sent you,
" Using blynk timer or simple timer "

I’m guessing that you didn’t take my advice…

The forum is for helping people with Blynk related issues, not teaching C++ programming basics.

Pete.

Oke I think I find it:

mytimer.setTimeout((long int)tijd, ActionOFF);

It seems working allright now. Only thing when I abrupt the timer by ActionOff and the timer did not finished his time. Then the next time I pressed its not the correct time anymore. Wait I a minute then its normal. Must I clear the buffer of the timer?

I’m surprised that works. It’s certainly not the correct way to do it.

I’m guessing that you didn’t read the link in post #5

Pete.

Nog good enough :wink:

timer.deleteTimer(timer_ID);

or with enable and disable

I will try what happend withe the delete....

If you just want the relay to be on for x amount of time when a widget button is pressed then you are using the wrong type of timer.
I’d use a timeout timer in a lambda function.

Pete.