Using millis() in Blynk

Awesome thanks for this confirmation because I did see this before. So basically I mustn’t use millis() at all.

//int BlinkDelay1 = 1000;//to blink
//int BlinkDelay2 = 1000; //to blink
int BlinkDelay1 = 100; //to flash
int BlinkDelay2 = 50; //to flash

WidgetLED blinkled(xx); //virtual blink led
#define LedSys 2 //pin2 onboard Led

//setup
 pinMode(LedSys, OUTPUT);
 timer.setInterval(BlinkDelay1, blinkLedWidget);


// V1 LED Widget 
void blinkLedWidget()
{
  blinkled.on(); // virtual blynk led
  digitalWrite(LedSys, HIGH); //physical led

  timer.setTimeout(BlinkDelay2, []() {  // 2nd Timed Lambda Function - Turn OFF LED
    blinkled.off();  // Turn Virtual LED OFF
    digitalWrite(LedSys, LOW);
   
  });  // END 2nd Timer Function
}
1 Like

You can use millis() with Blynk, but in many situations Blynk Timer is better.

Pete.

2 Likes

In What situations can you use millis() ?

millis() is part of the Arduino core functions… and is not required nor excluded when using Blynk, any more so than other Arduino commands or libraries.

Generally it is used anytime you want to display UpTime or other “time past since boot” calculations.

1 Like

I think you can see my point :stuck_out_tongue:

2 Likes

Thanks @Gunner and everyone else for the insight! Appreciate it.

Regards
Darshal

1 Like

I want to use millis() inside a function and I want to call the function in every second. So, the period should be less than 1sec or there is no problem if I use a 5seconds period? I’m going to use this condition inside the function

currentMillis = millis();
if(currentMillis - startMillis <= period)
{do something;
}

and yes, everything is declared in the global section with unsigned long and period is const unsigned long period = 5000;

And one thing, for example in the time period of 5 seconds if certain condition is met then I just want to start the counting again from the beginning. In this case, what should be change currentMillis or startMillis? Or It will automatically start from the beginning of if(currentMillis - startMillis <= period) this condition?

It really depends on what you’re trying to achieve with this bit of code.
If for example you were trying to monitor whether the function being called every second was taking an unusually long time to complete (more than one second for example), then use of millis in this situation would be logical.
Otherwise it’s probably best to use a second timed function.

The millis value returned by the MCU starts at zero when the MCU boots and continues to increment for as long as the MCU us running. If the MCU is still running after around 49 days then the value of millis will exceed the size of the variable that’s used to store it, and will overflow back to zero.

startMillis=millis() takes a snapshot of the value at a point in time and currentMillis is another snapshot at this point in time (technically you could just compare startMillis to millis() if you wished, but snapshotting the current millis value and storing it as currentMillis is neater).

So, it’s the startMillis value that you’d need to reset if you want to begin counting a new period.

Pete.

1 Like

very informative, thanks sir

I just want to read a sensor value every 1second delay in the function and if the value is in between a certain range (say 40 to 80) then it will upload on the blynk server and if not it will read again for 5 seconds period without delay and don’t update to the blynk server if the value is not in between the above range. And if the value is within the range in the checking period then it will upload on the cloud and stop the period immediately and normally send the sensor values on a 1second delay and if again that situation occurs then it will start a 5seconds period again.

So, this is the idea. Is that suitable to use millis() here? Or I should use simple if else and flag to do that? Can u please provide me a simple pseudo code if u have the time. It will means a lot. Thanks.

What resources do you think you’re saving by not taking any readings for 5 seconds, as opposed to simply taking a reading once every second?
To my mind, a reading every second is too frequent anyway for most situations, unless you’re trying to do some fairly fine control.

Pete.

1 Like

I’m struggling to work out the reason for the 1 second and 5 second routine? And what sensor are you reading so we can try to establish a good sample time for you.

2 Likes

Pulse sensor

It depends how your pulse sensor works, but normally the solution is to use an interrupt so that every time the sensor sends a HIGH or LOW signal (depending on which ones you’re interested in) a callback function is called automatically.
If you’re interested in frequency per minute then millis is a good way to do this (if milliscounter >= 60000 then reset milliscounter and process the results - although I suspect that with a heat rate monitor you’d actually want to do this say every 15 seconds).

Pete.

1 Like

some general query about blynk:

  1. Actually which programming language is used in all blynk sketch?
  2. Which compiler is used in arduino ide to compile the blynk sketch?

Language is C++ for libraries and sketches.

Server is java.

Compiler I don’t know off hand.

2 Likes

Compiler is AVR :wink:

2 Likes

AVR or AVR C?

Thank You so much. :slight_smile:

2 Likes