Attempting to code a distance sensor to detect speed like a Radar Gun.

I recently tried to find an object’s speed with a distance sensor, while I could use a timer and use the distance formula, I decided to try to create a code that would automatically find the constant speed of an object.

Code:

int Brain_precision = 0;

float Variable1, Variable2;

// "when started" hat block
int whenStarted1() {
  Brain.Timer.reset();
  while (true) {
    if (Distance12.foundObject()) {
      Variable1 = Distance12.distance(mm);
      wait(0.25, seconds);
      Variable2 = Distance12.distance(mm);
      Brain.Screen.print(printToBrain_numberFormat(), static_cast<float>((Variable1 - Variable2) / (Brain.Timer.value())));
    }
    else {
      Brain.Timer.reset();
    }
  wait(20, msec);
  }
  return 0;
}


int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  whenStarted1();
}

Are there anyways I can improve this code.