Separate the code in short and simple functions
Your current functions are way too long: for example, main()
has 1282 lines. That's much more than acceptable.
While the exact maximal number of lines is a subjective thing, there are some good rules of thumb. For example, this question mentions 100 to 200 lines as the upper bound. Ideally, every function should do just one thing and do it well - this called SRP, single responsibility principle.
In many programs main()
is somewhat an exception of this, as it usually does both initialization (by calling an init()
or similar function) and the main loop. Still, the main loop should consist mainly of calls to other functions and/or state machine logic. I believe the other answers give more details on structuring it.