Arduino freezes after 4-5 minutes

Hello everyone,

I have a very simple program that counts the number of button presses (plus and negatives), does ratios and print a set number of leds accordingly.

The program works well and is stable. the issue begins when I start to sync it up with ntp server. It then starts to freeze after 4-5 minutes. I have tried a few different versions of ntp sync codes, checked free memory as well and everything seems to be fine but the issue persists.

counter.ino (13.7 KB)

You normally need to call Ethernet.begin(mac) only once in setup. In loop, you should be calling Ethernet.maintain() to renew a lease.

Do you have a SD card in the shield's slot?

Thanks for the reply SurferTim.

I will change the code.

I do not have an SD card in the slot. Do i need one?

ok so I kept a closer eye on the output and turns out that NTP is requested every 5 minutes. How can I turn that off? I would rather have that requested again at set times (when I am resetting the counter)

That seems to be what is causing the problem.

But this time around, it did not freeze. :-)

I don’t understand what you mean by “turn that off”. Put some Serial.println() calls in your code to determine where it freezes.

Actually your suggestion regarding switching the ethernet statements fixed the issue. not sure how, would you be able to explain the reason for my understanding?

I want to stop the program from requesting a new ntp packet every 5 minutes, maybe change it to once a day or once a week. Trying to figure out and seems like this line is what is triggering it

 uint32_t beginWait = millis();
  while (millis() - beginWait < 1500)

but not sure how the 1500 translates to 5 minutes.

Once again thanks for all your help, cannot believe that fixed it so quickly.

It doesn't. It translates to 1500ms, or 1.5 seconds. Without seeing the rest of the code, I can't tell how it translates to 5 minutes. I guess that 200 of those delays would equal 300 seconds, or 5 minutes.

SurferTim: It doesn't. It translates to 1500ms, or 1.5 seconds. Without seeing the rest of the code, I can't tell how it translates to 5 minutes. I guess that 200 of those delays would equal 300 seconds, or 5 minutes.

that is my entire code. I still cannot figure out where the 200 delays are coming from. Will keep looking has to be somewhere in here.

That isn’t your entire code. It wouldn’t compile on any version of the IDE that I have.

edit: If you mean counter.ino, that code is very hard to read due to the format. I’ll try to muddle through it, but things like this don’t help.
delay(20000);

edit2: This is just a delay from the NTP packet send until the code checks for a returned packet. It has nothing to do with how long the delay is between NTP sends.

 uint32_t beginWait = millis();
  while (millis() - beginWait < 1500)

The only reason I added delay(20000); was to prevent the device from restarting for the entire minute at 7 am and 7 pm. Wasnt really sure that if i specify down to a second that the machine would not accidentally miss it.

Is there any other format that I can send it to you to make it helpful? (kind of new to arduino hence newbie issue. Thanks for the support. )

Would you be able to shed some light on the ethernet issue? still dont understand why ethernet maintain vs using a new ethernet begin would cause such problems.