Skip to content

Prevent divide by zero errors in temp calculation #20

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion adafruit_tcs34725.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,11 @@ def _temperature_and_lux_dn40(self):
lux = G1 / CPL

# CT Calculations (DN40 3.4)
CT = CT_Coef * B2 / R2 + CT_Offset
# Prevent divide by zero errors. if r2 = 0 use a really small r2 to print offset
if R2 > 0:
CT = CT_Coef * B2 / R2 + CT_Offset
else:
CT = CT_Coef * B2 / 0.001 + CT_Offset
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


return lux, CT

Expand Down