Are you reffing to something like this?
Edit: Added more comments.
#define FPS_to_MPH 0.68182 // scale factor
#define DISTANCE 35.5625 // distance between the two sensors.
const byte SensorOnePin = 2;
const byte SensorTwoPin = 3;
byte SensorOneState;
byte SensorTwoState;
float Speed = 0, MPH = 0, tmp = 0;
boolean GotSecondSensor = false;
unsigned long SensorOne_timer = 0, SensorTwo_timer = 0;
void setup()
{
Serial.begin(115200);
pinMode(SensorOnePin, INPUT);
pinMode(SensorTwoPin, INPUT);
}
void loop()
{
// This is using normal buttons to detect car passing through one gate to another,
// this can be changed to use US sensors to replace the buttons. That part you will
// need to write yourself.
SensorOneState = digitalRead(SensorOnePin);
SensorTwoState = digitalRead(SensorTwoPin);
if(SensorOneState && GotSecondSensor == false) // car drives through first gate "sensor"
{
SensorOne_timer = millis(); // record time
GotSecondSensor = true; // lockout this IF statement and unlock the next IF statement
}
if(SensorTwoState && GotSecondSensor == true)
{
SensorTwo_timer = millis(); //record the time the car reaches the second gate
Speed = GetSpeed(SensorOne_timer, SensorTwo_timer, DISTANCE); // send the times and the distance into the function.
Serial.print("MPH: ");
Serial.println(Speed);
GotSecondSensor = false; // unlock first IF statement and lockout this IF statement.
}
}
float GetSpeed(unsigned long T1, unsigned long T2, float distance)
{
MPH = distance * (FPS_to_MPH); // "(FPS_to_MPH)" -> conversion factor, feet per second to miles per hour
tmp = (T2 - T1)/1000.00; // since the time we are using is in milliseconds, we need to convert milliseconds to seconds
Serial.print("Time (seconds): ");
Serial.println(tmp);
return (MPH / tmp); //return the speed of the car in MPH
}