How can I make motor vibrate using arduino depending on the ultrasonic distance? I want to make the vibration motor vibrate depending on the distance to an obstacle, for example if the distance is too short it should vibrate quickly and vice versa.

How can i make motor vibrate using arduino depending on the distance?
Can you explain for me how can I use the delay in the vibration code?
I found this code in the internet for vibration:

digitalWrite(y, HIGH);
delay(50);
digitalWrite(y, LOW);
delay(50); 


I know that I can change the delay, but how can we do it depending on our purpose?
Please help me .. I don't know the use of the delay :(
You could just replace the "50" in the first delay with the ultrasonic distance variable, I'll explain a bit more below.

Let's assume your ultrasonic sensor outputs an analogue value between 0V and 5V. The output is connected to analog pin A0. Then you just set pinMode(A0, INPUT) and analogRead the value from the pin and store it in the variable X or whatever. Then you just use the variable X as a delay and it will do what you want. The higher the value gets the longer it will vibrate. Something like this:

[...]
val = analogRead(A0);
digitalWrite(5, HIGH);
delay(val);
digitalWrite(5, LOW);
[...]


Second option, if you only wish to turn it on if it is above or under a certain limit, would be using an if condition. 

For example: 

if(variable &gt; 50){
  // if the variable named variable is over 50 this code is going to be executed
} else {
  // If the variable is below 50 this is executed
}


If you are unfamiliar with if conditions then I suggest reading this too.