multiple leds act as one [and ldr questions]

[edit] Changed the title for search purposes :slight_smile:

Hi all!

So another day, another silly question.

I have a sketch that fades a led using a ldr. But now i want to hook up more leds. They dont have to do anything complex besides all be triggerd by the ldr. Acting as one... I hope that was a good explanation.

So i thougt that i could use a array to get all the leds to function like one, but that doenst help... So can somebody give me some pointers?

Thanks

// Licht sensor

int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider
//int LEDpin = 11;          // connect Red LED to pin 11 (PWM pin)
int ledPins[] = {3, 5, 6, 9, 10, 11};
int LEDbrightness;        // 
void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);   
}

void loop(void) {
  photocellReading = analogRead(photocellPin);    
  // LED gets brighter the darker it is at the sensor
  // that means we have to -invert- the reading from 0-1023 back to 1023-0
  photocellReading = 1023 - photocellReading;

  Serial.print("Analog reading = ");
  Serial.println(photocellReading);     // the raw analog reading

  //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
  LEDbrightness = map(photocellReading, 850, 1012, 0, 255);

Serial.println(LEDbrightness);

  analogWrite(ledPins, LEDbrightness);
  
  delay(100);
}

If you read reference manual, you can see that analogWrite() method takes two arguments:

pin: the pin to write to. 


value: the duty cycle: between 0 (always off) and 255 (always on).

You can not give all the array to specify pin. You must call that analogWrite() method for each pin:
To do this in cycle, try something like:

int ledPins[] = {3, 5, 6, 9, 10, 11};

for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)
      analogWrite(ledPins[i],  LEDbrightness);

Thanks for the quick reply, that helped alot!

I am stuck on one other thing though...

My ldr is giving a good output but when i try to map it, so it becomes more sensitive, well i cant get the control i want.

With the current code i get this output:

Analog reading = 1015
Brightness = 253
Analog reading = 1015
Brightness = 253
Analog reading = 1015
Brightness = 253
Analog reading = 1015

This is with dim light, and when i shine a flashlight at it it becomes:

Analog reading = 877
Brightness = 218
Analog reading = 876
Brightness = 218
Analog reading = 875
Brightness = 218
Analog reading = 876

So as you can see the light isnt really fading away.

So the mapping function has helped me alot in the past and i understand it. So i made this little piece of mapping:

  LEDbrightness = map(photocellReading, 800, 1023, 0, 255);

This works quite well and now i see the light becoming less bright.

But as i said, i want more control over it and using the mapping works ok but not perfect.

For example when it becomes to light and the number is under the 800 as i mapped it goes all wierd... flashes on and off and all other stuff.

Are there any other things that i can do to gain more control?

So why don't you just increase range of analog reading to affect the birghtness value? For example. Instead of mapping values from 800 to 1023, do map of 768 (1023-255) to 1023 - then every analog value change will chane brightness of your leds.

Or even better way is to map all the range of analog reading (0 to 1023) to brightness value of something about 150 to 255.
First check the lowest PWM value of these leds on what you are playing on that causes minimal brightness and then use that value in yout mapping.

How did you hook the LDR up to your Arduino ?

The way to do it is to make it one of the legs in a voltage divider with a fixed resistor in the other leg. This should give you a bette resolution.

How did you hook the LDR up to your Arduino ?

Like this:

First check the lowest PWM value of these leds on what you are playing on that causes minimal brightness and then use that value in yout mapping.

Well the lowest should be 0 and highest 255. So the last suggestion isnt going to cut it.

So why don't you just increase range of analog reading to affect the birghtness value? For example. Instead of mapping values from 800 to 1023, do map of 768 (1023-255) to 1023 - then every analog value change will chane brightness of your leds

Sorry i dont follow, i coded it like this now:

  LEDbrightness = map(photocellReading, (1023-255), 1023, 0, 255);

But this gives olmost the same result as my other attempt.

Should i map the values twice?

