# Record Ping time using ultrasonic sensors and calculate speed

I'm trying to find the speed of remote control toy car passing by two ultrasonic sensors. I need help on my code and suggestion on max speed car should go to record time. Opposite direction not accounted for and ideally the car would pass by both sensors without stopping. I know laser reading is better for this simulation but I need it for a presentation in two weeks.

Here are the steps I was recommended to use:

1. Get the duration of pulse for sensor 1 and convert to distance
2. if the distance the object is within the desired threshold (you detected something) 2a. record the system time at which object was detected 2b. start a loop to for sensor 2
3. Within the sensor 2 loop keep getting the duration/distance until an object is detected or too much time has passed
4. If an object is detected record the system time
5. Subtract the two system times (from sensor 1 and 2) and use that to determine object speed

``````#define sensor_distance .111 //distance between two sensors in meters
const int trigPin1 = 3;
const int echoPin1 = 4;
const int trigPin2 = 5;
const int echoPin2 = 6;

double first_time = 0;
double second_time = 0;
double final_time = 0;

double vehicle_speed = 0;

void setup() {

pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);

Serial.begin(9600);
}

void loop() {

double ping_time(int trig1, int echo1, double first_time);
double ping_time(int trig2, int echo2, double second_time);

final_time = second_time - first_time;
vehicle_speed = sensor_distance/final_time;
}

double ping_time(int trig, int echo, double record_time) //function used to record time.
{
long duration, distance;

digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

duration = pulseIn(echo, HIGH);
distance = duration * 0.034 / 2;

if (distance < 35) {
Serial.print(distance);
Serial.println("  cm");
record_time = millis();
record_time = record_time/100; //Divided by 100 to get time in seconds

return record_time;

}
if (distance > 35) {
Serial.println("OUT OF RANGE");
}
delay(20);
}
``````

You can try this, but I don't know if this gonna works or not since it is "imaginary-based"

``````const int trigPin1 = 3;
const int echoPin1 = 4;
const int trigPin2 = 5;
const int echoPin2 = 6;

void setup() {

pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);

Serial.begin(9600);
}

void loop()
{
if (ultraSonicdetect())
{
while (ultraSonicdetect(trigPin1,echoPin1)); // wait the car pass ultrasonic 1
first_time = millis();
while (!ultraSonicdetect(trigPin2,echoPin2)); //wait the car approach ultrasonic 2
second_time = millis();

//here you can calculate the speed, time needed to travel from ultrasonic 1 to ultrasonic 2
//can be calculated from
//(speed = second_time - first_time) / sensor range
}
}

bool ultraSonicdetect(int trig, int echo) {
long duration;
float distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(60);
digitalWrite(trigPin, HIGH);
delayMicroseconds(60);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.034) / 2;
if (distance<35) return true; else return false;
}
``````

In THEORY, this code should work. If there are any mistakes, please notify me and not hate on me. I did not have the equipment at hands to test the code in real life.

Chears!

``````const int trigPins = {3, 5}; //trig1 and trig2
const int echoPins = {4, 6}; //echo1 and echo2

const int sensorDistance = 7;   //7 is the cm distance at which the sensors distant from eachother in CM

void setup() {
pinMode(trigPins, OUTPUT); //set trig pin 1 as output
pinMode(echoPins, INPUT);  //set echo pin 1 as input
pinMode(trigPins, OUTPUT); //set trig pin 2 as output
pinMode(echoPins, INPUT);  //set echo pin 2 as input

Serial.begin(9600);
}

void loop() {

if (getDistance(0) == true)                   //if the car passed the first sensor
{
unsigned long currentMillis = millis();               //time when the passed the first sensor

while (getDistance(1) == false);            //wating car to pass the second sensor

unsigned long endMillis = millis();                   //time at which the car passed the second sensor

unsigned long timeMillis = endMillis - currentMillis; //get duration in ms
float timeSeconds = timeMillis / 1000.0;        //because 1000ms = 1s

float s = sensorDistance / timeSeconds;       //speed = distance / time

Serial.print(s);
Serial.print(" cm/s");
Serial.println();
}
}

bool getDistance(int sensor)
{
//max range is 200cm, if further, it will go a little wild

digitalWrite(trigPins[sensor], LOW); //set the trigger pin to low
delayMicroseconds(2); //wait for 2 microseconds
digitalWrite(trigPins[sensor], HIGH); //set the trigger pin to high
delayMicroseconds(10); //wait for 10 microseconds
digitalWrite(trigPins[sensor], LOW); //set the trigger pin to low

long duration = pulseIn(echoPins[sensor], HIGH); //read the echo pin and calculate the duration in microseconds

long distance = duration * 0.034 / 2;

if (distance < 50) //50 is a reasonable distance other wise it might be innacurate
{
return true;
}

else
{
return false;
}

}
``````
• So my output has just been 0 cm/s so far. I'm still trying to figure that out. Also, why is your sensor distance zero? Also, how can getDistance() determine which sensor you are using? – flyer3110 Nov 9 '16 at 19:35
• I included an input for the sensor. I dont call `getDistance()`, I have to say `getDistance(sensor 0 or 1)`. 0 is the first sensor and 1 is the second. The reason I do this is to use an array for the pins thus saying which sensor I want. – Dat Ha Nov 9 '16 at 19:54
• How far are your sensors from each other?? – Dat Ha Nov 9 '16 at 20:04
• Replace `timeMillis / 1000` with either `timeMillis / 1000.0` or `timeMillis / 1e3`. – Edgar Bonet Nov 9 '16 at 20:27
• the sensors are 7 cm apart from each other. I input 7 for sensorDistance. Now highest reading is 7 cm/s. – flyer3110 Nov 9 '16 at 22:27