Arduino Light blinking

Hi I am new to Arduino so this may be a stupid question, I have a ultrasonic sensor and a Led that is supposed to blink in different intervals depending on the distance of the object from the sensor. The Led is blinking in strange intervals

Please help

This is my code

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
int Ledpin = 11;

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(Ledpin, OUTPUT); // set the ledpin as output
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// 50 cm away from object
if (distance <=50){
digitalWrite(Ledpin, HIGH);
delay(250);
digitalWrite(Ledpin,LOW);
delay(250);
}
//40 cm away from object
if (distance <=40){
digitalWrite(Ledpin, HIGH);
delay(200);
digitalWrite(Ledpin,LOW);
delay(200);
}
// 30 cm away from object
if (distance <=30){
digitalWrite(Ledpin, HIGH);
delay(150);
digitalWrite(Ledpin,LOW);
delay(150);
}
// 20 cm away from object
if (distance <=20){
digitalWrite(Ledpin, HIGH);
delay(100);
digitalWrite(Ledpin,LOW);
delay(100);
}
// 10 cm away from object
if (distance <=10){
digitalWrite(Ledpin, HIGH);
delay(50);
digitalWrite(Ledpin,LOW);
delay(50);
}
}

You logic is not correct. If you calculate the distance as 10, all of you if() statements will be true so all of them will execute. A better approach would be to

if(distance > 60) {
  // do nothing
} else if( distance > 50 ) {
  // do 50-60 stuff
} else if( distance > 40 ) {
  // do 40-50 stuff
}
//...

@nick9920 if you want the led to blink at certain points
(50cm the red led blinks, at 40 the blue one, or something like that)

there is a problem with your logic which @blh64 pointed out but he/she provided a basic explanation, you seem to be new to programming so I will break it down for you

Your code in the first if statement checks if the distance of the object is < or = than 50
So when you place the object at 50cm your code will say “is the distance <= than 50? Yes”
and it light up your led.
After this it will check the 2nd and it will realize that it is not at 40 and skip that chunk of code until the next if-statement.

The problem is that <= 50 includes a distance of 50cm as well as 40 and 30 and 20 and 10.
The solution would be if-else statements:

With an if-else if the first statement is true (<=50) your arduino won’t bother checking the other else-if statements. With only if-statements it will have to check every single one and many will return true.

For example if your object is 30cm away your code will check the 50 and it will blink because the distance is <=50, it will do the same with 40 and 30, then it will check 20 and 10 but it will not execute them because they will be false.

With the if-else it will only check if it is <= than 50 and it will not check 40 30 20 10.
You can see anther problem here, and this is why it is better if you put >= instead of <=

This should solve your issue, if you need more explanation let me know