Simple fade transition

I am trying to create a simple transitional fade effect when I turn an LED on/off. I have been at it for days and can’t figure out how to do it. I have a sensor and when someone steps into a certain range, I want the LED to FADE ON and stay ON. When he/she walks out of that range I want the LED to FADE OFF and stay OFF. I do not what the light to oscillate when it is on or off, just fixed status…

it is just a simple transitional effect…

int SensorPin = A0;
#define ledPin 9

void setup() {

pinMode(9, OUTPUT);
pinMode(SensorPin, INPUT);
Serial.begin(9600);
}

void loop() {

int val = analogRead(SensorPin);

if (val >= 150) // pre-defined distance i am using
{digitalWrite(9, HIGH);} // At this point I want the LED to FADE ON

else if (val <= 170) // pre-defined distance i am using
{digitalWrite(9, LOW);} // At this point I want the LED to FADE OFF

Serial.print(val);
Serial.println(" ");}

Did you try Fade Example code?

int SensorPin = A0; 
#define ledPin 9

void setup() {                

  pinMode(9, OUTPUT); 
  pinMode(SensorPin, INPUT);
  Serial.begin(9600);
  }

void loop() {
  
  int val = analogRead(SensorPin);
  
  if (val >= 150)             // pre-defined distance i am using
   {for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
}    
   
  else if (val <= 170)      // pre-defined distance i am using
  { for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  } 
}    
  

Serial.print(val);
Serial.println(" ");}

I did... I don't want it to pulse. It is just a simple transition effect. I want it to fade from off to on and then stay on when someone is in range. When he/she moves. it fades off...

check out this similar post.

The last post you had was close.

this video is essentially what i want.. when the sensor is triggered it gradually comes on. when you walk of range, the light gradually shuts off... https://www.youtube.com/watch?v=A-EEppHIzxs

I would like to do this with the arduino rather than building the circuit..

so try the example I provided and play with the delay(10) to something like delay(20) in the two functions like this:

you have to experiment with it and see what you get…

int SensorPin = A0; 
int ledPin = 9;
int oldVal;

void setup() {                

  pinMode(9, OUTPUT); 
  pinMode(SensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {

  int val = analogRead(SensorPin);
  if (val <= 150 && oldVal > 150)//<<<<<<<<<<<< 
  {
    fadeUp();
  }      
  else if (val >= 170  && oldVal < 170)//<<<<<<<<<<
  {
    fadeDown();
  }
  oldVal = val; 
}

void fadeUp()
{
  for (int i = 0; i <= 255; i++)
  {
    analogWrite(ledPin, i);
    delay(20);
  }
}

void fadeDown()
{
  for (int i = 0; i <= 255; i++)
  {
    analogWrite(ledPin, 255 - i);
    delay(20);
  }
}

the lights are on when they should be off and they are still pulsing. also, the sequence is also backwards.. I tried flipping the fadeUp and fadeDown int's you built and that doesn't fix it.