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);
}
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.
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
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
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:
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 +
/* 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);
}