Brightness Controll with LDR

Hey,

I'm currently trying to adjust brightness control using an LDR and three LEDS. Basically if there is a lot of light none of the LEDS show, if there is partial light, one shows, if there is mostly darkness two are on and if it's complete darkness all three LEDS turn on. I've been looking everywhere on the internet for some help but have been unsuccessful. Any help here would be much appreciated :).

Here is my code:

int LDR = 0;                                //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0;                    //that’s a variable to store LDR values
int light_sensitivity =  600;  //This is the approx value of light surrounding your LDR
 
void setup()
  {
    Serial.begin(9600);            //start the serial monitor with 9600 buad
    pinMode(13, OUTPUT);  
    pinMode(12, OUTPUT);
    pinMode(11, OUTPUT);   //we mostly use13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabled
  }
 
void loop()
  {
    LDRValue = analogRead(LDR);          //reads the ldr’s value through LDR which we have set to Analog input 0 “A0?
    Serial.println(LDRValue);                  //prints the LDR values to serial monitor
    delay(50);                                                //This is the speed by which LDR sends value to arduino
 
    if (LDRValue < light_sensitivity) 
      {
        digitalWrite(13, HIGH);
        digitalWrite(12, HIGH);
        digitalWrite(11, HIGH);
      }
    else
      {
        digitalWrite(13, LOW);
        digitalWrite(12, LOW);
        digitalWrite(11, LOW);
      }
  }

HazardsMind:
You want brightness control, yet your using digital HIGH/LOW pins, instead of using PWM~ pins + the analogWrite() function

That's not what etan32 wants to do. It was explained clearly in the first post.

Etan32, you will need 3 separate "if" statements, each controlling one led, instead of one "if" controlling all 3 like you have now. Set up 2 more values to compare the ldr value with, at perhaps 300 & 150, and use one of the 3 levels in each if statement.

Paul

Ok, bad on my part, I just read the title then skimmed the post and went from there.

Ok, I would use a case statements and the map function. This way everything is scaled and neat.
But IF statements are ok too, if you want to control at what brightness to set the LEDs to.

#define Lowest 0
#define Highest 600

LDRValue = map(analogRead(LDR), Lowest, Highest, 0, 3); // This will automatically scale your values

switch(LDRValue)
{
 case 0: // lowest LDR value = All LEDs on
     digitalWrite(13, HIGH);
     digitalWrite(12, HIGH);
     digitalWrite(11, HIGH);
     break;

 case 1:// LDR gets some light = 2 LEDS on
     digitalWrite(13, LOW);
     digitalWrite(12, HIGH);
     digitalWrite(11, HIGH);
     break;
 
 case 2: // More light = 1 LED on
     digitalWrite(13, LOW);
     digitalWrite(12, LOW);
     digitalWrite(11, HIGH);
     break;

 case 3: // Full brightness = All LEDs off
     digitalWrite(13, LOW);
     digitalWrite(12, LOW);
     digitalWrite(11, LOW);
     break;
}

HazardsMind, what will your code do if the ldr reading is over 600?

Also, although map () is a very useful little function, it is linear, but the human eye isn't!

Those values I used were just an example, I don't know his actual readings. It could be 0 - 1023. He could also add a default case to blink if need be.

map () is a very useful little function, it is linear, but the human eye isn't!

True, but if everything is scaled properly, (using the map function) you will notice a difference in light levels, with just 3 LEDs.
Now if he added more LEDs, then yes, it would be rather difficult to notice any changes.

Also he did add a Serial.println to his code, to see the values change.

You also need to bear in mind that LDRs themselves don't have a linear response - I think it's a square-law response, but a datasheet should give the details.

...R

Thanks for the replies everybody! HazardsMind, I am trying to implement your code into mine. I semi-understand what I have to do to get it work but I am having difficulty getting the coding right. Could someone please give me a hand?

etan32:
Thanks for the replies everybody! HazardsMind, I am trying to implement your code into mine. I semi-understand what I have to do to get it work but I am having difficulty getting the coding right. Could someone please give me a hand?

Post your current code with changes added. And, tell us what is/isn't happening.

Thanks

No need anymore, I have done it! Thanks for the help everybody