Reputation: 73
I am designing a system where a light sensor detects light in a room and based on that it adjusts the output of a few light bulbs. The goal is to maintain a standard level of light in the room based on the environmental conditions such as external sunlight. The code is written in Python and uses Phillips hue bulbs to function. The pseudocode is as follows:
if (read light is in between 10 - 50 lumins) {
set bulb to 100 lumins
}
if (read light is in between 51-100 lumins) {
set bulb to 50 lumins
}
etc
However what ends up happening is that after each iteration, when the light is set to the specific value, the sensor detects the bulbs' own light and on the next iteration turns down the light. The lights end up flickering from high to low every second. Does anyone have any experience with this sort of thing, of an algorithm to deal with it? Essentially, the problem is that the light sensor is detecting the bulbs' own light and then undoing its previous decision. I am using a standard TSL2561 sensor to detect the light and the bulbs are Phillips hue.
Upvotes: 1
Views: 507
Reputation: 7061
The placement of the sensor is key in these situations. You can also try an optical filter but that is not the full solution.
Your algorithm is to crude to compensate for a dynamic environment. The real solution is to use a PID algorithm to make small adjustments over time to the light output to stay close to an ideal total (ambient+LED) light level.
See this example, there is many similar out there if you search for pid controller light sensor.
A simplified pseudo code representation for a PID like control system would be:
read in_lumins
if (in_lumins is in between 10 - 50 lumins) {
increment out_lumins
}
if (in_lumins is in between 51-100 lumins) {
decrement out_lumins
}
set bulb to out_lumins
Loop and repeat. Time increments on loop and/or increment size should vary with distance from ideal.
Upvotes: 3