Removing ambient light level from ldr input

I'm using a simple LDR to receive the light level when someone shines a torch like device against the ldr. I need to detect more than just on/off but the intensity of the torch and try to keep this somewhat uniform in different ambient light levels.

I want to somehow subtract the ambient light level from the current signal, is there some general algorithm for this? The easiest way would just be to take a snapshot of the input at some point and use this as a baseline to subtract. But i'd rather use some method that is automated and doesn't.

Any ideas?

Use a push button to read the ambient, then subtract that from the following readings:
Pseudo code:

if(buttonPressed)
ambientValue = analogRead(LDR);

ldrValue = analogRead(LDR) - ambientValue;
Serial.println(ldrValue);
delay(500);

Or something like that.

kimjongilla:
I'm using a simple LDR to receive the light level when someone shines a torch like device against the ldr. I need to detect more than just on/off but the intensity of the torch and try to keep this somewhat uniform in different ambient light levels.

I want to somehow subtract the ambient light level from the current signal, is there some general algorithm for this? The easiest way would just be to take a snapshot of the input at some point and use this as a baseline to subtract. But i'd rather use some method that is automated and doesn't.

Any ideas?

That's how its done, but it won't be great because LDRs aren't linear and are temperature sensitive.
They aren't great for any accurate measurements. Photodiodes are used for measurement as their current is linear with light intensity and pretty much unaffected by temperature.

In a photodiode light particles create electron-hole pairs that each contribute immediately and directly to the measured current.

In an LDR light causes a background population of charged particles whose concentration is then sensed by
measuring the resistance of the material. The number of particles is a balance between those created by the
light and those that recombine by chance. The chance thing is both temperature sensitive and not linear in the light flux, as well as being slow.

I think your real problem is to define ambient light! It will be different in each direction it is pointed at. Be sure to use a diffuser over the sensor. FYI For optimized response, luminous intensity measurements are carried out in isolation without interference of ambient lighting. Measurement instrumentation is commonly used to acquire multiple photometric measurements to assess the broad optical characteristics of a light source. Photometers capable of carrying out luminous intensity measurements must be equipped with either a cosine corrector or an integrating sphere in 2π configuration. The first however is the most compact and thus most practical solution. I would use a different sensor but that is your choice not mine.