I am very new to programming and am trying to operate a switch that is turned on for a second or two and then off again after a proximity type sensor is triggered, I want the program to ignore the proximity sensor for a set time after it senses movement so as not to keep restarting the process. Any help will be appreciated
static unsigned long StartOfTimer = 0;
const unsigned long IntervalInMilliseconds = 5000; // 5 seconds
// When the interval starts:
StartOfTimer = millis();
// When you want to be sure the interval has expired:
if (millis() - StartOfTimer > IntervalInMilliseconds) {
// interval has passed
}
Thanks John, I will try to implement this
I suspect it probably won't apply to your project, but the 32-bit long millis() function wraps around every 49.7 days or so. If you used micros() which gives you time in microseconds instead, that would wrap around in a little over 1.15 hours. However, if you eventually need to write a long running application, you would need to handle the wrap around.
MichaelMeissner:
I suspect it probably won't apply to your project, but the 32-bit long millis() function wraps around every 49.7 days or so. If you used micros() which gives you time in microseconds instead, that would wrap around in a little over 1.15 hours. However, if you eventually need to write a long running application, you would need to handle the wrap around.
The code, as written, works across the wrap-around.
If switched to micros() it would still work across the wrap-around.
If you doubt it, do the math yourself (like I did years ago).
It will be hard enough for me not knowing any code at all to try and get something simple working But I will try... I dont even know what wrap around means...
I dont even know what wrap around means...
Sure you do. You have a watch or clock, right? What comes after 12:59:59? (or 23:59:59, if its a 24 hour watch/clock)?
That's wraparound. The Arduino is perfectly capable of dealing with wraparound, if subtraction is used, and the interval between events is less than the time it takes for wraparound to occur (49+ days for millis()).
If your intervals are 49+ days, using the Arduino as a clock is the wrong approach, anyway.