Using Millis with optical encoder

You should not use counter when interrupts are enabled. Your broken version does exactly that.