Reactive lightning

Hi

I´m trying to controll an pwm output but i´m not abel to make it fade the way i want.

I´m using a ldr to read the light intensity and i want the led to fade when the value gets to high. I want the led to fade down a bit and when the light intensity value sinks i want the led to fade up.

Anyone got any ide of solving this??

enough ideas,

can you post your code written so far?
it is easier to see deltas on your own code.

It´s not much but it´s a start i think..

const int sensorlightPin = A0 ;       // Sensor Light
const int sensorheatPin = A1 ;        // Sensor Heat
const int analogOutPin = 5;           // Led 
const int ledoutvaluemin = 150;       // Led min 
const int ledoutvaluemax = 1024;      // Led max 
const int ledheatmax = 512;           // Led max Temp
const int sensorlightmax = 950;       // Sensor Light max
const int fade = 10;                  // Fade



int sensorlightvalue = 0;       // Variabel Sensor light
int sensorheatvalue = 0;        // Variabel Heat
int ledoutvalue = 0;            // Variabel Led 
int lastreading = 0;            // Lastreading Light
int ledoutvalue1 =0;            // 0-255 PWM Led



void setup() { 

  Serial.begin(9600);
}


void loop () {
  
 sensorlightvalue = analogRead(sensorlightPin);  // Read Light
 sensorheatvalue= analogRead(sensorheatPin);   // Read Temp
 lastreading = analogRead(sensorlightPin);      // Read lastreading Light
 delay(10);



if (sensorlightvalue > sensorlightmax) 



 if (sensorheatvalue > ledheatmax) {
   ledoutvalue = ledoutvaluemin;
 }
 
  else if (sensorlightvalue > sensorlightmax)
 {ledoutvalue = lastreading - fade;}  
  
  
 
  else ledoutvalue= ledoutvaluemax;
  ledoutvalue1 = map(ledoutvalue, 0, 1023, 0, 255);
  
  
  
  analogWrite(analogOutPin, ledoutvalue1);
 
 
 
 
 Serial.print ("SensorLight = ");
 Serial.print (sensorlightvalue);
 Serial.print ("\t SensorTemp = ");
 Serial.println (sensorheatvalue);
 Serial.print ("LedValue = ");
Serial.println (ledoutvalue);
Serial.print ("PWM led = ");
Serial.println (ledoutvalue1);
 
 delay (200);
  
}

it is quite a lot already

Please use [ code] tags --> # button above smileys (you can still modify your post)

you have to learn to use good layout : TIP use CTRL-T in the IDE and see what happens,

I'll have a look at the code

refactored to only your requirements in your first post
removed unneeded comments as you use good variable names.

const int sensorlightPin = A0 ;
const int analogOutPin = 5;
const int sensorlightmin = 200; 
const int sensorlightmax = 950; 
const int fade = 100;       

void setup() 
{ 
  Serial.begin(9600);
}


void loop () 
{
  // I´m using a ldr to read the light intensity 
  int sensorlightvalue = analogRead(sensorlightPin);
  int ledoutvalue = sensorlightvalue;

  // i want the led to fade when the value gets to high
  if (sensorlightvalue > sensorlightmax)
  {
    ledoutvalue = sensorlightvalue - fade;
  }

  // and when the light intensity value sinks i want the led to fade up
  if (sensorlightvalue < sensorlightmin)
  {
    ledoutvalue = sensorlightvalue + fade;
  }

  // adjust the value to the output range
  ledoutvalue = map(ledoutvalue, 0, 1023, 0, 255);
  // and apply to the LED
  analogWrite(analogOutPin, ledoutvalue);

  Serial.print ("SensorLight = ");
  Serial.print (sensorlightvalue);
  Serial.print ("LedValue = ");
  Serial.println (ledoutvalue);

  delay (1000);
}

Thank you !!

Works great but i´m having one more issue and it´s to make the led fade ( smooth or stepwise) up and down. Any advice ?

make fade a formula instead of a constant. make it depend on the amount above the max or min.

  if (sensorlightvalue > sensorlightmax)
  {
    fade = 4 * (sensorlightvalue  - sensorlightmax);
    ledoutvalue = sensorlightvalue - fade;
  }