Running sub-loops causes software to lock up over time

Hey everyone,

I am working on my first project and using buttons to decide which loop is being run. The loop then controls a stepper motor and a specific motion is made. I also have an analog angle sensor that reads the angle of the stepper motor and resets it to zero before a new loop starts. This happens on initialization and anytime a button is hit. The loops should run until the Arduino is turned off or another button is hit

The issue I am having is that if I hold a button or mash buttons, my code freezes and my stepper motor stops. I am assuming that this is an issue with running out of RAM which is most likely due to how my code is set up. There are a lot of lines so I'm going to write a quick pseudo code to explain how everything is set up.

void setup ()
--run alignment code to reset position

void buttonOneLoop ()
----if alignment value is zero, run alignment value

--while x < value
----run clockwise loop
----check buttons for H or L signal
------if button H, run loop for that button

--while (x > value) && (x < 2*value)
---- run counter-clockwise loop
----check buttons for H or L signal
------if button H, run loop for that button
--if (x <= 2value)
----x = 0

This is repeated or each button, then

void loop()
--run alignment code (initial alignment)
--read buttons and start sub-function if button is high.

so all in all, each time a button is hit, a subloop is started, an alignment runs, and the loop runs. while the loop is running it is constantly checking to see if another button is hit.

what worries me about this, is each time a button is hit, a new subloop starts. This would mean that by the third loop change, I would be in a loop, inside of a loop, inside of a loop. I have never coded in ide before so I am unsure if this what is happening or not

if you have any recommendations on how to rewrite the loop or on what the problem might be, please let me know!

Attached below is an image of the serial window and what I am seeing:

I think that we need to see the real code. Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Explain exactly what you are trying to do. There may be a way to accomplish your goal that you have not though of.

Also, post a schematic showing how everything is wired, especially the switches.

If you are re-entering functions, that can cause the stack to fill up.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.