Great, but this is already long made and supported on many computer mice. Just a click to switch between cogged and freespinning mouse wheel.
Also, you shall learn how to split source code into files.

//EDIT: Also, to stay on topic. Adding an ESD protection device on the USB would not hurt. USBLC6-2SC6, to name one.
Also, having just only a small ceramic cap on the power supply input without overvoltage protection or additional dampening is asking for trouble.
Add via stitching to your ground plane!
Why does the NRST pin appear to not be decoupled to ground with a 100n cap, rather to some other trace common with likely BOOT0 pin? Where is the BOOT0 pull-up resistor? This pin is HiZ, there is NO integrated pull-up.
And lastly a design note: Why a DC motor as the encoder? You need a POSITION encoder, not velocity one.