Best to simplify early on,
Just about every micro's ADC can operate in differential mode, e.g. the difference between 2 ADC inputs. So apart from the boilerplate to get the ADC set up, your only dealing with 1 measurement.
Next up, PWM, pick a suitable pin for the method of PWM you want, likely using a 16 bit timer to allow finer control. (these are baked in to just about every 8 bit micro out there), this is just 1 output, the timer compare register. and using the timer will mean it runs on its own, independent of what the processor is doing.
And finally the switch, seeing as your not doing much, you can just sit there polling it in the time between ADC measurements, when it changes state, you use an 8 bit timer, and if it stays in that state until your debounce period has elapsed you treat it as valid and do whatever you where planning
All that leaves is the averaging, now in reality I would just size my variable to take all the summed values and work off that rather than averaging, as it gives much finer granularity,
E.g. you have a 10 bit ADC, you can sum up to 32 readings and be confident it will fit in a 16 bit value.
or if you really want a rolling average, then you need 16 bit chunks of memory to hold each sample. you keep track of a pointer in that memory area, each new sample you add the new sample to the total, then subtract the old and overwrite the old samples location with the new one in RAM. (lets say 32 bytes for 16 samples, plus 1 byte for the pointer, and 2 bytes for the Total
Finally control, you have some desired current, so you subtact your average (total) from your desired value (ADC value for current * number of averaged samples) and sum it to the PWM output, you can divide down this difference by powers of 2 to reduce the gain of the corrections, but as the motor has a mass, the micro will be making corrections way faster than the mass can respond, so integral only control works out beautifully.