Why is the millis function keeping track of time as intended in Project #8?

The Arduino Projects Book states that loop() runs continuously after setup() has completed.( I intepret this as finishing all the code writtin within the curly braces, and then starting again from the top).

However, in project #8, Digital Hourglass, the book uses the millis() function to keep track of the amount of time that has passed.

Code Extract

void loop() {
    unsigned long currentTime = millis();
    //more code....

In the code shouldnt above, shouldn’t the currentTime be placed outside the loop() function as a global variable instead of being declared inside the loop() function? I say this because in the example aboce, wouldn’t the variable currentTime be reset everytime the loop() has finished one iteration(i.e it reaches the last line and starts over again.)

However, the code works perfectly with currentTime() inside the loop() function, doesn’t work if i declare currentTime as a global variable outside loop() and setup(), and will not work if declared in setup() as i will get an error saying that currentTime was not declared in this scope and i can’t seem to figure out why…?

Any clarifications would be greatly appreciated.

EDIT:(Full Code)

const int switchPin = 8;
unsigned long previousTime = 0;
int switchState = 0;
int prevSwitchState = 0;
int led = 2;
long interval = 60000;

void setup(){
  for(int x = 2;x<8;x++)

void loop(){
  unsigned long currentTime = millis();
  if(currentTime - previousTime >interval){
    previousTime = currentTime;
  if(led == 7){
  //What happens when all the leds light up
  if(switchState != prevSwitchState){
    for(int x = 2;x<8;x++){
    previousTime = currentTime;
  prevSwitchState = switchState;

What you say is right. But as I don't have access to the code I can't say why it is working or indeed what it is doing. If you posted all the code maybe I could come up with a better answer for you.

Hi Mike,

I've edited my initial post to include all the code for the project =)

OK yes that variable is updated every time but that is what is supposed to happen because it compaires the current time with the previous time and it is that which is a global variable and only gets updated every time the interval time has elapsed.

Ask again if you don't understand that.

Hi Mike, Thanks for taking the time to reply.

I've output the values of currentTime via Serial.println, and am seeing the values increasing, as it should.HOwever, what i'm wondering is why currentTime does not get set to 0 whenever the loop finishes one iteration?

Currently all i'm seeing is the value for currentTime increasing according to the time since it first started.

what i'm wondering is why currentTime does not get set to 0 whenever the loop finishes one iteration?

Because it is a total elapsed time counter that keeps track of the number of milliseconds since power was applied. When you use it you should always use it in a relative way. That is by subtracting the time now from the time then to get an idea how long it has been since then. It might seem odd to you at this stage but it is how most computer systems keep track of the time. It is much more flexible to do it this way and you can track the time over many many times round the loop. And not just for one time but for many different times.