Hi All.
To give a quick explanation first, I'm wanting to create a simple function for Timer On Functionality. This is to replicate what is used in all PLC type of programming where you Set the Time value and then set a Bit to say Start timer. When the Time is reached a Dun bit will turn True to state the Time has elapsed. BUT lol there will be plenty things happening in between the start and completion of the Time so cant use the simple Delay() function. I know there is far better accuracy using a RTC board but I want to make it available for use in Every day simple programs on a single Arduino board. I'm certified as Severely Sight Impaired so when I've exhausted myself looking through the Arduino documentation I fall back to asking simple questions on forums to help me find the answer. To also add that I have been a PLC design and build engineer for over 20 years so understand how to program in C as well as a few other languages. I have written this Timer code for the Arduino and tested it and it works fine but I have a problem when the Time rolls over hence my question below.
Question:
When using the micros() when does the Time Roll Over i.e. what is the Maximum Time count using the micros().
I know the documentation says approximately 50 days but I need to know what the exact max number is so I can compensate for roll over.
the following is a pretty convention approach to using a timer that allows the period to be programmed as well as disabled. the timer is also conditional on msecPeriod being non zero, perhaps the inverse of you Dun bit
the timer could either reset itself by setting msecLst or disabling itself by setting msecPeriod to zero.
This works because the variables are unsigned integers. Unsigned means there are no negative values
You can test this if you calculate
unsigned long my1stUL = 0;
unsigned long my2ndUL = my1stUL - 5; // result is a very large number simulating beeing near of a rollover
my1stUL = 1; // simulating beeing just after the rollover
unsigned long my3rdUL = my1stUL - my2ndUL; // still a small number because of unsigned integer-math
Roll Over is required for accuracy of your Time completion.
Imagine if you set the Timer On (TON) for 30 seconds and lets, for the sake of explanation here, the Max time for the Milli or Miros is 3000 = 30 seconds. Now if you set the TON when the Count is 1500 and you don't know when the Max count is and now every time you check the timer has counted up to 3000 you have to add the last taken time to the new count. What will happen is you will get to 3000 counts and only have calculated 1500 counts when it Rolls Over. You will need to know the Max Count to determine how much to add to the new count.
I understand this is going to be fairly inaccurate anyway Especially over longer periods of seconds, minutes or hours, but what happens if you are doing a TON for very small Times?
The Accuracy is going to be worse.
@tokoloshe
I'm pretty good in math but I do not understand what you want to explain.
Can you please show all details with easy to follow example-numbers for everything
= example numbers and all detail calculations explicitly posted
that fit to each other?
No. Exactly as @gfvalvo and others wrote, the subtraction of unsigned numbers ensures that it does not matter where is the beginning and where is the end, the difference will be correct if the measured time period is less than maximal possible value.
The best way is to try it by yourself.
you do know the Max count by the variable type, in this case unsigned long, and so does the computer which does the math correctly when subtracting 2 unsigned long values, A - B even when B > A