As far as I can tell from your limited description, you are using some homebrew optocoupler to defect some opaque object passing between the LED and the photodiode.
What are the usual failure modes here:
Optics lose alignment. I.e. the brightest spot of the LED is not on the photodiode anymore.
Reasons: Mechanical stress, substandard build quality.
Mitigation 1: There are factory-made air-gapped optocouplers that could perform better in this regard. They have a rigid package containing both elements aligned against each other.
Mitigation 2: Use a visible light LED. They still can lose alignment, but one would be in a better position to diagnose what happened.
The LED ages and dims.
Reason: Mediocre quality LED driven at high current.
Mitigation 1: Use better LED, drive it 50% of the rated current (or lower). Be aware that this will lower the signal/noise at the receiver side.
Mitigation 2: Use AC-driven LED, detect AC signal at the receiver side. Complex. On the other hand, you may find an "optical sensor" element that does everything this in a single package.
The LED and/or the photodiode are accumulating dust.
Reason: It simply happens, especially when something moves.
Mitigation 1: Air the dust once in a while.
Mitigation 2: Use another kind of sensor (e.g. magnetic, acoustic, capacitive) if applicable.