Speed Sensor (Velocity Km/H) Using Ultrasonic Sensor (Ping)

for the example..i put ping ultrasonic sensor in front of my car..than, find the different speed between my car and the front car..

"If you can see my Ping, you're driving too close" :slight_smile:

OK, so show us your code so far.

Well assuming you have the coding done to measure the distance in the first place, which is well documented if I recall from looking at Ping)) some time ago....

In it's simplest form you just need to take two distance readings noting the time using millis() or micros() at which you took the readings. Then you calculate the speed as distance / time as:

Speed = (FirstDistance - SecondDistance) / (SecondTime - FirstTime)

If FirstDistance < SecondDistance the distance is increasing and the car in front is moving away; that would give a -ve speed.

Something like that, anyway 8)

AWOL:

for the example..i put ping ultrasonic sensor in front of my car..than, find the different speed between my car and the front car..

"If you can see my Ping, you're driving too close" :slight_smile:

OK, so show us your code so far.

yes..u are too close if u see my ping sensor..hehehe...
i dont know how to start the coding for speed sensor..before this i just 'play' with range..sens and show the range between your car, and front car..

#include <LiquidCrystal.h>   // lcd
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //lcd

const int pingPin = 7; //sensor

const int redPin = 6; //led
const int greenPin = 5; //led
const int bluePin = 4; //led

void setup() {
  // initialize serial communication:
 
  pinMode (redPin, OUTPUT);
  pinMode (greenPin, OUTPUT);
  pinMode (bluePin, OUTPUT);
  
  
  Serial.begin(9600);
  
  
  lcd.begin(16, 2);
  lcd.print("Distance (cm):");
}


void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  lcd.setCursor(0, 1);
  
  long duration, inches,cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(20);
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  


  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  // convert the time into a distance

  if (inches > 30) {
    digitalWrite(greenPin, HIGH);  // green LED
    digitalWrite(redPin, LOW);
    digitalWrite(bluePin, LOW);
  }
  else if (inches <= 30 && inches > 12) {
    digitalWrite(greenPin, HIGH);
    digitalWrite(redPin, HIGH);    // orange LED
    digitalWrite(bluePin, LOW);
  }
  else if (inches <= 12 && inches > 6) {
    digitalWrite(redPin, LOW);    // red LED
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, HIGH);
  }
  else {
    digitalWrite(redPin, HIGH);    // purple LED
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, HIGH);
  }


  lcd.print(cm);
  lcd.print("cm");
  lcd.print("");
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}


long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Moderator edit: CODE TAGS

JimboZA:
Well assuming you have the coding done to measure the distance in the first place, which is well documented if I recall from looking at Ping)) some time ago....

In it's simplest form you just need to take two distance readings noting the time using millis() or micros() at which you took the readings. Then you calculate the speed as distance / time as:

Speed = (FirstDistance - SecondDistance) / (SecondTime - FirstTime)

If FirstDistance < SecondDistance the distance is increasing and the car in front is moving away; that would give a -ve speed.

Something like that, anyway 8)

thank you for your idea.. if you can see from my code, where i can get the value of secondtime and first time?

where i can get the value of secondtime and first time?

Here

Every time through loop() you are measuring the ping duration and then calculating a distance...

Where you read the duration, you could add a line like time = millis so that would record when in the day (or in fact since the sketch started running) that duration was captured.

You'll need to do something to move each current time reading into an oldtime variable (at the top of loop() ?) so that you can subtract one from the other to give the time between readings. Similarly, you'll need to move the current distance to an olddistance variable so you can subtract them.. as it stands now, values like cm and so on are overwritten each time through loop() so you need to have a means of storing the previous one before the new one comes along.

okay friends..i will try first...

matt121187:
okay friends..i will try first...

Awesome 8) .... and my advice is that before you dive into code, draw a flowchart of what it is you want to do. Get the logic clear on paper and in your head before you get into the nitty-gritties of coding.

JimboZA:

matt121187:
okay friends..i will try first...

Awesome 8) .... and my advice is that before you dive into code, draw a flowchart of what it is you want to do. Get the logic clear on paper and in your head before you get into the nitty-gritties of coding.

thanks for your advice..i just draw the flowchart..but i bit confusing to get the value of FirstDistance ,SecondDistance, SecondTime, FirstTime =(

i'm really blur...cant solve this problem.... =(

In that case I suggest you pick a simpler problem.

cant solve this problem.

Can't or won't? What have you tried?

how to hold the previous distance?

this is my coding right now..but the previous state still show the current distance

const int pingPin = 7;
int start = 0;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm, currentState, previousState;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);
  
  currentState = cm;
  
  previousState = currentState + start ;
  previousState = currentState;
  
  Serial.print(previousState);
  Serial.print("cm, ");
  Serial.print(currentState);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}



long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Any variable declared within a function like "loop" is going to have limited use for remembering its value.
If you want a variable to maintain its value, declare it with global scope.

can u show me the example from my coding?

long duration, inches, cm, currentState, previousState;

AWOL:
Any variable declared within a function like "loop" is going to have limited use for remembering its value.
If you want a variable to maintain its value, declare it with global scope.

I thought declaring a variable static solves any 'memory' problem for variables declared within the scope of a function?

Lefty

These variables aren't declared static in the routine though

I thought declaring a variable static solves any 'memory' problem for variables declared within the scope of a function?

It does - I just suggested global scope because it is what most noobs are more comfortable with.