How to exit for(;;) if it wont complete one cycle within specific time (25sec)

Hello sir, I am new to Arduino, I would like to know how to write a sketch when infinite loop for(;:wink: don't complete one cycle within a specific time say (25sec) then print error message ("m/c is not running at optimal time please do necessary")

break;

Look at the BlinkWithoutDelay example. Modify it to do what you want after a period of time

By the way, why use an infinite for loop in the first place when the loop() function does the same thing.

But for it to work the problem must be in the for-loop. Aka, the loop must still run for it to work. If a call in the for-loop is just blocking everything there is no way to exit the loop. Then you must fix the blocking function that's halting the program :slight_smile:

Let me ask in a simple way, please suggest a sketch (code) for the following condition

IF INPUT PIN A1 IS NOT HIGH WITHIN 25SEC THEN PRINT.LCD('' ERROR 0123")

thank you all for replying !!!!

unsigned long start = millis ();
for (;;)
  {
  // do whatever here
  if (millis () - start >= 25000)
    {
    Serial.println ("m/c is not running at optimal time please do necessary");
    break;
    }
  }

It's a forum for help, not ready made code. Did you look at Blink Without Delay? Then you can give it a try and come back (with YOUR code) if you have trouble.

Few extra questions

  • 25 seconds from when?
  • What do you want to do in the mean time? Really just put you head in the sand and wait?

And yes, the code of Nick also gives you some help :slight_smile:

:slight_smile: Thank you so much Nick Gammon

:confused: OK Septillion sir, kindly excuse me for my faults, I will keep in mind your valuable suggestion said above.

UKHeliBob:
Look at the BlinkWithoutDelay example. Modify it to do what you want after a period of time

By the way, why use an infinite for loop in the first place when the loop() function does the same thing.

before for(;:wink: I have other functions like m/c initialization steps. which is required to run once at the beginning and this for(;:wink: loop is used for repetitive work (automation)

But that's why there is a setup() and a loop()... Put all that initialization into setup() and tadaaa, you can let the loop() loop :slight_smile: