Flyback diode is acting weird
The LM311 has an open-collector output. You need to connect the open emitter to ground, not VCC, and provide a pullup resistor on the open collector output.

Don't try to use an LED as a flyback diode. Use a diode with a high surge current rating.