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