Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent divide by zero errors in temp calculation #20

Closed
wants to merge 1 commit into from

Conversation

@dsiee
Copy link

dsiee commented Jul 19, 2019

If a black object or if it is low light causes R2 in _temperature_and_lux_dn40 to be zero the line for color temperature calculation will throw a divide by zero error. This can be repeated by running tcs34725_simpletest.py which prints lux and temp, disabling the LED by pulling LED pin to GND and placing something on the sensor to block ambient light (e.g. your finger). This can also occur, although less reliably, with a very black object with the LED on.

Proposed fix is just an if to check if R2 is greater then zero. If it isn't use a small value in it's place to allow B2 to still be considered. Not sure that this is the best solution.

If a black object or if it is low light causes R2 in _temperature_and_lux_dn40 to be zero the line for color temperature calculation will throw a divide by zero error. This can be repeated by running tcs34725_simpletest.py which prints lux and temp, disabling the LED by pulling LED pin to GND and placing something on the sensor to block ambient light (e.g. your finger). This can also occur, although less reliably, with a very black object with the LED on.

Proposed fix is just an if to check if R2 is greater then zero. If it isn't use a small value in it's place to allow B2 to still be considered.
@ladyada ladyada requested a review from adafruit/circuitpythonlibrarians Jul 20, 2019
Copy link
Contributor

tannewt left a comment

One comment on the change.

if R2 > 0:
CT = CT_Coef * B2 / R2 + CT_Offset
else:
CT = CT_Coef * B2 / 0.001 + CT_Offset

This comment has been minimized.

Copy link
@tannewt

tannewt Jul 22, 2019

Contributor

Instead of copying and changing the computation line please invert the if to check R2 == 0 and then set R2 to 0.001 instead.

This comment has been minimized.

Copy link
@caternuson

caternuson Jul 23, 2019

Contributor

How about limiting all the values in the initial computation of R2, G2, and B2?

R2 = max(0.001, R - IR)
G2 = max(0.001, G - IR)
B2 = max(0.001, B - IR)

and also define a const up to for the 0.001 value

@caternuson

This comment has been minimized.

Copy link
Contributor

caternuson commented Nov 15, 2019

Closing due to lack of activity. This was taken care of by #24.

@caternuson caternuson closed this Nov 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.