Lag(?) issues

No, just about any software I've written to drive anything mechanical will have been running at least ten times as fast as it needs to.
I don't understand what you mean by clearing memory - What purpose would that serve?
And no, I don't think you have a memory problem.

Comments about what you think the code is supposed to do would be useful.
Maybe Google "state machine"