PIR Input programming

Ok, I have a PIR and some code which work together fine.
At the moment if motion is continuous the LED brightens from 0-255, then jumps back to 0 and climbs to 255 again (as expected) if motion is continuous.
I would like the output (LED) to remain at (255 = brightest) until no motion is detected for x number of subsequent digitalReads of the input.
How to implement this?

Thanks a bunch.

int LED = 11;
int sensor = 7;
int n = 0;
int light = 0;
void setup(){
  pinMode(LED, OUTPUT);
  pinMode(sensor, INPUT);
  digitalWrite(LED, LOW);
  Serial.begin(9600);
  delay(1000);
}
void loop(){
  int sens = digitalRead(sensor);
  if(sens == LOW)
  {
    for(n=0; n<255; n++)
    {
      analogWrite(LED, n);
      delay(10);
      Serial.println(n);
    }
    light = 1;
  }
  if (light == 1)
  {
    if (sens == HIGH)
    {
      for(n=255; n>-1; n--)
      {
        analogWrite(LED, n);
        delay(10);
        Serial.println(n);
      }
      light = 0;
    }
  }
}

I would like the output (LED) to remain at (255 = brightest) until no motion is detected for x number of subsequent digitalReads of the input.

Incomplete requirements are nearly impossible to implement.

You need to look at the state change detection example, to learn how to detect a change from no motion to motion, or from motion to no motion.

Do something when each transition occurs.

Step 1: write your code using "pseudocode" - i.e., plain english.

If movement and brightness is less than 255
  increase brightness
end if

if no movement and brightness is not 0
  set brightness to 0
end if

Then convert that to real code - should be simple enough...

majenko:
Step 1: write your code using "pseudocode" - i.e., plain english.

If movement and brightness is less than 255

increase brightness
end if

if no movement and brightness is not 0
  set brightness to 0
end if




Then convert that to real code - should be simple enough...

Really appreciate the pointers majenko. Thanks.