Help with coding for sonar project

I have a project due soon for school. I was designing a shoe with a sonar sensor on it for blind people. my idea was to have a vibration motor in the shoe. I got the sonar to work, but I don't know how to program the motor. I want it to vibrate in long bursts when something is like 4 feet away, and progressively get shorter when approaching the object. Can I get some advice/help?

Don't use delay() to time motor activity, as that will interfere with collecting the sonar distance data.

Study this tutorial on how the Blink Without Delay example works, and use that basic approach to time the motor action.

If you run into specific problems, post your specific question and the code, using code tags, as described in "How to use this forum".

That was exactly what I was looking for. I need to be able to use the sonar data to change the motor intervals. How would I collect like a sample of the data or like an average of like 5 integers from the sonar?

You still haven't read the "How to use this forum" post. Do take a moment to do that.

Have you chosen a vibrating motor? From what I've seen, even the tiny ones draw too much current for a digital output's 20mA, so you would need a transistor to switch 5V from the Arduino 5V pin (assuming it can provide enough current, but it should, up in the 100s mA is available there.)

And a flyback diode...