Need help writing a state machine

You might want to use a plain Arduino board and a handful of parts to learn how to program, or improve your skills at doing.

You now have a substantial complicated program buried in the hardware, this just compounds the difficulty by making your problem akin to finding lima beans during a hurricane.

If there's a basic principle here it is divide and conquer. Divide your work into separate problems by design. Get sections working. These might correspond to the various junk you have hanging off the Arduino. Conquer when combinating these working pieces into a smoothly functioning hole.

And… keep the little sketches you wrote or found that test all the junk each one by one. When trouble strikes, get them out and check section by section after you've eliminated obvious problems.

You may want to do sox me of this work without spending money of dealing with the complications real hardware always brings. Here a demo of "state change detection", another key concept down here, using the wokwi simukator:

a7