Using if statement with conditional timing

The first step line of code in the loop() function should be to set the value of currentMillis.

    currentMillis = millis();

That's what I see through the tiny window from under the umbrella.

a7

 currentMillis = millis();

So does this reset the count at the beginning of the loop? I get a bit confused.

@LarryD code works once I corrected with the line above. I was able to make some changes to my liking once I got this going.

I appreciate the help and explanations through the code.

No. It basically reads the clock, which is the ticking millis counter value, and places into the variable currentMillis. The call to millis() returns the number of milliseconds since the start of the program.

Then for the rest of the loop, currentMillis is used to see if time has elapsed since some other variable was set to the then current time.

At some point

   lastHighVoltageTime = currentMillis;

we start the timing by noting what time it is.

Many hundreds or thousands of times a second, we look at

    currentMillis - lastHighVoltageTime

which difference is the number of milliseconds which have elapsed since the timing was started, because with each iteration of the loop we move currentMillis along.

    currentMillis = millis();

When that difference is, say, greater than 2000, we can deduce it is time (at last!) that we do something.

You may have just omitted the crucial update to currentMillis when you used the example @UKHeliBob wrote. On the other hand, you may have thought that ever saying

unsigned long currentMillis = millis();

would establish a permanent and magic means for having currentMillis stay, um, current. This is not how it works - that is a declaration with an initial value. Which would not change unless a new assignment to the variable is made.

It's exactly like putting the pizza in the oven at 4:45 and watching the clock until 4:57, if you want twelve minutes to have elapsed. At 4:49 only 4 minutes have gone by.

HTH

a7

Thank you for the detailed explanation. I am going to mark this as solved.

I appreciate the explanations and help with the code.