PWM led control based on SRF05 distance

Hello,
I have written a code for controlling led with pwm based and srf05 sensor readings.
The code almost works.The only problem is that the ledvalue doesn't go to 0 when my hand is almost 1 to 2cm away from sensor.
Any suggestions. I am kinda new to arduino.

P.S : the ledvalue is calculated approxiamtely

int duration;                          // Stores duratiuon of pulse in
int distance;                          // Stores distance
int sensorpin = 7;                        // Pin for SRF05
int ledvalue = 0;
int ledpin = 9;
void setup()

 {

}

void loop()

{
  pinMode(sensorpin, OUTPUT);
  digitalWrite(sensorpin, LOW);           // Make sure pin is low before sending a short high to trigger ranging
  delayMicroseconds(2);
  digitalWrite(sensorpin, HIGH);          // Send a short 10 microsecond high burst on pin to start ranging
  delayMicroseconds(10);
  digitalWrite(sensorpin, LOW);           // Send pin low again before waiting for pulse back in
  pinMode(sensorpin, INPUT);
  duration = pulseIn(sensorpin, HIGH);    // Reads echo pulse in from SRF05 in micro seconds
  distance = duration/58;             // Dividing this by 58 gives us a distance in cm
  delay(50);   // Wait before looping to do it again
  
  if(distance < 255)
  {
    ledvalue = distance + 1.5 ;
    analogWrite(ledpin,ledvalue);
 }
if (distance > 0)
{
  ledvalue = distance - 1.5;
  analogWrite(ledpin,ledvalue);
}
}
int distance;                          // Stores distance
int ledvalue = 0;

    ledvalue = distance + 1.5 ;
  ledvalue = distance - 1.5;

Since ledvalue and distance are both integer, adding or subtracting 1.5 doesn't make sense. Please explain.

  if(distance < 255)
  {
    ledvalue = distance + 1.5 ;
    analogWrite(ledpin,ledvalue);
 }
if (distance > 0)
{
  ledvalue = distance - 1.5;
  analogWrite(ledpin,ledvalue);
}

If distance is 300, that's not less than 255, so the first block will be skipped. The value is greater than 0, though, so the block will be executed, with a value of 299 being used as the duty cycle. This is greater than the maximum allowed value, so, what value is really used?

Suppose the value in distance is 27. This value is both less than 255 and greater than 0, so analogWrite() will be called twice, with different values.

This is probably not what you intended to do. So, please explain what you did intend.

i know that using this code wont be good but it somehow works.
and yes integer and 1.5 doesn't at all makes any sense.My bad.

What i want is that as the distance(in cm) of SRF05 decreases the led should get dimmer and as the distance increases the led should get brighter

and yes integer and 1.5 doesn't at all makes any sense.My bad.

No problem as long as you are aware that the .5 on the end is ignored.

What i want is that as the distance(in cm) of SRF05 decreases the led should get dimmer and as the distance increases the led should get brighter

So, you want to set the LED level if the distance is greater than 0 (it can't not be) AND the distance is less than 255.

if(distance >= 0 && distance < 255)
{
   ledvalue = distance;
   analogWrite(ledpin, ledvalue);
}

Thanks that was so simple.

Btw if i want to control LED exactly between 0 to 255 based on readings from srf05 what will i have to use ?
i mean should i try mapping ?

Btw if i want to control LED exactly between 0 to 255 based on readings from srf05 what will i have to use ?
i mean should i try mapping ?

Since the range of distances of interest (0 to 255) matches the range of PWM values allowed (0 to 255), I'm at a loss to understand what mapping you think you need.