i try to fade a led when a ldr be on a given value
for a example: i want that when the ldr value be => 1000, the led fade from off state to highest brightness and the led stay on but the problem is that the led fade and fade in a loop, do not stay on.
and the opposite, when the ldr value = <1000
it's part of my code:
int brightness = 0;
int pinLDR = 5;
int valueLDR = 0;
int brightness = 0;
int pinLDR = 5;
int valueLDR = 0;
#define LDRMAX 1000 // Set max value for LDR
#define LDRMIN 250 // Set min value for LDR
void setup ()
{
pinMode (9, OUTPUT);
Serial.begin (9600);
}
void loop ()
{
valueLDR = analogRead (pinLDR);
Serial.println (valueLDR);
if (brightness < 255 && valueLDR > LDRMAX) // Check if brightness is lower then 255. otherwise it will overflow. PWM only uses values from 0 to 255
{
analogWrite (9, brightness += 1); // you typed "+ =" must be "+="
delay (10);
}
else if (brightness > 0 && valueLDR < LDRMIN) // Check if brightness is higher then 0. otherwise it will overflow. PWM only uses values from 0 to 255
{
analogWrite (9, brightness -= 1); //you typed "- =" must be "-="
delay (10);
}
}
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?