Go Down

Topic: Speed Sensor (Velocity Km/H) Using Ultrasonic Sensor (Ping) (Read 12372 times)previous topic - next topic

matt121187

Dec 30, 2012, 11:28 amLast Edit: Dec 30, 2012, 03:47 pm by matt121187 Reason: 1
hello everybody..
can someone help me on this problem?

i have arduino mega and ping ultrasonic sensor, but the coding is problem for me. hopefully someone can help me......  =(

AWOL

#1
Dec 30, 2012, 12:05 pmLast Edit: Dec 30, 2012, 12:10 pm by AWOL Reason: 1
What's the problem, and why are you SHOUTING?

1kmh-1 is 0.277ms-1

The Ping can take maybe 20 readings a second, out to a sensible range of what? Four metres?

Do some simple sums.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

matt121187

#2
Dec 30, 2012, 03:45 pmLast Edit: Dec 30, 2012, 03:47 pm by matt121187 Reason: 1
can u help me for the coding part?i really blur to do the speed sensor coding...

AWOL

Can you stop and think for a moment, and tell us exactly what you're trying to do?
Also think about the limitations I described in my other post.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

retrolefty

Your ping sensor is a simple time of flight sensor where the variable time represents the variable distance to a fixed object. To turn that into a speed sensor would require that you sense the change of distance of a return pulse of a moving object over a specific time period, so it's a second order value that would be based on the integration of the primary time of flight measurement that the sensor provides you. So the solution is math based.

Lefty

matt121187

Can you stop and think for a moment, and tell us exactly what you're trying to do?
Also think about the limitations I described in my other post.

sorry..
i try to do the speed sensor between two car to find the crash impact..

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..

AWOL

Quote
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"

OK, so show us your code so far.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

matt121187

#8
Dec 30, 2012, 04:22 pmLast Edit: Dec 30, 2012, 04:35 pm by AWOL Reason: 1

Quote
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"

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..

Code: [Select]
`#include <LiquidCrystal.h>   // lcdLiquidCrystal lcd(12, 11, 5, 4, 3, 2); //lcdconst int pingPin = 7; //sensorconst int redPin = 6; //ledconst int greenPin = 5; //ledconst int bluePin = 4; //ledvoid 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

matt121187

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

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

AWOL

Quote
where i can get the value of secondtime and first time?
Here
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

JimboZA

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.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

matt121187

okay friends..i will try first...

JimboZA

okay friends..i will try first...

Awesome  .... 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.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)

matt121187

okay friends..i will try first...

Awesome  .... 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 =(

Go Up

Please enter a valid email to subscribe