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);
}
}