Arduino millis() not ticking?

Hello! I am porting code from Arduino. I’m running into a problem where it seems like millis() isn’t ticking.

When I do the following

    while (1){
      Serial.println(millis());
    }

It prints 0 and never increases. I’m uploading (successfully) to Arduino Uno and can do other things in the code, it is simply millis() that seems to not be working. Is there something specific that needs to be done to make it work with PlatformIO?

When I imported this project I did NOT choose to keep it compatible with Arduino, but I am including the Arduino.h header.

Thanks for any suggestions! :slight_smile:

Hi @jordanapplewhite!
Serial.println(millis()) works perfectly with my Uno board.
Can you post here your full sketch?

Sure, I just tested it with the following:

main.cpp:

#include <Arduino.h>

int main(void){
  Serial.begin(9600);
  while(1){
    Serial.println(millis());
  }
}

This was from initializing a new project and adding Arduino Uno as the board. Am I doing something wrong by doing while(1)? I assumed millis worked off an interrupt so putting it in the while loop wouldn’t be blocking. Thanks for your help :slight_smile:

Arduino sketch should have setup and loop functions, something like this:

#include <Arduino.h>

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

void loop()
{
  Serial.println(millis());
  delay(1000);
}
1 Like

Oh my goodness I feel like a goof. For some reason I thought the project needed to have a main() function if it wasn’t using the arduino compatibility option. Thank you for clearing this up for me!!! :slight_smile: