RCWL-0516 & Timer

Advice/pointers please?

I want to utilise an RCWL-0516 motion sensor to turn off some LEDS.

If there hasn’t been movement detected (HIGH) for x amount of time (say 15minutes) then the LEDS turn off.

Basically, I believe I need to have a timer reset to 0 every time the RCWL-0516 reads HIGH. When the pin is LOW the timer starts until HIGH when it will reset.

In my application I can have the LEDs turn off if the timer is >15minutes (1000x60x15).

I have looked at the StateChangeDetection example, which I believe I can adapt, but I can’t figure how to incorporate a timer into the sketch.

Any help would be appreciated

That would be in the BlinkWithoutDelay example...

And what timer settings would I utilise? millis()?

Did you look at the sketch? To start a timer, save an unsigned long copy of millis(). To read the timer, subtract the saved value from millis().

@aarg - spent all weekend trying to break down the sketch, but I just cant get it.

Current sketch below. If the sensor is HIGH I can make the timer = 0, but if the sensor is LOW (“else” satement in LOOP) I cannot get the timer to equal “millis() minus the last time the pin was HIGH”.

I’m sure it is a simple fix, but I have tried so many itterations with no avail- can anyone help?

const int   sensorPin = 2;    // RCWL-0516 connected to pin2

int sensorState = 0;         // current state of the sensor

unsigned long timer = 0; //store the last time seonsorPin was HIGH

void setup() {
  pinMode(sensorPin, INPUT); //RCWL-0516 as an INPUT
  Serial.begin(9600);
}

void loop() {

  sensorState = digitalRead(sensorPin);   // read the sensor input pin:
  unsigned long currentMillis = millis();
  
  if (sensorState == HIGH) {
    timer = 0;
    Serial.print("Sensor Activated: ");
    Serial.println(timer);
  } else {
    unsigned long timeSinceHigh;
    timer = timeSinceHigh;
    Serial.print("Sensor Off: ");
    Serial.println(timer);
  }
}