-1

I want to send message NRF24L01 to NRF24L01. I want to see if I can send just one second. How can I stop code after 1 second?

1
  • study the example code that is included with the arduino IDE
    – jsotola
    Nov 9 '20 at 9:05
0

You can use different ways:

Use setup only

Instead of putting code in the loop function, put it in setup, which is only executed once (use a for loop to send around 1 second.

millis() excluding setup

It is better to use the millis function and compare it against the start of sending; when 1 second is reached, don't send anything or use an infinite while loop.

Use something like:

unsigned long time_start;

void setup()
{
    // Your normal code 

    time_start = millis();
}

void setup()
{
    if (millis() - time_start >= 1000) 
    {
        while(1) {};
    }

    // your normal code (which should not take many ms,
    // otherwise the above code is not reached.

millis() including saetup (Thanks to Edgar Bonnet):

 void loop()
 {
     if (millis() >= 1000)
     {
        while(1) {};
     }
 }

This will stop exactly after 1 s after startup (excluding the setup). It will start counting after millis start counting, which is before the setup function.

Actually, in none of the above the Arduino 'stops', it just is doing endlessly nothing (well internally endlessly checking against the true condition which never changes).

2
  • 1
    Or even if (millis >= 1000) exit();. Nov 9 '20 at 9:11
  • @EdgarBonet Thanks, I added it to the answer. Nov 9 '20 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.