LDR programming with Roboduino

this is the simple program for glowing two LEDs when LDR is exposed to light.
void setup()
{
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(2,INPUT);
}
void loop()
{
analogWrite(9,analogRead(2));
analogWrite(10,analogRead(2));
}

now how to write a program in which.... one LED have to glow while other remains dark when LDR is exposed to light. And when the light is cut off from from LDR 1st will gonna dark while the 2nd will glow?
explain the logic.

void loop()
{
  if(analogRead(2) < 500)
  {
    analogWrite(9,100); // 100 can be changed to "analogRead(2)"
    analogWrite(10,0);
  } 
  else
  {
    analogWrite(9,0);
    analogWrite(10,100);
  } 

}

If you're writing 0, 100 levels, could also just use digitalWrite (9, HIGH), and (9, LOW). No PWM needed.

analogWrite writes a PWM value from 0 to 255. analogRead returns a value from 0 to 1024. Depending on the range of values you're reading from your LDR, you may have to "map" from one to the other. arduino.cc/en/Reference/Map

--Michael

can you please explain me the logic? i m soo new to this world of arduino

I'll try. Note that this is seperate from the problem of how to make the two leds brighten and darken opposite to one another.

You have two ranges, one for the analogRead of your LDR (0 to 1023), one for the analogWrite to an led (0 to 255). That they're not the same width is the issue.

Say you get a reading of 1023 (or any reading greater than 255) from your LDR. You can't analogWrite this value to the led; it's bigger than the PWM 255 maximum. So what you need is a mathematical method for proportionately shrinking the read scale to fit the write limit.

The Arduino library includes a function to do that called "map." (See the link in my previous post and the explanation on that page.) With map, you can get the values you need so that:
In 0 == out 0.
In 1023 == out 255.
And every value in between, proportionately.

A complication is that the reading you get from your LDR will probably not reach the full range of 0 - 1023. You will need to do some experiments to find out what range of response you get from your sensor and your light conditions.

Hope this helps.
--Michael

Edit: See the example at the bottom the the map page. It is exactly what I describe here.