Need help with a timer that starts when an LED turns on


I need to find a way so that whenever I turn a LED on, I record the duration of that LED without stopping the program (no delay()). The LED will last a few seconds until it turns off automatically but the program still runs infinitely long (until shut off). So that I can turn on the LED again, record that time, and shut off after a few seconds. Repeat.

The problem I had with using millis() is that it records the program time when the hardware is first turned on.

Are there any libraries that can assist me with this?

Thank you.

Can you please upload the code that you are referring to?

The problem I had with using millis() is that it records the program time when the hardware is first turned on.

millis() doesn't record anything: it returns how long since power on or reset. Normally that gets read into a variable like currentMillis, or theLedWasTurnedOnAt and is used for the basis of timing calculations.

It's not clear to me what you actually want to do. You use the word "time", but do you mean real time (time of day, like 06:05:00 or 15:27:32)? You also say you need to record the duration of the led, yet you say it switches off automatically, which sounds to me like the duration is already set.

I turn a LED on

What turns it on? Do you press a button or what?

it turns off automatically

What turns it off?

More info required I think.

I'm trying to make a box for the sport of fencing. So in short, when the tip of the blade depresses, it emits a voltage change that I can code to make an LED matrix light up for a few seconds on their first hit then the light will go away after a few seconds until the tip of the blade depresses again, and repeat. I can't use delay() because there's a chance the other person can get a hit in quickly enough, so I don't want to pause the program.

So when I first turn on the box, the currentMillis() tell me the amount of milliseconds when the box first turns on. When I depress the tip of my blade, I get the FIRST exact moment of a currentMillis() time of when the tip of the blade first depresses (LED matrix lights up for a few seconds too).

Essentially, what I'm coding is the lockout timer for fencing, if you are familiar with the sport.

Have a look at Blink Without Delay. That’s the timing method you need to use.