How to calculate the velocity by using ultrasonic sensor

I'm using Ultrasonic-HC-SR04 , which has the many examples to calculate the distance, I want to calculate the velocity which is = ( distance [2] - distance [1] ) / ( time[2] - time[1] )

the distance is changing depand on my delay (..);

1- How can I take these values of distance and time frequently ??
2- if there is an array with variable (i) how can I make it ?

I think it should be writen as :

V = (d (i+1) - d(i) ) / ( time(i+1) - time(i) ) ;

#include <HCSR04.h>

UltraSonicDistanceSensor distanceSensor(13, 12);  



void setup () {
    Serial.begin(9600);  

}

void loop () {

    double Dis = distanceSensor.measureDistanceCm();
    Serial.println(Dis);
}

3ziz3:
the distance is changing depand on my delay (..);

I don't see any delay in your code. Please post your full code.

1- How can I take these values of distance and time frequently ??

Take two distance readings, separated by a known time interval (the delay() function is fine for this simple experiment), and compute the velocity as you described.

The velocity won't be correct unless the object is moving directly toward or directly away from the sensor.

As jremington notes, there will be cosine errors if the angle of motion is anything but directly to/away from the sensor.

You can try this. It compiles, don't know if it works:

#include <HCSR04.h>

UltraSonicDistanceSensor distanceSensor( 13, 12 );  

void setup( void ) 
{
    Serial.begin(9600);  

}//setup

#define READ_INIT           0
#define TAKE_MEASUREMENT    1
//
void loop( void ) 
{
    float
        fVelocity;
    static float
        fDist1, 
        fDist2;
    static byte
        stateVelocity = READ_INIT;
    static unsigned long
        timeVelocity = 0;
    unsigned long
        timeNow;

    
    switch( stateVelocity )
    {
        case    READ_INIT:
            fDist1 = distanceSensor.measureDistanceCm();
            timeNow = micros();
            //wait until we have an error-free reading before proceding to take measurements
            if( fDist1 != -1.0 )
            {
                timeVelocity = timeNow;
                stateVelocity = TAKE_MEASUREMENT;
                
            }//if

        break;

        case    TAKE_MEASUREMENT:
            if( timeNow - timeVelocity < 500000 )
                return;
                
            fDist2 = distanceSensor.measureDistanceCm();
            timeNow = micros();
            timeVelocity = timeNow;

            //if measurement is -1.0 there was an error (out of range, too close etc)
            //if so, go back to READ_INIT to sync up again
            if( fDist2 == -1.0 )
                stateVelocity = READ_INIT;
            else
            {
                //measurements are taken at 1/2-sec intervals
                //change in distance divided by that time is speed/velocity
                //result may be negative if object is receding
                fVelocity = (fDist1 - fDist2) / 0.5;
                fDist1 = fDist2;
                
                Serial.print( "V = " ); Serial.print( fVelocity, 1 ); Serial.println( "cm/s" );
                                      
            }//else
            
        break;
        
    }//switch

}//loop