You shouldn't drive a led with the DAC directly. Could you share you schematic? I can't tell if the issue is noise or the schematic for example.
I see no issue with your code :
if voltage on pin A4 is between 2.45V and 2.55V, the DAC outputs 0.3V, else, it outputs 0V.