Go Down

Topic: how to reverse the function of an LDR sensor (Read 5640 times) previous topic - next topic

SJK70

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

liuzengqiang

You mean replace original code with this?
analogWrite(LED, 255-val/4);
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

robtillaart


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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

Or you could swap the positions of the LDR and the resistor in the voltage divider, without any change to the code.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

liuzengqiang


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! :smiley-mr-green: :smiley-mr-green: :smiley-mr-green:
We do software everything as much as we can before having to touch that breadboard again. ]:)

Yeah, swap them if you will.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

9000612

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

dougp

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.
Everything we call real is made of things that cannot be regarded as real.  If quantum mechanics hasn't profoundly shocked you, you haven't understood it yet. - Niels Bohr

No private consultations undertaken!

JCA79B

#7
Apr 23, 2019, 08:56 pm Last Edit: Apr 23, 2019, 09:03 pm by JCA79B
Like this, I use 47k for resistor, YMMV, connect test point to Arduino analog input pin:

9000612

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

Help, Thanks

Go Up