AFMotor shield - PWM strangely getting interrupted by sketch code

You changed something a the library, but you don't show what you have changed.
The pin numbers of the AFMotor library are hardcoded in the library, if you miss one of them, the library no longer works.