How to start loops when I press a key and stop when I press again?

I am creating a dashboard system for my car game. I just need when I press a key, the light turns on and turns off after 0.3 seconds and loop until I press the key again.


I would do this:
When the button is pressed you toggle a boolean variable between true and false. If the boolean is toggle on, you reset the “timer”.
For the timer, I would use Time.time along with the “timer” and a little bit of math to determine whether the light should be on or off every frame. This timer will only run if boolean variable is on.

Just as a little bit pseudocode if it helps:

toggle = true
timer = 0.0

    if button
        toggle = !toggle
        if toggle
            timer = Time.time
    if toggle
       light.enabled = (Time.time - timer)%0.6 < 0.3

Hope this helps,