Digitizing output for QTRX Sensors

Hello. I have a simple task at hand. I simply want to know if the surface under my sensor is black or white. How could I go about doing this(a simple binary value of 0 or 1)?

Initially I thought I could perhaps take the calibrated maximum of each sensor and say if value >= calibrated_maximum - 100, it is black and if value <= calibrated_min + 100 then it’s white. But this causes an issue that there is a range of values that’s missing from being caught by these if statements(suppose values between calibrated max and calibrated min). I picked 100 arbitrarily.

Any other, possibly simpler and more reliable ways to just say whether a surface under the sensor is black or white?


Any method you choose for discretizing that is going to have some type of drawback since the possible inputs are not discrete. If you want your algorithim to always return one of two answers (0 or 1), then you ultimately need to decide how to handle grey inputs. (Even if you only have black and white sufaces, it is still possible for the sensor to read an intermediate value, for example if it is right on the edge of where black meets white.)

The simplest method would probably be to set your threshold right in the middle of your range. Alternatively, you might consider building some hysteresis type behavior into your algorithm.

- Patrick