Auto generate version at compile time

The UI for my software is an LCD display. I hardcode a version in the sketch and display it on the LCD so I can see what version of my software that I'm running. This procedure is only as good as my ability to remember to change the version every time I compile. I'm wondering if there is a way to auto-generate the version at compilation time? Is there a time/date function that can plug that info into a variable? That would be very cool. Thanks.

1 Like

Some systems of version control (RCS, CVS or git) has features of auto-generate version number and include it in the source code.

You could use the string literals __DATE__ and __TIME__ to get the date and time of compilation. Note: Two underscores before and after.

Adjacent string literals are concatenated so you can put them together: __DATE__ " " __TIME__ to get a single string literal containing both:
Jan 19 2023 09:56:50

1 Like

My only solution used "DATE" and "TIME" constants and a couple of EEPROM bytes.

Just get compile date and time with:
const char compile_date[] = __DATE__ " " __TIME__;
then based on that string I get an (almost) unique ID, like checksum. I then read the first byte from EEPROM: if the value is not equal, I assume a newer version has been compiled, so update the EEPROM id value and increase the value of the second EEPROM byte (if you assume you don't have more than 255 versions, otherwise use two bytes).
I can't at the moment find my old code, but it was something like this:

const char compile_date[] = __DATE__ " " __TIME__;
...
void setup()
{
  unsigned int ver = 0;
  byte chk = 0;
  for(int i=0; i<strlen(compile_date); ++i)
    chk += compiledate[i];
  if (EEPROM.read(0) != chk) {
     EEPROM.get(1, ver);
     EEPROM.put(1, ++ver);
  }
}

Perfect!!! This is exactly what I need. Thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.