Reading and writing a variable using If conditionals.

I'm trying to use the If conditionals to write to a variable. In the same sketch, I'm trying to read the same variable. And use the Arduino as a timer. I'm using a PIR Sensor for input.
Example :
If motion is sensed, set a timer(variable) to 10 minutes. Count the timer variable down.
Check the timer variable, if it hits zero, set an output pin to HIGH.
How exactly would I do this?

How exactly would I do this?

  if(theConditionOfInterestIsTrue)
  {
     someVariable = someValue;
  }
void loop() {
    // If motion is sensed, 
    PIRstate = digitalRead(PIRPin);
    if (PIRstate == HIGH && PIRstate != PIRoldstate) {
        // set a timer(variable)  to 10 minutes. 
        DelayTimer = 10 * 60;  // Ten minutes in seconds
    }
    PIRoldstate = PIRstate


    // Count the timer variable down. 
    if (millis() - PreviousTime >= 1000) {
        DelayTimer--;  // Another second has passed
        PreviousTime += 1000;


        // Check the timer variable, if it hits zero, 
        if (DelayTimer == 0) {
            // set an output pin to HIGH. 
            digitalWrite(OutputPin, HIGH);
        }
    }
}

I'm sorry, but I don't understand your response. Can someone please try to explain it differently?

TBIsurvive:
I'm sorry, but I don't understand your response. Can someone please try to explain it differently?

I took your very rough outline and turned it into code. I quoted your rough outline as comments so you can see what part is done by each bit of code. What parts of it do you not understand?

Hi,
Welcome to the forum.

Can you tell us your electronics, programming, arduino, hardware experience?

Also what model arduino are you using?

Thanks.. Tom.. :slight_smile:

Differently..

When you sense the motion, you look at the clock and note down the time.
motionTime = milis();

Then you calculate what time it will be when you want to take action.
actionTime = motionTime + waitTime; // waitTime is in milliseconds.

A good idea would be to make a note somewhere that your waiting.
doingWait = true;

Now when you go through loop over and over check to see if its time to do something.
if (doingWait && millis()>=actionTime) {

Its time to set the pin high and probably note that you are no longer waiting.
digitalWrite(myPin, HIGH);
doingWait = false;

}

The curly brackets group together things your doing if the "if" case is true;

Hope this helps!

-jim lee

motionTime = milis();
actionTime = motionTime + waitTime; // waitTime is in milliseconds.
if (doingWait && millis()>=actionTime) {

Be warned that this will go wrong every 49 days or so. When millis() gets near the rollover point adding waitTime to that value will overflow the unsigned long and give you a small value. Millis() will immediately be larger than that small value and will trigger the action prematurely. This will repeat until the millis() rolls over to 0.

To avoid the rollover problem always use:
currentTime - startTime >= waitTime
(All values are unsigned long.) This subtraction method will work without a glitch for all cases where the waitTime is less than 49 days. This also saves you a variable since you don't need 'actionTime':

motionTime = milis();
...
if (doingWait && millis() - motionTime >= waitTime) {

johnwasser:

motionTime = milis();

actionTime = motionTime + waitTime; // waitTime is in milliseconds.
if (doingWait && millis()>=actionTime) {




Be warned that this will go wrong every 49 days or so. When millis() gets near..

Shush! Let 'em lick the sugar before they taste the pill.

-jim lee