delay function

in my program, when analogRead(9)<511, i would like to check, is the analogRead(9) is smaller than 511 continuously in few seconds, say 2500msec.
does anyone can tell me how can i do that?

void setup()
{
pinMode(22, OUTPUT);
Serial.begin(9600);
analogReference(DEFAULT);
}

void loop()
{
Serial.println(analogRead(9));
if (analogRead(9)<511)
{
delay(2500); //delay 2500ms
digitalWrite(22, LOW);
}
else if (analogRead(9)>=511)
digitalWrite(22, HIGH);
}

The value read from the analog pin may move up and down hundreds of times during your nap.

If you want to know if the analog pin's value is less than some value for a period of time, you must record when the value was tested/fell below the threshold, and continuously read the value. If the idea is to do something if the value has remained below the threshold for some period of time, you would do that something if the difference between "now" and "then" (when the value dropped below/was first noticed to be below the threshold) is greater than the desired interval.

Look at the blink without delay example.

There was no reason to make this a poll!

thanks for your help, but what i want to do is, keep checking whether analogRead(9)<511 for a period of time,
after that we set digitalWrite(22, LOW)
is it different from the situation in blink without delay example?

After delaying for 2500ms, you don't bother to check the current value of analogRead(9).... you just set pin 22 low no matter what. (hint you need an if-statement in there)

using millis you would:

  1. Read the analog value.
  2. store the current value of millis()
  3. wait until the current value of millis() is longer than your "couple of seconds"
  4. Read the analog value again.

if the analog value is still less than 511, set your pin low.

I think what he's asking is to read the value numerous times for 2500mS, and if the value is always low to then make the output pin low. If it goes high on one of those reads, then the output pin will not go low.
Then you need some more code to indicate when the 2500 started, if it failed to complete, and then the output pin changing if it passed.

Which is different than reading it once, waiting, then reading again and deciding.

CrossRoads:
I think what he's asking is to read the value numerous times for 2500mS, and if the value is always low to then make the output pin low. If it goes high on one of those reads, then the output pin will not go low.
Then you need some more code to indicate when the 2500 started, if it failed to complete, and then the output pin changing if it passed.

Which is different than reading it once, waiting, then reading again and deciding.

this exactly what i am asking, but i don't know how to do that
does anyone can help me?

does anyone can help me?

Something like this will measure the value continuously for the whole 2500 milliseconds.

int threshold = 511; // Or whatever value is important

void loop()
{
   bool wentOver = false;
   unsigned long startTime = millis();

   while(millis() - startTime < 2500)
   {
      int val = analogRead(0);
      if(val > threshold)
      {
         wentOver = true;
         break;
      }
   }

   // If wentOver is true, the threshold was exceeded at some point
   // Otherwise, the value was under the threshold the whole time
   if(!wentOver)
   {
      digitalWrite(outPin, LOW);
   }
}

This could be adapted to not block for the whole 2.5 seconds.