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;
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.
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
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.