Comparing analog signals

(to OP) Yes, reading the reference resistor is a suspicious bit of code. It suggests that your circuit is not designed properly.