I have a program for a garden watering reminder system. When it is really dry it goes into alarm mode to try and get my attention. All the LED's flash and every 5 minutes the piezo will chirp. I don't want it to chirp within a few hours after sunrise, or more than 3 hours after sunset. I don't want to use the time library because I don't want to set the clock every time there is an interruption to power.
So I take my lead from the 'Blink without delay' project to determine time since sunrise/sunset.
BUT! millis() starts as soon as the board is powered up. Being an unsigned long, it wraps to zero again after about 50 days.
At that point, isn't it possible that testing whether a counter integer is greater than the 3 hours interval since sunrise will falsely return that it is a smaller because millis() has wrapped to zero in the meantime?
If you want your project to keep accurate track of time over many months, the best solution is an RTC, which will keep time during power outages too. I really like the "extremely accurate" DS3231 based board from Jeelabs: http://moderndevice.com/product/jeelabs-precision-rtc-plug/ A bonus is that you get an accurate 10-bit temperature sensor as well.
I have a program for a garden watering reminder system. When it is really dry it goes into alarm mode to try and get my attention. All the LED's flash and every 5 minutes the piezo will chirp. I don't want it to chirp within a few hours after sunrise, or more than 3 hours after sunset. I don't want to use the time library because I don't want to set the clock every time there is an interruption to power.
So I take my lead from the 'Blink without delay' project to determine time since sunrise/sunset.
BUT! millis() starts as soon as the board is powered up. Being an unsigned long, it wraps to zero again after about 50 days.
At that point, isn't it possible that testing whether a counter integer is greater than the 3 hours interval since sunrise will falsely return that it is a smaller because millis() has wrapped to zero in the meantime?
That depends on how you wrote that piece of code. if(millis() - startTime =>delayTime)will not be affected by millis() rolling over after 50 days.
Okay, so if Millis has wrapped and start time (the time since dawn) was last sampled just before the wrap at, say ( 4,294,967,294). With a desired delay time of 3 hours (10,800,000), then let millis have wrapped and reached the required delay time. So millis() is 10.800.000 and delay time 10,800,001.
if (10,800,00 - 4,294,967,294 => 10,800,000) = true?
Ignoring the fact that a zero is missing, the if statement will be true. Add 4,294,967,295 (the maximum 32 bit number) to both sides to see why. Remember that 4,294,967,295 can also be interpreted as -1.
jremington:
Ignoring the fact that a zero is missing, the if statement will be true. Add 4,294,967,295 (the maximum 32 bit number) to both sides to see why. Remember that 4,294,967,295 can also be interpreted as -1.
It's important that the time variables are declared as unsigned long.
An other solution is to use a light sensor (a simple LDR of 10 cents) and combine that with the millis().
You could detect the night and day, and even synchronize with the daylight.