Adding time(not delay) to arduino code

Hello! I am trying to implement time to determine the "crashtime" for the ultrasonic sensor. crashtime=distance/speed; or in my situation crashtime=(distance2-distance)/speed;
distance 2 should be distance after 10 miliseconds, like a delay but, I dont know how to write that in code.Project: A smart rc car that brakes if the crashtime is less than 0.7. I want the rc car to stop brakeing after i release the acceleration on the remote and go back to normal which means:relay is low and relay2 is high. This is what I have done so far:

int relay1=12;
int relay2=13;
int trigPin=2;
int echoPin=3;
double speed;
int crashtime;
long duration;
int distance;
int distance2;
void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
 digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.0343/ 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  distance2=
  crashtime =distance2 * (distance / time);
  if(crashtime<=0.7 || crashtime == 0){
    digitalWrite(relay, HIGH);
    delay(1);
    digitalWrite(relay2, LOW);
  }
  else{
   digitalWrite(relay, LOW);
    delay(1);
    digitalWrite(relay2, HIGH); 
  }
}

Take a look at the documentation for pulseIn
You may be pleasantly surprised.

Lazy.
Pointless.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.