Hello,
I want to link the light sensor of my LilyPad to my LED, so that the LED gets lighter when the light around gets darker. I tried this code:
int light=A6;
int lightval=0;
int ledlight=9;
int brightness=0;
void setup() {
pinMode(ledlight, OUTPUT);
pinMode(light, INPUT);
}
void loop() {
lightval= analogRead(light);
lightval /= 4
brightness= 255-lightval;
analogWrite(ledlight, brightness);
}
But this code gives me the exact opposite result than wished, the LED goes darker if I cover the light sensor.
When I tried to reverse the value of brightness by setting brightness to lightval, the LED was just on all the way through.
Now what can I do?
It would make approximately -750, so I divided lightval by 4 before calculating brightness, which I expected to give me the right outcome, but the outcome stayed the same.
If the input from the light sensor is 1007, does that mean, there is much light? Because then the calculation would be like following:
With the sketch right now, the diode gets brighter nicely when the light gets brighter.
But of course I want the diode to increase in brightness when the light gets darker.
So it works well- just the other way round. It´s supposed to be like a night light.