Unified sensor read questions

I am using a TSL2591 light sensor and run into some problems with the UnifiedSensorApiRead function. I want it to return the float Lux variable using this code :

void unifiedSensorAPIRead()
{
/* Get a new sensor event /
sensors_event_t event;
tsl.getEvent(&event);
//
/
Display the results (light is measured in lux) /
Serial.println (" -----------Unified Sensor API read----------");
Serial.print("[ "); Serial.print(event.timestamp); Serial.print(" ms ] ");
Lux = float(event.light);
if ((Lux == 0) |
(Lux > 4294966000.0) |
(Lux <-4294966000.0))
{
/
If event.light = 0 lux the sensor is probably saturated /
/
and no reliable data could be generated! /
/
if event.light is +/- 4294967040 there was a float over/underflow */
Serial.println("Invalid data (adjust gain or timing)");
}
else
{
Serial.print(Lux); Serial.println(" Lux");
}
}

Question 1: it will sometimes return negative values. How so and what to do about it?

Question 2: I want to change the Gain of the sensor depending on the aquired value of Lux.
I have no clue when to change form Gain 0 to Gain 1 or 2.

Do you want to return the lux value to some caller function? Or just display it on the serial console?

From the TSL2591 library (function calculateLux):

@returns Lux, based on AMS coefficients (or < 0 if overflow)

So if the the return value is < 0 the light sensor is probably saturated. Basically this means too much light was sensed. So this is the perfect spot to adjust the gain or timing (as the code already mentions). Just decrease the gain (the sensor will be less sensitive) or lower the integration time (the sensor will sense for a shorter time) until you don't get overflows anymore.

However, it might be a good idea not to decrease/increase the gain/timing immediately. Only if the sensor reads back saturated data for lets say five consecutive times or so. Otherwise the sensor will be too sensitive to changes in the overall brightness I suppose.

Lux is a global variable and I want to use it for calculation of contrast and exposure time for photograpic purposes.
I am designing a densitometer and have a construction that sheds LED light on paper and measures its reflection with the sensor. Now the sensor seems to be overloaded even with a gain of 0 .
I can hardly imagine that de leds produce more than 88000 lux.