What I am trying to do:
-Use a PIR sensor to turn on and off the lights(which is controlled by RF 433.94 MHz).
-Also play music from a SD card when the lights turn on.
What I need help with:
I need a timer so that the lights will stay on for atleast 30 minutes after last motion detected.
I know I can use millis(). My system will close to never be switched off, so the return value of millis() will be extremely high, does this give me any problems? Should I use another method?
Do you guys have any recommendations for me?
I have been looking around for timers, but I haven't found anyone that fits my needs.
(This is my first arduino project, so please bear with me)
There are several ways to handle the millis.
Most people say subtract the start time from the current milis, but I prefer a more readable approach (for me), such as :
void loop(){
nowMilli=millis(); // used to simulate multi-tasking
if(nowMilli< lastMilli ) { // for 6 week rollover
logSpeed();
toggleLed();
updateLcd();
}
lastMilli=nowMilli;
if(nextMilliLogSpeed<nowMilli) logSpeed();
if(nextMilliLedToggle<nowMilli) toggleLed();
if(nextMilliUpdateLcd<nowMilli) updateLcd();
loopCounter++;
}
void toggleLed() { //toggle led
digitalWrite(led, ! digitalRead(led));
nextMilliLedToggle = nowMilli+99;
if (digitalRead(led) == LOW){
nextMilliLedToggle = nowMilli+4999;
}
}
Then inside each function, it sets the nextMilli time to run.
Just my opinion.
Most people say subtract the start time from the current milis, but I prefer a more readable approach
I find this perfectly readable
void loop()
{
unsigned long currentTime = millis();
if (currentTime - startTime >= requiredPeriod)
{
//execute code at the end of the required period
}
}
No messing around with exceptions for the rollover. It just works, as long as the variables are declared as unsigned longs.
electrofun123:
I know I can use millis(). My system will close to never be switched off, so the return value of millis() will be extremely high, does this give me any problems? Should I use another method?
Do you guys have any recommendations for me?
You will never get into trouble if you declare your "lastMoveDetectMillis" as an "unsigned long" value, and if your timeout condition is creating the time difference between the current millis() and the previously stored value before comparing against a timeout value.
And if you never try to compare values which are older than the millis() counter needs to create an overflow (ca. 50 days).
Perhaps like that:
void setup() {
// init input and output pins here
}
boolean motionDetect()
{
// function should return motion detection of motion sensor
};
const byte lightPin=13;
boolean lightState;
#define TIMEOUT (30L * 60000L) // 30 minutes in milliseconds
unsigned long lastMotionMillis;
void loop() {
if (motionDetect())
{
lightState=HIGH;
lastMotionMillis=millis(); // remember time when last motion was detected
}
if (lightState && (millis()-lastMotionMillis>=TIMEOUT)) lightState=LOW;
digitalWrite(lightPin, lightState);
}
Unless you tell the compiler differently it uses integer maths when doing the multiplication. Explicitly making one or more of the elements of the multiplication an unsigned long (UL) makes the result of the multiplication an unsigned long. I also separated the constants and the operators to make it easier to read that that has nothing to do with the result.