Fade led

Hi guys, i have a small problem

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;

void setup ()
{
pinMode (9, OUTPUT);
Serial.begin (9600);
}

void loop ()
{
valueLDR = analogRead (pinLDR);
Serial.println (valueLDR);

if (valueLDR> 1000)
{
analogWrite (9, brightness + = 255);
delay (10);
}

else

{
analogWrite (9, brightness- = 255);
delay (10);
}

}

I tried several combinations but it has not worked ):

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);
  }

}

see comments in code

Hi,
Welcome to the forum.

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?

Thanks... Tom.... :slight_smile:

yes, i too new in this forum and in the worl of arduino, sorry for no read the 1st post