Then I don't get what do you mean by "to get more control over it". :slight_smile:

Then I don't get what do you mean by "to get more control over it". Smiley

Well what i want is that i can calibrate the sensor to the place it is going to be in. So in "normal" daylight the lights should be off and when somebody is blocking the light it should light up to 255 (max brightness).

I did a little update on the code, including the multiple leds. They work pretty good.

I also added a mapping to try and get a more specific reading, but this makes it even less specific... So any tips on that are always welcome :slight_smile:

   int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
    int photocellReading;     // the analog reading from the sensor divider

    //int LEDpin = 11;          // connect Red LED to pin 11 (PWM pin)
    // More POWAH
    int ledPins[] = {3, 5, 6, 9, 10, 11};

    int LEDbrightness;        //
    void setup(void) {
      // We'll send debugging information via the Serial monitor
      Serial.begin(9600);   
    }

    void loop(void) {
      photocellReading = analogRead(photocellPin); 
     
      Serial.print("Analog reading = ");
      //Serial.println(photocellReading);     // the raw analog reading
     
      // LED gets brighter the darker it is at the sensor
      // that means we have to -invert- the reading from 0-1023 back to 1023-0
      photocellReading = 1023 - photocellReading;
     
      photocellReading = map(photocellReading, 0, 1023, 0, 10);
     
        Serial.println(photocellReading);
     
      //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
      LEDbrightness = map(photocellReading, 0, 10, 0, 255);

    //  analogWrite(LEDpin, LEDbrightness);
    // More POWAH
    for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)
       analogWrite(ledPins[i],  LEDbrightness);

    Serial.print("Brightness = ");
    Serial.println(LEDbrightness);
     
      delay(100);
    }

[edit]
I also made a new version of the breadboard layout:

[/edit]

Have you tried adjusting the value of the resistor? Your LDR is connected in what is called a Voltage Divider http://www.doctronics.co.uk/voltage.htm Adjusting this will increase/decrease the swing between low and high. LDRs, Phototransistors and LEDs in reverse bias can have large resistances and differ so much between manufacturers. Perhaps start with a 1k Pot and see what the numbers do as you adjust it. Reversing the logic is just a matter of swapping + for GND at LDR and Resistor to +

So i might have a chance by simply changing the resistor?

Hmm i was kinda hoping that i could do something in the sketch that does this...

Hows about a if else statement? Could that be a lifesaver in this case?

if readingOfSensor is less then 800 leds go off
else readingOfSensor is greater then 800 do the brightness mapping

Or is this to slow to give the impression of a led fading to the brightness of light?

Yay! my if else idea worked out pretty fine :slight_smile:

Final code:

/* Photocell sketch that lights up 6 leds when blocking the light */

int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the sensor divider

// More POWAH, more leds!
int ledPins[] = {3, 5, 6, 9, 10, 11};
int LEDbrightness;

void setup(void) {
  // We'll send debugging information via the Serial monitor
  //  Serial.begin(9600);   
}

void loop(void) {
  // Read analog pin
  photocellReading = analogRead(photocellPin);  
  
  // Debugging only
  // Serial.print("Analog reading = ");
  
  // LED gets brighter the darker it is at the sensor
  photocellReading = 1023 - photocellReading;
  
  // Debugging only
  //Serial.println(photocellReading);

// If statement to turn of all the leds when below a "normal" reading
if (photocellReading < 850)
{
 for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)
      analogWrite(ledPins[i],  0);
}
  // Or else! Or else what? Exactly, turn on the leds
  else
{
  //now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
  LEDbrightness = map(photocellReading, 851, 1023, 0, 255);
  // More POWAH, turn them all on
for (int i = 0; i< sizeof(ledPins)/sizeof(ledPins[0]); i++)
      analogWrite(ledPins[i],  LEDbrightness);
}

// Debugging only
// Serial.print("Brightness = ");
// Serial.println(LEDbrightness);
  
  delay(100);
}