Hi
I'm writing my own generic library so I don't have to write the same code all the time.
What would be very useful is if you could pass in a reference for a function. The idea
is you have the following
void Timer::delay(//FunctionReference)
{
currentTime = millis();
if(currentTime >= (loopTime + 1000)){
digitalWrite(ledPin, !digitalRead(ledPin)); // toggles the LED on/off
loopTime = currentTime; // Updates loopTime
}
}
However instead of having to keep changing the "digitalWrite(ledPin, !digitalRead(ledPin));" you could get it to call
a function which is defined in the actual sketch burnt to the chip.
As an aside, this is not a good way to do timer arithmetic since it does not handle overflow conditions well. It's best to ensure that you hold time values as unsigned long variables, and then use subtraction rather than addition. Logically the results are the same if you ignore the limitations of the data types, but in practice this approach works correctly when timer values overflow.
Also, just as a detail, in order to maintain an accurate frequency and avoid slippage, it's best to increment the time by the interval rather than assume that the next period starts 'now'.
Also, just as a detail, in order to maintain an accurate frequency and avoid slippage, it's best to increment the time by the interval rather than assume that the next period starts 'now'.
This conflicts with the whole subtraction-using-time-is-better concept. What should be stored is last time, not next time. Then, there is never a problem if overflow occurs.