Why does millis() does not start at zero?

Hello!

I wanted to use millis to define my delta t for a project, apparently millis does not start at zero and I am wondering why.

Even if I use the example code of arduino, millis starts at 3000

unsigned long myTime;

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.print("Time: ");
  myTime = millis();

  Serial.println(myTime); // prints time since program started
  delay(1000);          // wait a second so as not to send massive amounts of data
}

Is there a way to to get timer start at zero?

millis() does start at 0.

Every time you rest the MCU the millis() count starts at zero.

If you wait a few days millis() will start counting at 0 again.

yeah... a few...
after 49 days...

But why does it not start at zero once void loop starts?

The ESP32 rolls over once every 207 years.

That's not what the millis() function does. If you want that functionality just create a variable at the start of the loop which gets set to zero each time.

millis() start counting when the CPU has pronounced its a live not when it starts running your code.

millis() time comes from the cycle counter.

Words have been provided as cluses so one can do research.

Look at File|Exmaples|02.Digital|BlinkWithoutDelay.

No amount of arguing or what about is going to get make millis() start at 0 when you want it to.

Why do you need that millis() starts from predefined value? The absolute value of millis() doesn't matter at all, all arduino timers uses the difference between millis() values at start and finish

I made a function to calculate delta t, but since millis does not start at 0, delta does not start at 0 as well:

unsigned long oldTime = 0;

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

I am getting data from sensors and I want to assign the data to the time they werde recorded

You should change your code this way, for example:

It does, you probably miss the first few lines when connecting to the board.

what about

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

if the OP was to look to see how to use millis() when it does not start at 0.

and what is your problem? It is irrelevant to the fact, start millis() from zero or not, just assign data to the current value of millis()

Letting us know right off the bat you wanted time stamps would have saved. Use an RTC

yeah, but wouldn't I miss the data from t=0? like if a want to make a graph later on, then there would be no data for when t=0,1,2

Why not use an RTC?

If the first time stamp is at 3000 then call that 0.

I'd prefer if I wouldn't have to use an additional hardware component

Then use a relative count as time 0. In some 47 odd days the millis() count will start all over again anyways.

Say when I first read millis() the value is 12. If 12 is my starting point then I store that number and call it my relative 0. When the next count comes is, say 15. The abs(12-15) gives me 3 millis! Later on when the count is 3012 then I can do abs(12-3012) to get the thing.

please look to the post #11 - there is a solution to your problem