Is my implementation of millis for 100 ms delay correct?

I am using this code to get velocity using change in height. For height measurement, I am using a bmp390 sensor and the height is calculated taking ground as reference.

Delay Code:

alticmp= bmp.readAltitude(SEALEVELPRESSURE_HPA);
delay(100);
bmp.performReading();
altitude= bmp.readAltitude(SEALEVELPRESSURE_HPA);
velocity= 10*(alticmp-altitude);

millis Code:

alticmp= bmp.readAltitude(SEALEVELPRESSURE_HPA);
if(currentMillis-milli100>99)
{
milli100=currentMillis; 
bmp.performReading();
altitude= bmp.readAltitude(SEALEVELPRESSURE_HPA);
velocity= 10*(alticmp-altitude);
}

Is my implementation of millis for 100 ms delay correct?

Print millis() each time the TIMER expires, do you get a 100ms difference.

Try

alticmp= bmp.readAltitude(SEALEVELPRESSURE_HPA);
if(currentMillis-milli100 >= 100)
{
milli100 += 100; 
bmp.performReading();
altitude= bmp.readAltitude(SEALEVELPRESSURE_HPA);
velocity= 10*(alticmp-altitude);
}

Yes I do, but the interval is 104 ms which I guess is due to the time taken for executing the code

I don't notice any difference in delay generation using your method over mine. Will your method make my code more efficient in terms of processing load?

Perry’s suggestion is better at timing slippage, however, it can result in abnormal results under certain instances; won’t get into those at the moment.

How are you checking? It's only a few minutes since I posted that, so you would not notice with your naked eyes! You might notice with your eyes pointed at the display of an oscilloscope.

Processing load is negligible in either case. Doing it that way ensures the interval increases by 100 (or whatever) every time, even if the exact 100 point is missed.

if(currentMillis-milli100>99)

What happens when you try:

if(currentMillis-milli100>95)

I checked by printing millis() in serial monitor when the if() is executed and outside the if loop and checked the time difference between the two.

1 Like

Its still 104 ms, but when I mate it 90, then delay is of 96 ms

Just so we don’t overlook something:

Always show us a good schematic of your proposed circuit. Show us a good image of your ‘actual’ wiring. Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘posting menu’ to attach the copied sketch.

Currently I don't have access to arduino, am checking the code on tinkercad. Will test it on arduino tomorrow. Will let you know as soon as its done

There's a while loop in the Adafruit library that's active while parsing fifo frames. My guess is you're using this library and this is contributing to the 4ms timing discrepancy.

I guess you'll need to look through your full code (I don't see it here).

My original code comprises of a lot of sensors and 8-9 functions, that's why I only sent a part of it. And the test code which generated 104 ms delay is this one

unsigned long a=0;

void setup() {
  Serial.begin(9600);

}

void loop() {

  Serial.println("Hi");
  if(millis()-a>99)
  {
    a=millis();
    Serial.println("ok");
    Serial.println(millis());
  }
  Serial.println(millis());
 
}

This code produces exactly 100ms when the TIMER expires.

unsigned long a = 0;

void setup()
{
  Serial.begin(9600);

}

void loop()
{
  if (millis() - a >= 100)
  {
    a = millis();

    Serial.println(millis());
  }
  
}

1 Like

So basically I need to adjust the >= value according to the execution time of contents in the if loop?

The basic code you gave us in post #14 works fine.

Use real hardware to confirm this.

If there is a constant error when you add new code, you can try to see if your adjustments to the TIMER will compensated.

1 Like

Try:

void loop()
{
  static unsigned long milli100 = 0;
  static float altitude = 0;
  static float alticmp = 0;
  static float velocity = 0;

  unsigned long currentMillis = millis();

  if(currentMillis - milli100 >= 100)
  {
    milli100 += 100; 
    bmp.performReading();
    altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA);
    velocity = 10 * (alticmp - altitude);
    alticmp = altitude;
}

Hmm, at 9600 baud, this would take maybe 5ms?

I tested the code in post 14 on actual arduino and it generates exact 100ms delay, thank you everyone for your help.

1 Like