My while loop does not work

I am using arduino nano and arduino IDE 1.8.9. I know delaySecs(30) works

It is a very basic question: Why this code doesnt work:

boolean flag = 1;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (flag) {
    delaySecs(60);
    flag = 0;
  }
}

void delaySecs(int secs){
  unsigned long temp = millis();

  while( (unsigned long) (millis() - temp) < secs*1000){
    Serial.println(millis());
  }
}

My serial monitor is forever loop while, Until the End of Time

secs is an int, which can hold a number from -32768 to +32767. So what happens when you call delaySecs(60)?

Regards,
Ray L.

this

while( (unsigned long) (millis() - temp) < 60000)

Oooh, thanks

boolean flag = 1;
void setup() 
{
  Serial.begin(9600);
  Serial.println("ok");
}
void loop()
{
  if (flag) 
  {
    delaySecs(10);
    flag = 0;
    Serial.println("done");
  }
}
void delaySecs(int secs)
{
  unsigned long temp = millis();
  while((millis() - temp) < (secs * 1000UL))
  {
    //Serial.println(millis());
  }
}

I changed delaySecs(60) to delaySecs(10)
[ == 10 secs]
because I didn't want to wait. . . . . . . . . . . . . . for 60 seconds.

thanks it is working now.

I guess your solution is like

void delaySecs(unsigned long secs)

Why would you even do this?

Despite using millis() the net result is not much different than simply using delay().

Is there nothing else the CPU could be doing than waiting 60-seconds staring only at an hourglass? If you want to expand the program later to add functionality, to add more than one thing going on at a time, how will you integrate that when so much of the CPUs time is taken up with a DoNothing() function?

mmm, the answer is... Using long delay() my code CPU is freezing. I am using delay() like timeout because my MCU is communicating with GRPS module using AT commands. I need wait the answer before send next command. The problems is when the signal GPRS is not good, the answer take a long time.

In general, you should avoid the use of delay() for any reason. You need to wrap your head around the concept of pseudo-concurrency, or sometimes called cooperative multithreading. There is an excellent article at Overview | Multi-tasking the Arduino - Part 1 | Adafruit Learning System that explains this.

My advice has always been, if you write a delay() statement in anything other than a toy example, you have made a fundamental design error. If you think you have an exception to this rule, you are wrong.

Because I work with low-powered devices, I care a lot about sleep, so I use the DeepSleepScheduler library (GitHub - PRosenb/DeepSleepScheduler: DeepSleepScheduler is a lightweight, cooperative task scheduler library with configurable sleep and task supervision.), which puts the chip into sleep mode if there is nothing to do, and wakes it up after a time interval. This usually gives me between one and three orders of magnitude improvements in battery lifetime. Once you understand multithreading, you will never find a use for delay(), ever.
joe

Then the worst thing you can do is sticking your head in the sand for 60 seconds. You will overflow your input buffer and might "freeze up."

Process every character as it arrives. If you get the sequence of characters you were expecting then great! If you don't get the expected sequence and it has been more than X hundred milliseconds since you asked for the data, try again. If it has been more than Y thousand milliseconds then stop trying and raise an error.

@flounder Okay I will read about multithreading.

On the other hand, I am using actually a interrupt and deep sleep mode, working okay with atmega328p and rtc ds3231. But I can say with my experience testing battery life, the best option is using a ultra low power timer IC.

I am testing delay() but I am not using delay() in my main code, so @MorganS my code process each character

for example

SWATCommand("AT","OK",10000)

int SWATCommand(String command, String keywork, unsigned long timeout) {

  unsigned long timer = millis();
  uint8_t auxFlag = 0;
  String Smsp = "";
  
  serialSIM800.println(command);
   
  while((millis() - timer) < timeout) {
       
    if(serialSIM800.available()>0){      
       Smsp = serialSIM800.readString();
    }
    
    if(strstr(Smsp.c_str(), keywork.c_str())){
      auxFlag = 1;
      break;
    }
    
  }
  
  return auxFlag;
}

I am not a expert programming, but using this code my project was working 18 hrs sending data from inside a small sewer. Maybe was only a current problem when the water pressure sensor is varying the resistence. I don't know, in the testing without sensor and traveling with the project for a long period using the battery at least was working 10 days without problem.