We’re rewarding the question askers & reputations are being recalculated! Read more.
4 Added 7 to sensorDistance because distance between sensors is 7 cm
source | link
const int trigPins[2] = {3, 5}; //trig1 and trig2
const int echoPins[2] = {4, 6}; //echo1 and echo2

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

void setup() {
  pinMode(trigPins[0], OUTPUT); //set trig pin 1 as output
  pinMode(echoPins[0], INPUT);  //set echo pin 1 as input
  pinMode(trigPins[1], OUTPUT); //set trig pin 2 as output
  pinMode(echoPins[1], 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;
  }

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

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

void setup() {
  pinMode(trigPins[0], OUTPUT); //set trig pin 1 as output
  pinMode(echoPins[0], INPUT);  //set echo pin 1 as input
  pinMode(trigPins[1], OUTPUT); //set trig pin 2 as output
  pinMode(echoPins[1], 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;
  }

}
const int trigPins[2] = {3, 5}; //trig1 and trig2
const int echoPins[2] = {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[0], OUTPUT); //set trig pin 1 as output
  pinMode(echoPins[0], INPUT);  //set echo pin 1 as input
  pinMode(trigPins[1], OUTPUT); //set trig pin 2 as output
  pinMode(echoPins[1], 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;
  }

}
3 added 2 characters in body
source | link
const int trigPins[2] = {3, 5}; //trig1 and trig2
const int echoPins[2] = {4, 6}; //echo1 and echo2

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

void setup() {
  pinMode(trigPins[0], OUTPUT); //set trig pin 1 as output
  pinMode(echoPins[0], INPUT);  //set echo pin 1 as input
  pinMode(trigPins[1], OUTPUT); //set trig pin 2 as output
  pinMode(echoPins[1], 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;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;
  }

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

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

void setup() {
  pinMode(trigPins[0], OUTPUT); //set trig pin 1 as output
  pinMode(echoPins[0], INPUT);  //set echo pin 1 as input
  pinMode(trigPins[1], OUTPUT); //set trig pin 2 as output
  pinMode(echoPins[1], 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;        //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;
  }

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

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

void setup() {
  pinMode(trigPins[0], OUTPUT); //set trig pin 1 as output
  pinMode(echoPins[0], INPUT);  //set echo pin 1 as input
  pinMode(trigPins[1], OUTPUT); //set trig pin 2 as output
  pinMode(echoPins[1], 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;
  }

}
2 added 4 characters in body
source | link
const int trigPins[2] = {3, 5}; //trig1 and trig2
const int echoPins[2] = {4, 6}; //echo1 and echo2

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

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

  Serial.begin(9600);
}

void loop() {

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

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

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

    intunsigned long timeMillis = endMillis - currentMillis; //get duration in ms
    intfloat timeSeconds = timeMillis / 1000;        //because 1000ms = 1s

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

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

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

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

  Serial.begin(9600);
}

void loop() {

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

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

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

    int timeMillis = endMillis - currentMillis; //get duration in ms
    int timeSeconds = timeMillis / 1000;        //because 1000ms = 1s

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

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

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

void setup() {
  pinMode(trigPins[0], OUTPUT); //set trig pin 1 as output
  pinMode(echoPins[0], INPUT);  //set echo pin 1 as input
  pinMode(trigPins[1], OUTPUT); //set trig pin 2 as output
  pinMode(echoPins[1], 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;        //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;
  }

}
1
source | link