# How to get a function in void loop() to run for a certain amount of time

I have a program in which an LED blinks at 4hz. When a button is pressed, the LED lights up for 1 second, then calls for a the function “nightCode”. While the nightCode is running the LED turns off for 5 seconds. My issue is, once those 5 seconds are over, the LED goes back to blinking at 4hz, but the nightCode continues to run. How do I stop the nightCode from running after those 5 seconds are up and have the program reset and wait for the button to be pressed again?

``````void loop()
{
//flash the led at 4 Hz until button is pressed
digitalWrite(led,HIGH);
delay(62.5);
digitalWrite(led,LOW);
delay(62.5);
}

digitalWrite(led,HIGH); //led lights up for 1 second
delay(1000);
digitalWrite(led,LOW);

nightCode(); //starts nightCode 1 second after button has been pressed
delay(5000); //turns of led for 5 seconds
}

void nightCode()
{
//20LineCode
}
``````

You set a boolean to be true when you want to run nightcode and false when you don't. Then use an if statement to check if you need to run it.

delay() takes an unsigned long value.

1 ÷ 4 = 250ms, what is 62.5 all about ?

larryd: delay() takes an unsigned long value.

1 ÷ 4 = 250ms, what is 62.5 all about ?

That is how long the LED stays on and off. It blinks really fast about 4hz.

I think the point is that 4Hz means that the LED is on and off twice in that time (ie, 4 states in 1000 ms). This means that each time period between changes of state should be 250ms.

62.5ms means that there are 16 changes of state (8~~16~~Hz).

62.5ms means that there are 16 changes of state (16Hz)

16 changes of state in 1 second would be 8Hz, you need 2 changes of state for 1 cycle.

For 4hz, you have a period of 250ms.

As mentioned, that would mean 125ms for the LED to be ON then 125ms for the LED to be OFF ;).

you need 2 changes of state for 1 cycle.

Yep, my mistake. As I wrote that it didn't seem right. Thanks for the correction.

Thank you all for the help, believe me I'm decent at math (/s lol). Anyhow, I tried to use a boolean at instructed by @marco_c, but I struggled. BTW, I forget to mention I am quite new to Arduino. Instead, I chose to do this

``````void loop()
{
//flash the led at 4 Hz while waiting for a button closure
while (digitalRead(button) == HIGH){ //do until button closes
digitalWrite(led,HIGH); //flash led once
delay(250);
digitalWrite(led,LOW);
delay(250);
}

digitalWrite(led,HIGH); //get here on button closure
delay(1000);
digitalWrite(led,LOW);

nightCode();
}

}

void nightCode()
{
delay(2000); //turns of led for 5 seconds
long duration, distance, cm;

digitalWrite(TRIGGER_PIN, LOW);
``````

Now, it begins by the LED blinking. After I press the button, the LED lights for 1 second, turns off, then nightCode runs (WOOOO!!). The only issue is that nightCode goes on forever. How could I make it reset back to blinking LED after 5 seconds? Thank you

Let’s start over.

For people to help you they need to see the complete sketch.

I figured it out. At first I was going to use LED blinks to time 5 seconds but that was too complicated. Instead, I used an

``````if (millis () < 5000) {
}
``````

command.

Thank you all for your help!! Have wonderful rest of your day or night!

if (millis () < 5000)

You do know this is ‘true’ only during the first 5 seconds after power goes on, then again every 49th day thereafter.

@larryd

I don’t quite know what you exactly mean, but the “nightCode” works for 5 seconds then everything stops. Though, the downside is I have to plug the power out then in to “reset” it.

The millis() function returns a value from 0 to 4,294,967,295.

This value increments every millisecond.

4,294,967,295 milliseconds is about 49 days.

When 4,294,967,295 is reached the next value is 0 (zero) i.e. the value resets to zero and things start all over again.

When you power up the Arduino, millis() starts out at 0.

From the above, hopefully you can see millis() will be < 5000(milliseconds) for ‘only’ five seconds after power up then again every 49 days.