For(....) loop seems doesn't work, I mean it doesn't take time

So what is the preferred way to end the program? Many people do recommend using the infinite loop, but clearly it can cause problems when it prevents code directly above it from running, and/or causes outright crashes...

:+1:

Not if the program is properly written.

The arduino is not like a program you run in an app on a PC etc. where the environment will continue doing something.
You CAN stop it - for example with deep sleep, used to reduce supply current. Or tie it up in a forever loop.
The real question is what do you WANT it to do when the section of program you are running has achieved its objective.

@johnerrington Well I personally didn't want it to do anything- but the OP wanted I assume for the program to just end without going through loop() again, so he could see the time it took for one single iteration, without his serial monitor being inundated with data.

While this worked fine on the UNO, it resulted in a crash on the ESP. The example where it prevented the code directly above it from being run was in Java, so ignore that one I guess.

So assume we are talking about the ESP, and with the same objective as the OP

dup

Then simply make a Boolean variable that governs if the code inside the loop is to be run. At the end of the first run set this to false. Then the loop function will run and complete without doing anything.

Or put the code you want to run once inside the setup function and have a blank loop function.

Hi

I don't remember I read and learned that when using an empty while loop in ESP8266/32, the processing gets stuck and prevents other "activities" such as the wifi circuit and the watch dog from working.

This causes wtch dog reset in processing.

The recommended solution was to use the "yield()" function inside the empty loop.

I have used this recommendation and it has worked.

RV mineirin

1 Like

No need to go changing the optimization flags. Just declare testval and sonuc as volatile, which tells the compiler not to optimize away the code with those variable. Volatile is used most often for a variable that can be altered by an interrupt handler, because the value can change at any time during program execution.

I see someone has already mentioned the yield() function for the ESP8266, that is needed whenever you are coding blocking code, to allow the background processes to update properly. If I recall correctly, the ESP8266 has a watchdog timer intended to catch instances of code that does not return to the background processing often enough.

1 Like

Yes David. I ve tried just now.
its better then changing the optimization flags. I draw back it. then do your suggestion. So, faster then the my way. Thank you much.
I was looking for the tips like this. thats it. I love you all arduino community. cheers.

/*
   created 25 kas 2021
  by ****************
     This example code is in the public domain.
     speed test  if(    vs   min(
*/
#include <Arduino.h>
//#include <math.h>

unsigned long pv_time = 0;
//int16_t  testval = 0;
volatile int16_t  testval = 0;

void setup() {
  // put your setup code here, to run once:
  // initialize serials:
  // debug ve proglam port.
  Serial.begin(115200);
  Serial.println("Start ... ");
}

void loop() {
  // put your main code here, to run repeatedly:
  // * * * * * *
  pv_time  = millis(); // proses time olcelim.
  Serial.println(pv_time);

  for (int y = 0 ; y < 10 ; y ++) {
    for (uint16_t i = 0 ; i < 65535 ; i ++)  {
      // making band  using min and max func.
      //        testval = min(i, 123);
      //        testval = max(i, 30);

      // or using constrain func.
      //testval = constrain(i, 30, 123);

      // or using if(....      and else if(....
      // BTW best performance is this.
      if (i < 30 ) testval = 30;
      else if (i > 123 ) testval = 123;
      
    }
  }
  Serial.print("Process Time is = "); Serial.print(millis() - pv_time); Serial.println(" ms");
  while (1);  // stop here
}

conclution:
The "empty for loop" is possible without changing the optimization flags to -O0.
You just need to use the volatile variable inside of the for loop. ( David_2018 said that. Thank You David ).

There is a proverb in Turkish language. " Mind is superior to mind ". Bye bye...