Auto-generate version numbers

Is there any way for me to generate automatically a version number in a sketch? Like something to count lines or number of updates?

I know how to count number of resets via using EEPROM but no of resets doesn't imply no of flashes.

Does anyone have any good ideas?

const char version[] = "build "  __DATE__ " " __TIME__;

To take that a step further, i juggle that content a bit and end up with..
yymmdd_hhmm

or omit the hhmm if not needed.

Version numbers in a sketch are all very well but if you don't change the filename so that you can revert back to a previous version then what is the point ?

You can look at the output from a running sketch on a customer site - and know where it sits in the evolution timeline.

There was an intriguing idea of adding a build hook to embed the Git hash in the code proposed on the issue tracker:

I think that would be the best approach by far for an automatic system.

If you care enough about versioning to want an automatically generated version number, you should certainly be using a formal version control system like Git.

UKHeliBob:
Version numbers in a sketch are all very well but if you don't change the filename so that you can revert back to a previous version then what is the point ?

Ugh. That brings back bad memories from before I started using Git.

lastchancename:
You can look at the output from a running sketch on a customer site - and know where it sits in the evolution timeline.

In the case of giving something to customers, it should be sufficient to just manually update a version string in the sketch, since you're only going to be sending release versions to the customers. Releases come infrequently enough that it's probably not worth the effort to set up an automated system. The automated system is really only necessary for development or beta testing.

I use this shell script:

zzz
#!/bin/tcsh

# bmp version of program (or set)

set ftyp = $1

set todayS=`date +%y/%m/%d`
set today=`date +%y%m%d`

#see if it is one of my known types
switch ($ftyp)
case mg:
	set fstem =	'dhMegaLib'
	set fbase =	"~/Documents/Arduino/libraries/${fstem}"
	set fsfx =	'.cpp'
	set faltsfx =	'.h'
	breaksw

case dt:
	set fstem =	'dht'
	set fbase =	"~/Documents/Arduino/${fstem}"
	set fsfx =	'.ino'
	breaksw

#sprinkler project
case ds:
case dsp:
	set fstem =	'dhsp'
	set fbase =	"~/Documents/Arduino/${fstem}"
	set fsfx =	'.ino'
	#set ardDir = 	'/'${fstem}
	set dirScheme =	'ard'
	breaksw

default:
	echo 'No match for program ' $1
	exit

endsw


#make sure necessary variables are set

if(! $?dirScheme) then
	set dirScheme = ''
endif


#set oldFile = 	`cd ${fbase}; ls  */*/*/${fstem}*${fsfx} | tail -1`
set oldLink = 	${fbase}/${fstem}${fsfx}

if (-e ${oldLink}) then
	set oldFile = `readlink ${oldLink}`
	#echo oldFile is ${oldFile}
else
	# no symlink, so find newist version
	echo finding old
	set oldFile = `(cd ${fbase};ls -l */*/*/${fstem}*${fsfx}) */*/*/*/${fstem}*${fsfx})|tail -1|sed -e "s/^.*\ //"`
	#echo oldFile is ${oldFile}
endif

set oldVer =	`echo ${oldFile} | sed -e "s/^.*${fstem}\.\([0-9]\{6\}.\).*/\1/"`
#echo "oldVer" ${oldVer}
set oldDate =	`echo ${oldVer} | sed -e "s/\(^......\).*/\1/"`
#echo "oldDate " ${oldDate}
#echo "today" ${today}
if (${oldDate} == ${today}) then
	#then it's an increment the same day
	set newVer = `echo ${oldVer} | tr "[a-zA-Z]" "[b-zA-Za"] `
	#echo "bump letter newVer" ${newVer}
else
	#the first for today
	set newVer=${today}"a"
	#echo "newwday  newVer" ${newVer}
endif

switch ($dirScheme)
case ard:
	#arduino needs a directory wit the same name
	set dirSup='/'${fstem}'.'${newVer}
	breaksw
default:
	set dirSup = ''
	breaksw
endsw

echo $dirScheme
set newFile = ${fbase}/${todayS}${dirSup}/${fstem}.${newVer}${fsfx}
echo newFile is ${newFile}
echo oldFile is ${oldFile}

#make sure today's directory exists

if (! -d ${fbase}/${todayS}${dirSup}) then
	mkdir -p  ${fbase}/${todayS}${dirSup}
endif

#make the new name

cp -p ${oldFile} ${newFile}

#change the link
set link = ${fbase}/${fstem}${fsfx}

ln -sf ${newFile} $link

# for any extra files
if ($?faltsfx) then
	#should really be a loop for multiple possibilities
	set oldAlt =	`echo ${oldFile} | sed -e "s/${fsfx}/${faltsfx}/"`
	set newAlt =	`echo ${newFile} | sed -e "s/${fsfx}/${faltsfx}/"`
	set link = ${fbase}/${fstem}${faltsfx}
	
	echo newAlt ${newAlt}
	echo oldalt ${oldAlt}

	cp -p ${oldAlt} ${newAlt}
	ln -sf ${newAlt} $link

	echo link ${link}
endif


exit

it has switches so that I can use it on different programs. For Arduino programs, by default it copies the most recent version to a new folder of appropriate name, and uses a YYMMDDa format, were the last letter is incremented for each version in a day.

I suppose it would be easy to use sed to change the first line to define a string with the version number in it.