Comprehension 101

Hello,
I am having a real heard time wrapping my head around this bit of code:

const int ledPin =  13; 
int ledState = LOW;  
unsigned long previousMillis = 0;   
const long interval = 1000;          

void setup()
{   
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  unsigned long Time = millis();
  if (Time - previousMillis >= interval)
  {
    previousMillis = Time;
    if (ledState == LOW)
    {
      ledState = HIGH;
    }
    else
    {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
}

Specifically previousmillis and interval. I see we defined previousmillis as an unknown variable. However where in the sketch does code define what previousmillis is? Almost seems like the word should be in blue. Same thing with interval. I see we defined it as 1000. Where in the sketch is interval defined to be relative to millis? I dont see the relationship. Also seems like it could be in blue. Thanks in advance!

What do ypu mean by unknown variable? It's declated in the beginning of the sketch.

if (Time - previousMillis >= interval)
  {
    previousMillis = Time;

Can't be much clearer.

unsigned long previousMillis = 0;

Also rather clear.

To be fair, the OP mentioned "previousmillis" which is not defined anywhere in the code.

previousMillis = Time; I guess I just do not understand how you can do the if statement before we know previousMillis = Time;. KeithRb. I see. Just ya. maybe ill fully understand how later. Thanks for pointing out that bit though!

You don't know. It goes like this the first time through:

previousMillis() is set to 0.
interval is set to 1000.

Eventually we start loop.

Time is set to the value of millis().

We get to the if statement.

First subtract previousMillis from Time. Since the first time through previousMillis is 0, we just compare to see if Time is greater than 1000.

If time is greater than 1000, we set previousMillis to the current Time and we are ready to wait for when millis() is 1000 ms greater than previousMillis.

jgreene33:
previousMillis = Time; I guess I just do not understand how you can do the if statement before we know previousMillis = Time;

Time (from millis()) is basically "now", and previousMillis is "then".

So when we look at Time - previousMillis >= interval, we're looking to see if now-then is bigger than the interval. If it's not, ie not waited long enough, do nothing. If it is, do stuff with the leds, and then we put "now" into "then", effectively resetting our personal timer. Next time through loop(), millis() gives us a new Time "now" to compare to the new "then", which is the now we just finished with.

Ok, that made sense when I wrote it....

Think about like boiling an egg.
You put the water on and start it boiling: that's setup()
You drop the egg in note the time: previousTime = 0;
You walk away, come back and check the time (at the start of loop(): currentTime = millis();
Did enough time pass for the egg to be done? currentTime - previousTime >= duration
Yes? instantaneously take the egg out, drop the next one in and note the new starting time: previousTime = currentTime
No? Keep on waiting and checking.
Can do other stuff while waiting, hence Blink Without Delay, watching the time pass to see if an event is done.

This can be combined with a 'flag' to indicate if a timer is running, such as for two events of uneven duration:

void loop(){
currentTime = millis(); // capture the current 'time'
if (timer1Running == 1){ // is timer1Running?
elapsedTime1 = currentTime - previous1Time; // see how much time passed
if (elapsedTime1 >= duration1){ // enough to stop the process?
timer1Running = 0; // turn off flag
timer2Running = 1; // turn on flag for alternate event
previous2Time = currentTime; // 'start time' for next event
}
}
if (timer2Running == 1){
elapsedTime2 = currentTime - previous2Time;
if (elapsedTime2 >= duration2){
timer2Running = 0;
timer1Running = 1;
previous1Time = currentTime;
}
}

}

Make sense?