Using Millis For Delay

im new to arduino. so far i have learned several things. but im getting stuck to delay() function. i have 4 different codes written in arduino mega 2560 which are quite time related. im using a simple led senor to turn on the light when led detects value under specified limit. i added 2 second delay in order to prevent sudden fluctuations to give a smooth output to the light. but when i add 2000mS delay the whole code stuck. which mess up with the other outputs . i have tried with millis but i dont know how to use it quite well . can anyone tell me how to use millis() properly. here is my code

int luxval;
const int ledpin = 2;
const int readpin = A0;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 2000;

void setup() {
Serial.begin(9600);
pinMode(readpin, INPUT);
pinMode(ledpin, OUTPUT);
}

void loop() {
luxval = analogRead(readpin);
Serial.print(luxval);
Serial.println(" lumen");

if (luxval <=140){
digitalWrite(ledpin,HIGH);
delay(2000); //a small delay to prevent sudden transaction to low
}
else {
digitalWrite(ledpin,LOW);
}

}

Why not just use the blink without delay example to take readings every two seconds?

sorry . i dont want to blink led. fir example if the light value is go down from the prescribed value , at that moment i dont want instantly turn the led off . i want to stay there for 2sec . in order to prevent fluctuations in case light increase again.

BlinkWithoutDelay

sorry but this thing is practically too difficult

Ok.

Imagine you replace the word "blink" with "do something".

Does that help?

kassiano742:
sorry but this thing is practically too difficult

What thing? the tutorial? Judging by the quickness of your response you have not studied the example long enough. Look at it until it makes sense.

TheMemberFormerlyKnownAsAWOL:
Ok.

Imagine you replace the word "blink" with "do something".

Does that help?

sir you are thinking that im talking about blinking an led. but the scenaio is quite different. when the LDR value would go under specified value the output led at pin will go offf. this circuit is working fine .but when the pin 2 (output for led on pin 2 ) would go to low , at that same time i want to stop that value for 2 second, reason being if in under 2 second ldr catches light from somewhere then output on pin2 will stay high in order to prevent fast oscilation between on and off cause outside light can increase and decrease a bit. thats why i put 2 second delay after digitalWrite(ledpin, HIGH), but i dont want to use delay cause it effect my whole code to stuck while runnin. i want to do that same ting with millis

sir you are thinking that im talking about blinking an led.

No, I don't think that at all.
That's why I wrote what I did about replacing the word "blink" with "do something".

Take a few minutes to think it through.

kassiano742:
sir you are thinking that im talking about blinking an led. but the scenaio is quite different.

Rather than think about what is different, you need to think about what is the same. Which is the use of millis() to manage timing. The BWOD code just blinks an LED for the purposes of illustration as all Arduino boards have an LED.

The demo Several Things at a Time is an extended example of BWoD and illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R