Trying to measure speed of obstacle from HC SR04 ultrasonic sensor

Okay so I am doing this project where when my sensor detects an obstacle and a motor starts running. The thing is when the obstacle is stationary, the motor shouldn’t be running. So the need to gauge the speed of the obstacle is important too. Therefore I tried employing 2 measurements for obstacle 10 ms apart. The distances subtracted and divided by 10ms gives me a rough speed estimation. But the thing is my logic isn’t quite showing any results.
Any suggestions to refine the code would be appreciated.
Edit: By meaning not showing results, I meant that when obstacle is in range the led wont light up(I connected pin 9 and pin 11 to led to test the result. I have Mega board.)

#define trigPin 2
#define echoPin 3
#define led 11
#define led2 10
#define mot1 9
#define mot2 8
 long duration1, distance1, duration2, distance2,spd;
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(mot1, OUTPUT);
  pinMode(mot2, OUTPUT);
 
}

void loop() {
  
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration1 = pulseIn(echoPin, HIGH);
  distance1 = (duration1/2) / 29.4;
  delay(10);
   digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration2 = pulseIn(echoPin, HIGH);
  distance2 = (duration2/2) / 29.4;
  
  spd= (distance2-distance1)/0.01;
  if (distance2<100 || spd!=0 ) { 
    digitalWrite(led,HIGH); 
  digitalWrite(led2,LOW);
  digitalWrite(mot1,HIGH);
  digitalWrite(mot2,LOW);
 }
 
  if(distance2<100 || spd==0)
  {
    digitalWrite(led,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(mot1,LOW);
  digitalWrite(mot2,LOW);
  }
  if(distance2>100 || distance2<800)
  {
    digitalWrite(led,LOW); 
  digitalWrite(led2,HIGH);
  digitalWrite(mot1,LOW);
  digitalWrite(mot2,LOW);
  }
  if (distance2 >= 800 || distance2 <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance2);
    Serial.println(" cm");
  }
  delay(500);
}

collision_det.ino (1.43 KB)

bhargavbuddy:
Therefore I tried employing 2 measurements for obstacle 10 ms apart.

I recall reading that measurements should be minimum 30ms apart.

ps....don't post the same question twice

I tried the same thing with 1000ms apart just in case, but the results are the same.
Sorry, I just joined the forum and didn't notice the sensor section until later.