I want to use millis() in my program, but I'm concerned that it's an unsigned long value. That seems rather inefficient to me and I really really don't need to measure any time more than 10 seconds max.
Is there a way to change that 50 day overflow to something a little more manageable?
For reference, I'm programming a sequencer for synthesizers.
Mstimer2 looks useful, but I think it might be a superfluous library to include because I'm going to need to be measuring time as well as changing things on intervals. Plus there will be instances at which the sequencer will be running and I may want to simultaneously blink an LED or two. . . so if I'm already going to be using the millis() command on everything else, I don't see why having an interrupt for one time related function would really be beneficial.
An "unsigned short" provides a delta of about 65 seconds. Use "unsigned short" where you've been using "unsigned long" with and you'll be in good shape.
unsigned short starttime = 0;
unsigned short currenttime;
short delaytime = 1000;
void setup() {
}
void loop() {
currenttime = millis();
if (currentime > (starttime + delaytime) {
do some stuff
starttime = currenttime;
}
}
Of course I'd add in a bit more programming in case starttime > (delaytime - 1000) so that there wouldn't be any hiccups from overflowing.
I don't understand why I don't have to make any adjustments to the millis() command. If the millis() is an unsigned long and it goes past 65 seconds and I assign it to an unsigned short, wouldn't I get an error?
I don't understand why I don't have to make any adjustments to the millis() command.
It has to do with the way two's complement arithmetic works.
If you'd like a more complete answer, I suggest searching and reading. Others have written far better descriptions than I could.
If the millis() is an unsigned long and it goes past 65 seconds and I assign it to an unsigned short, wouldn't I get an error?
No error.
Other combinations of compiler / operating system / compile switches will produce an "overflow exception". But exceptions of this kind don't exist in the Arduino world.