Pages: [1]   Go Down
Author Topic: Sensor controlled switch with delay timer?  (Read 591 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 203
Posts: 8719
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John, I will try to implement this  smiley
Logged

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1836
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 06, 2012, 08:03:38 am by MichaelMeissner » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 203
Posts: 8719
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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). smiley
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: