Problem getting variable from widget to timer.setInterval()

Hi, I’m trying to get a variable from a slider widget into the timer.setInterval() function.
The code is something like this:

BLYNK_WRITE(V11) {
  delayMinute = param.asInt();
}

void setup() {
timer2 = timer.setInterval((delayMinute * 60000), delayOff);
]

The problem is that delayMinute always returns 0, probably because it is 0 when void setup() is running. I have tried Blynk.syncAll() in setup, moving the BLYNK_WRITE after the setup, but nothing changes.

All my other widget is ok, but this one is the only one that require a variable from a widget in the void setup()

Does timer.setInterval() have to be in the void setup() ?

It can be anywhere you like.

Thanks for your answer.
But is it possible to get the widget variable from inside the void setup()?
It would be much easier for me.

I have tried with Blynk.run() in the setup. I would have thought that this would run all the BLYNK_WRITE functions, but no, my variable 'delayMinute’ is still 0…

Blynk.run should be in the loop() so it is running constantly. Otherwise you may have connection issues.

Is delayMinute declared in your code, if not that may be the problem? int delayMinute;

Try this for your slider function. Slider widget should be attached to Virtual Pin 11 (V11).
BLYNK_WRITE(V11) { int delayMinute = param.asInt(); Serial.println(delayMinute); //show value is serial monitor timer.deleteTimer(timer2); timer2 = timer.setInterval(delayMinute* 1000L, notifyUptime); }
Also, the 1000L would be 1 second, so technically the number input by the slider would be in seconds not minutes. Change the 1000L to 60000L if you want the input to be minutes.

Blynk.run() is in the loop, I also placed it in void setup() just to try, it will run just once, so it will not cause any flooding. I have deleted it now since it didn’t change anything. 'delayMinute' is declared so that is not the issue.

The BLYNK_WRITE(V11) you are suggesting I have tried, but with timer.disableTimer instead of timer.deleteTimer. It causes the code to stop, I’m not sure why.

I know I should have postet all my code, but it is so large I will have to make it smaller to highlight the issue. But my question still is; can you get a variable from a widget in void setup()? That would have solved the problem for me.

Yes, providing you request the “Blynk” variable after you have connected and it’s normally done with sync as you don’t have time to modify a widget in the app during setup(). However if you change a variable in the app whilst the hardware is asleep for example it will be available from the server (in setup()) when the hardware wakes.

If you haven’t already tried then use Blynk.syncVirtual(Vx) rather than syncAll()

Failing this, strip down the code and paste it.

I have Blynk.syncAll() in my void setup() after Blynk.begin(). Still, the variables from all of my BLYNK_WRITE() functions are zero. it seems like the B_W functions are executed after the setup?

But, the problem is solved. :grinning:
In my main function I had two timer.restartTimer(timer2);

When I deleted them and instead used
timer.deleteTimer(timer2);
timer2 = timer.setInterval((delayMinute * 60000), delayOff);
it’s now working perfectly.

The delay is 0 the first time the code runs, but that’s not a problem.
Thank you all for helping me.

Are you trying to have V11 revert to the last number saved on server? Maybe try this without using the Blynk.syncALL(); command in setup()

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V11);

 
}

Why do you think this is better?
Blynk.syncAll() will sync all virtual pins at once, also V11.

Like this?

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();

}

I’m sure that would be just fine, depending on what you want to happen. Blynk.syncAll(); would sync everything, including digital/analog pins.

I would put it in the BLYNK_CONNECTED() as this function gets called every time Blynk connects to the server. This is good in case your wifi goes down for a moment or something like that. If you put it in the setup, it will only run if the board gets restarted/reset.