Why does millis() does not start at zero?

You mean like this? It still does not start at zero :confused:

Good luck.

okay, but it does not always start at the same number. Like sometimes it starts at 3, simetimes at 6 and so on

what are 3 and 6 ? the reults of

function?

Do you understand that 3 and 6 - are milliseconds?????

The millis itself is considered correct, and this difference is the time it takes to print the value of the variable on the screen.

I print them out as seconds by dividing them by 1000. should have sait that before sorry.

so deltaTime is actually starting at 3 seconds (or sometimes even 4,5,6 seconds)

No, like this:

unsigned long oldTime;

unsigned long CalculateDeltaTime() {
  unsigned long TimeNow = millis();
  unsigned long deltaTime = TimeNow - oldTime;

  oldTime = TimeNow;

  return deltaTime;
}

See it here in action.


The loop reports delta time from the random delay that takes between 100 and 1000 milliseconds for the loop to finish. This is the full example in the wokwi I linked:

void setup() {
  Serial.begin(115200);
  Serial.println("delta T world!");
}

unsigned long oldTime;

unsigned long CalculateDeltaTime() {
  unsigned long TimeNow = millis();
  unsigned long deltaTime = TimeNow - oldTime;

  oldTime = TimeNow;

  return deltaTime;
}

int counter;

void loop() {

  unsigned long deltaTime = CalculateDeltaTime();
  
  Serial.print(counter); counter++;
  Serial.print("        ");
  Serial.println(deltaTime);

  int delayTime = random(100, 1000);

  Serial.print("                         going to sleep for ");
  Serial.println(delayTime);
  delay(delayTime);
}

Mine works, yours doesn't. Go line by line and spot the one thing I added to your function, and why that makes it work.

HTH

a7

What type arduino are you using? Boards with a native USB port, such as the Leonardo, take a significant amount of time to establish the USB connection, so you will not see anything sent to Serial before that is complete.

If I copy your code, it still does not start at zero

I am using arduino micro. But would it really take 3-6 seconds?

I don't think the micro does a reset when the serial monitor is opened, so you are likely seeing the time delay between when the upload finishes and the IDE re-establishes the connection to the serial monitor.

What do you see if you press the reset button, is it still 3 seconds?

I didn't tell you to copy my code. I suggested you run it in the simulator at the link I supplied. I asked you to study it to see where and how it differs.



So... you don't get this output?
delta T world!
0        0
                         going to sleep for 707
1        707
                         going to sleep for 449
2        450
                         going to sleep for 573

Get over the fact that by the time you enter loop(), millis() is no longer zero.

And what millis() is is not material. To time something, you want the difference between when it started and now.

a7

yes still :confused:

@miumiu
I think your problem has nothing to do with millis()
You definitely doing something wrong.

Below the results of working the code from post #21.
As you can see, the values start from zero:

0
999
1999
3000
3999
5000
6000
7001
8000
9000

the code (I have been added a setup() )

unsigned long oldTime = millis();

unsigned long CalculateDeltaTime() {
  unsigned long TimeNow = millis();
  unsigned long deltaTime = TimeNow - oldTime;
  return deltaTime;
}
void setup() {
  Serial.begin(9600);
}

void loop() {
unsigned long deltaTime = CalculateDeltaTime();
  Serial.println(deltaTime);
  delay(1000);
}

hmmm thats weird... even if I write the code exactly like post21, it still starts at 3000

As @alto777 said, get over it. Use the delta un mills(), not its absolute value for your timing.

Try it like this:

unsigned long oldTime;

unsigned long CalculateDeltaTime() {
  unsigned long TimeNow = millis();
  unsigned long deltaTime = TimeNow - oldTime;
  return deltaTime;
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
  oldTime = millis();
}

void loop() {
  unsigned long deltaTime = CalculateDeltaTime();
  Serial.println(deltaTime);
  delay(1000);
}

it worked!! thnak you so much!

But what exactly does while(!serial) do?

That waits for the USB connection to be established, the micro uses an atmega32u4 which has a native USB port, unlike an UNO where the USB is an external chip and doesn't get disconnected when the processor resets.

alright thanks again :slight_smile:

Something to be aware of, if you have while(!Serial); in the code, then the micro will NEED a USB connection to get past that point. If you intend to run the board disconnected from a computer, then leave that out (you can put a delay() after Serial.begin() to keep from missing any serial output instead).