using either a rotory encoder or button press count to change pwm mode

There seems to be a significant amount of control logic where handling of inputs depends on past inputs. If I were you I'd design the control logic as a finite state machine.