Go Down

Topic: Sensor controlled switch with delay timer? (Read 685 times) previous topic - next topic

Aviator1

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

johnwasser

Code: [Select]

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
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Aviator1

Thanks John, I will try to implement this  :)

MichaelMeissner

#3
Dec 06, 2012, 01:55 pm Last Edit: Dec 06, 2012, 02:03 pm by MichaelMeissner Reason: 1
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.

johnwasser


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). :)
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Aviator1

It will be hard enough for me not knowing any code at all to try and get something simple working  :smiley-red: But I will try... I dont even know what wrap around means...

PaulS

Quote
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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy