BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How to replacing (properly using) millis function

esp8266

#1

hi,
i have a problem. when i use blynk library on my program, millis function can’t work properly
is any solution of my problem?


#2

Deline… “can’t work properly:thinking:

millis() works just fine for everyone else… I use it in a few sketches with Ultrasonic sensors, etc.


#3

millis() uses timer0 on Arduinos (AVR chips). There are some more timers inside. All for you.
The other question is: what are you doing in your code, that ruins the timer config? There are few things, that can affect millis(), but I’m not trying to guess.

EDIT: I should have noted earlier, that the matter is ESP: The ESP AFAIK doesn’t use timers for millis(), instead it counts clock cycles. Anybody correct me if I’m wrong.


#4

millis() works the same with Arduino and ESP.
It represents the time in milliseconds since the device was booted or reset. As the value is stored as a Long variable type it will overflow and go back to zero after around 49 days.

Pete.


#5

Not really :slight_smile: Yes, what they return is the same, but the way how they are obtained might (and as I remember readings - is) different. The point is, when working with hardware at “low level” you might actually ruin it’s job.


#6

SimpleTimer/BlynkTimer is based off millis()… so if there is significant differences between ESP/AVR, we would have ran into problems long ago :wink:


#7

You run into problems very quickly, if you redefine timer0 registry settings on AVR :stuck_out_tongue: Can’t say how it is done on ESP however. I can only say - “differently” :smiley:


#8

Perhaps… but well beyond this OPs supposed issue :wink:

We don’t even know what type of device is being used here.


#9

As the author of this topic is not interested much, will might never know that one…


#10

From a software syntax perspective, the function is the same on both boards. How it works at low level is clearly different, as the two chipsets work in totally different ways (and the ESP32 works differently again).

As you say, it depends what the OP is doing in his code, but my guess is that he’s expecting millis() to initialise to zero each time it’s called, as opposed to it being a counter that runs all the time and big (seemingly random) numbers when it’s called.

Maybe the OP will enlighten us at some point.

Pete.


#11

ok, thank you for the informations
but in this case i use board wemos d1
so, is there any solution of it?


#12

Solution for what? You still have not shown us any info on what, if anything is wrong.


#13

Post the code that you’re using in which the millis() function isn’t working.

Pete.


#14
#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "";

char ssid[] = ""; 
char pass[] = ""; 
float BPM = 0.0;
int thick = 0;
float start;
float finish;
float pulseinterval;
float periode;

void setup() { 
  Blynk.begin(auth, ssid, pass);
  attachInterrupt(D3 ,sensor,RISING);
}

void loop() {  
  Blynk.run();
  android();

}

void sensor(){
  thick++;
  if(thick > 9)
  {
    thick = 0;
    finish = millis();
    pulseinterval = finish - start;
    start = millis();
    perioda = pulseinterval/10;
    BPM = (1/periode)*60*1000;
  }
}

void android(){
    Blynk.virtualWrite(V1,BPM);
}

this is the code


#15

So you’re grabbing the finish time, deducting it from the start time (which isn’t set first time around), then grabbing the start time.
Doesn’t mKe logical sense to me.

Your biggest problem though is that you’re calling a function in void loop that does a Virtual Write EVERY time the void loop executes, this will flood the server as you’ll be sending this data hundreds of time per second.

There’s nothing in your code that will prevent the millis() function working correctly, your problem is down to bad programming.

Pete.


#16

ok thanks


#17

Now properly formatted for forum viewing :wink:


#18

Then it wasn’t question about “how to replace”, but about how to properly use" (the part in brackets) :wink: Ok, hope @PeteKnight showed clearly the main (but not the only) issue. Keep working, feel free to ask :slight_smile: