millls() problem

Hi
I need help to resolve one problem .Does the code for the blinkwithoutdelay (attachment) which I changed a little is valid and if it is not what is the solution to this problem .
Because of the system solution I need to set the value of the previousMillis in one if statement and to use this value in another if statement.
I try to test this code but it seem it does not work.
the
Regards
Riste

BlinkWithoutDelay3.ino (523 Bytes)

Led turn on 1 sec after A0 reading >30.. and stays on.
Isnt this what you want ?

The led must turn on after one second when A0 < 30.
Regards
Riste

The led must turn on after one second when A0 < 30.

So, when the value is less than 30, and the timer is not running, start the timer. That is, record when the need to turn the LED on occurs, but only if the need to turn the LED on has not already been recorded.

if(analogsensor < 30)
{
   if(recordedMillis == 0)
      recordedMillis = currentMillis;
}

Then, you need to have some code to determine if it is time to turn the LED on.

if(recordedMillis > 0 && currentMillis - recordedMillis >= 1000)
   // The LED needs to be turned on AND its time to do so

You need to define the circumstances that cause the LED to be turned off, and, when they occur, turn the LED off and set recordedMillis to 0.

Thank you.

Hi
How to handle overflow of the mills function with the code that wrote PaulS?
Regards

currentMillis - recordedMillis >= 1000

So long as you declare the variables as unsigned long, then the code will handle the rollover without a problem.