20 Minute Freeze / Crash

Hi, I have the attached code running on a breadboard mounted atmega328. (It is handling the running of a stepper motor)

The incoming serial coms are delivered from a spark photon. I set it up this way because driving a stepper and running other stuff that invokes long delays (a few ms) was causing issues running the stepper motor smoothly.

The first run once is delivered when the photon connects to wifi so the Arduino knows its ready.

Everything works as expected but I noticed yesterday while I was working on the Photon code that the atmega328 code seems to stop working around the 10-20 min mark when idle.

The code is a bit hefty but when idle you can see that what should be happening is as follows:

check_serial() —> if (serial.available) should be False so Nothing happens

motor_operation() —> switch (motor_status) should be case 0 which is empty so Nothing happens

switch (running_state) —> should be case 0 which is empty so Nothing happens

calibrate() —> calibrating = digitalRead(calibration_pin) —> if (calibrating) should be false so Nothing happens

So as follows, I can’t see what might be causing a crash or overflow. All variables are unchanged so the only thing that is happening constantly I suppose is that digitalRead. Could that cause the issue or perhaps there is something else I have missed.

I am fairly confident that motor_status == 0 & running_state == 0 at this point because otherwise, I would see some operation of my motor.

Any advice would be greatly appreciated.

Stepper_Slave_V2.ino (7.52 KB)

I cannot spot any obvious errors in the code, are you sure that the ATmega328 actually crashes? Have you tried to attach a LED which blinks every second to show if it has crashed?. Your issue may be in the Photon or it may be the serial connection that for some reason breaks.

Oh, that's a good point. I assumed the photon was working because it is still responsive and interfacing to my app but I did not consider the serial connection.

I did get a slight motor noise on the "crash" followed by unresponsiveness until reboot so I assumed maybe it had pulsed the enable pin and then frozen.

As it is PCB mounted I did not have any LEDs or anything to interface with but now you mention it I should establish some sort of external feedback from it and test that before troubleshooting the code any further.

Thanks for the eyes on tho.

Don't know if it is related to the symptoms that you are seeing, but I do notice that you have a recursive call of z_calibrate(), which could lead to memory problems if not controlled. It looks unnecessary anyway, so I wonder if it is a cut and paste error.
I would only do calibrating = digitalRead(calibration_pin) once through loop(). Store the previous value if you need to detect changes. Reading the pin in different places just makes it more difficult to reason about the program flow.
Does the calibration_pin have a pull-down resistor? If it is left floating then it will read unpredictably.