Skip to content

Commit 9cd93c7

Browse files
committed
Update TSL2591_AutoAdjust.ino
Fixed issues relating to overflow (negative lux values) that were leading to adjustment loops. I think the initial measurement after increasing gain would, for some reason, always overflow. Also, modified the TSL2591 library from Adafruit based on this change to the CircuitPython version adafruit/Adafruit_CircuitPython_TSL2591#6
1 parent dc4b3b8 commit 9cd93c7

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

TSL2591_AutoAdjust.ino

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,13 @@ bool advancedRead(void)
110110
garbage = false;
111111
} else {
112112
int newSetting = settingsCounter;
113-
// Take a measurement
114-
uint32_t lum = tsl.getFullLuminosity(); // Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum.
115-
double newLux = tsl.calculateLux((lum & 0xFFFF), (lum >> 16));
113+
double newLux = -1;
114+
uint32_t lum;
115+
do {
116+
// Take a measurement
117+
lum = tsl.getFullLuminosity(); // Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum.
118+
newLux = tsl.calculateLux((lum & 0xFFFF), (lum >> 16));
119+
} while (newLux < 0); // Sometimes happens with a sudden shift to darkness (passing shadow), or switch to high gain
116120

117121
// Interpret the measurement and decide if the gain should be adjusted
118122
if (newLux == 0 || newLux > 60000) { // The sensor saturated (leading to a returned zero) or is close
@@ -121,9 +125,9 @@ bool advancedRead(void)
121125
}
122126
} else if (newLux > 1700 * hysteresis) {
123127
newSetting = 1; // Use low gain setting
124-
} else if (newLux < 5 * hysteresis) {
128+
} else if (newLux < 2 * hysteresis) {
125129
newSetting = 4; // Highest gain setting
126-
} else if (newLux < 100 * hysteresis) {
130+
} else if (newLux < 10 * hysteresis) {
127131
newSetting = 3; // Use high gain setting
128132
} else {
129133
newSetting = 2; // Use normal gain setting
@@ -145,7 +149,8 @@ bool advancedRead(void)
145149
return true;
146150
}
147151
} else {
148-
Serial.print("Updating setting "); Serial.print(settingsCounter); Serial.print(" to "); Serial.println(newSetting);
152+
Serial.print("Measured "); Serial.print(newLux);
153+
Serial.print(" Updating setting "); Serial.print(settingsCounter); Serial.print(" to "); Serial.println(newSetting);
149154
settingsCounter = newSetting;
150155
configureSensor(settingsCounter);
151156
}

0 commit comments

Comments
 (0)