how to reverse the function of an LDR sensor

Hi guys,

I have a code (below) that reacts to the proximity of a person, so the less light the LDR recieves (the closer you get), the dimmer the LED gets.
The more light the LDR receives, the brighter it gets.
I simply want to reverse this function, so that the closer you get to the LDR (the less light it recieves), the brighter the LED shines.
When the LDR receives lots of light, I want the LED to be dimm, (or in the future blinking at a slow rate!)

Does anyone know how to simply reverse this function of the LDR, here is the code:


//Example 06B: Blink LED at a rate specified by the analogue input

#define LED 9 // the pin for the LED

int val = 0; // variable used to store the value coming from the sensor

void setup() {
pinMode(LED, OUTPUT); //LED is as an OUTPUT

// Note: Analogue pins are automatically set as inputs..
}

void loop() {
val = analogRead(0); // read the value from the sensor

analogWrite(LED, val/4); // turn the LED on at the brightness set by the sensor

delay(10); // stop the program for some time

}


Any help at all is appreciated,

Cheers,
Sam
mrkrules@hotmail.com

You mean replace original code with this?
analogWrite(LED, 255-val/4);

For more complex behaviours you might look up the Map function - http://arduino.cc/en/Reference/Map

Or you could swap the positions of the LDR and the resistor in the voltage divider, without any change to the code.

AWOL:
Or you could swap the positions of the LDR and the resistor in the voltage divider, without any change to the code.

NOOOO! That is too much hardware modification! :grin: :grin: :grin:
We do software everything as much as we can before having to touch that breadboard again. ]:slight_smile:

Yeah, swap them if you will.

What are the pins to put the ldr in and how to get power.

9000612:
What are the pins to put the ldr in and how to get power.

Without a schematic or some other form of documentation/description it's impossible for anyone but you to know.

Like this, I use 47k for resistor, YMMV, connect test point to Arduino analog input pin:
LDR.png

So can someone send me the schematic to tell me how to wire it with the arduino

Help, Thanks