Calculating speed problems

So I have school project that I'm making and the idea is that when a car passes too fast infront of the sensor an alarm gets activated.Since I'm new at the arduino and programming world I used an tutorial to make that work.The tutorial's link;https://youtu.be/Ezc-NSU7cKU
The code has some errors and I also don't know where to put the buzzer's and led's part of the code.
Sensor's code;

const int trigPin = 5;
const int echoPin = 6;

long duration;
int FirstDistance;
int SecondDistance;
double speed;
int distance;
float Time = 1.0;
float delayedtime = 1000 * Time;

















void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  GetSpeed();
}
float GetDistance()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2;
  Serial.println(distance);

  return distance;

  void GetSpeed() {

    FirstDistance = GetDistance();
    delay(delayedtime);

    SecondDistance = GetDistance();

    speed = (FirstDistance - SecondDistance) / Time;

    Serial.print("the speed (cm/s) is :");
    Serial.println(speed);
  }




}

The led's and buzzer code;

#define led 2
#define buzzer 3

void setup() {
pinMode(led,OUTPUT);
pinMode(buzzer,OUTPUT);

void loop() {
if (speed > 3){
digitalWrite(led,HIGH);
tone(buzzer,3000);
delay(3000);
digitalWrite(led,LOW);
noTone(buzzer);



}
if (speed <= 3){
digitalWrite(led,LOW);
noTone(buzzer);
}
}

Here is my photo and my hand drawn diagram.

I suggest that you let getSpeed() return the speed (in the same way as you return the distance in getDistance().

Than you can test (in loop()) if a certain speed is exceeded and react on that.

So it should be a matter of adding getSpeed() etc to the second code.

well done for posting a "schematic"
Your measurement of the speed will only work if the car travels in a particular path.
The sensor measures the distance.
The measured speed is the rate at which that changes -
suppose the object is travelling towards or away from the sensor at 10kph
the measured speed will e 10kph.
suppose the car is travelling at 10kph on a circle so the distance from the sensor is always the same - the measured speed will be zero.

Thanks for the explanation but can you make an example?

If the sensor can't calculate the speed, then which sensor should I buy because it must be done by the 15th on December.Thanks btw.

The sensor code has a misplaced }

1 Like

The measured speed is dS /dt.
If the distance between sensor and car is always the same - if its travelling at right angles to the sensor - dS/dt = 0
if its travelling directly towards or away from the sensor at speed V then dS/dt = +/- V
If you can constrain the vehicle to travel directly towards the sensor (maybe with the sensor above a track like a motorway gantry) then you can get sensible readings.
its all geometry.

Hi!I am making a school project and I have to calculate the speed of a toy car running infront of it.The idea is that when the car's speed is less than x seconds the led turns on and the buzzer beeps.I used an tutorial for the calculating speed part but the other code is mine;https://youtu.be/Ezc-NSU7cKU.Idk if my arduino UNO is brocken or my HCSR04 or my code has errors cause nothing shows up on the Serial monitor the led won't turn on , the buzzer does nothing .It's like the sensor doesn't interact with the enviroment at all. I have 2 of them but I've tried both of them and nothing changes.Can a SRF05(I dont know what type of sensor that is or what does it do).This is the library I'm using;https://github.com/gamegine/HCSR04-ultrasonic-sensor-lib
Do I need to download a different library?IWhatever it is I need it FAST!!!
Here's my code;

const int trigPin = 5;
const int echoPin = 6;
const int  buzzer = 3;
const int led = 2;

long duration;
int FirstDistance;
int SecondDistance;
double speed;
int distance;
float Time = 1.0;
float delayedtime = 1000 * Time;




void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
pinMode(buzzer,OUTPUT);
pinMode(led,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
 if(speed > 3){
  digitalWrite(led,HIGH);
  tone(buzzer,3000);
  delay(2000);
  digitalWrite(led,LOW);
  noTone(buzzer);
 
 }
}
float GetDistance(){

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2;
  
  Serial.print("the distance is");
  Serial.println(distance);

  return distance;
}

  void GetSpeed() {

    FirstDistance = GetDistance();
    delay(delayedtime);

    SecondDistance = GetDistance();

    speed = (FirstDistance - SecondDistance) / Time;

    Serial.print("the speed (cm/s) is :");
    Serial.println(speed);
    return speed;
  }

Here's a decent hand drawn schematic;


Thanks,
Kat.

On the schematic, echo goes to pin 5. In the code echo goes to pin 6.

This library doesn´t seem to be #included in your code.

I included it in the code but I think there is code missing.I dont know if I'm right but the code doesn't mention the sensor at all.

#include<HCSR04.h>

const int trigPin = 5;
const int echoPin = 6;
const int  buzzer = 3;
const int led = 2;

long duration;
int FirstDistance;
int SecondDistance;
double speed;
int distance;
float Time = 1.0;
float delayedtime = 1000 * Time;

















void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
pinMode(buzzer,OUTPUT);
pinMode(led,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
 if(speed > 3){
  digitalWrite(led,HIGH);
  tone(buzzer,3000);
  delay(2000);
  digitalWrite(led,LOW);
  noTone(buzzer);
 
 }
}
float GetDistance(){

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2;
  
  Serial.print("the distance is");
  Serial.println(distance);

  return distance;
}

  void GetSpeed() {

    FirstDistance = GetDistance();
    delay(delayedtime);

    SecondDistance = GetDistance();

    speed = (FirstDistance - SecondDistance) / Time;

    Serial.print("the speed (cm/s) is :");
    Serial.println(speed);
    return speed;
  }

Not to be too deflating, but in actuality that is not really decent. It's a nice first try if you are a beginner...

Hi,
Look at an example in the HCSR04 library and use that for a start and get reliable ultrasonic readings BEFORE thinking about speed calculations.

In other words PROVE that you can communicate with your hardware.

Then get your speed calculation working.

I'm surprised your teacher has not taught you how to write your code in stages.

Tom.. :smiley: :+1: :coffee: :australia:
PS. Not a bad schematic for first time, you will improve with practice.

2 Likes

With my teacher I haven't gotten at that level yet.I just used an youtube tutorial.

Shouldn't you call the "GetSpeed()" function before you use the 'speed' value?

You get a compile warning because you are trying to return a value from a function that doesn't return. value:

sketch_nov29a.ino: In function 'void GetSpeed()':
sketch_nov29a.ino:71:10: warning: return-statement with a value, in function returning 'void' [-fpermissive]
   return speed;
          ^~~~~

The code would be a little easier to follow if you used the return value instead of a global.

const int trigPin = 5;
const int echoPin = 6;
const int  buzzer = 3;
const int led = 2;

unsigned long duration;
float FirstDistance;
float SecondDistance;

float distance;
const float Time = 1.0;
const float delayedtime = 1000 * Time;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop()
{
  // put your main code here, to run repeatedly:
  if (GetSpeed() > 3.0)
  {
    digitalWrite(led, HIGH);
    tone(buzzer, 3000);
    delay(2000);
    digitalWrite(led, LOW);
    noTone(buzzer);
  }
}

float GetDistance()
{

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2;

  Serial.print("the distance is");
  Serial.println(distance);

  return distance;
}

float GetSpeed()
{

  FirstDistance = GetDistance();
  delay(delayedtime);
  SecondDistance = GetDistance();

  float speed = (FirstDistance - SecondDistance) / Time;

  Serial.print("the speed (cm/s) is :");
  Serial.println(speed);

  return speed;
}

I am with @TomGeorge, develope code in stages. Not only get each bit to work before you move on, but make sure that you understand why and how it works. I have been coding for 50+ years and still write no more than a few lines at a time before i verify the code. If there is a problem, it is much easier to locate the problem.

1 Like

i followed your instructions but I'm stuck.This is my code so far;

#include<HCSR04.h>

const int trigPin = 5;
const int echoPin = 6;
const int  buzzer = 3;
const int led = 2;

long duration;
int FirstDistance;
int SecondDistance;
double speed;
int distance;
float Time = 1.0;
float delayedtime = 1000 * Time;



HCSR04 sensor(5,6);













void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
pinMode(buzzer,OUTPUT);
pinMode(led,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
 float GetSpeed();
 Serial.println(sensor.dist() );
 delay(60);
 if( speed > 3){
  digitalWrite(led,HIGH);
  tone(buzzer,3000);
  delay(2000);
  digitalWrite(led,LOW);
  noTone(buzzer);
 
 }
}
float GetDistance(){

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2;
  
  Serial.print("the distance is");
  Serial.println(distance);

  return distance;
}

  void GetSpeed() {

    FirstDistance = GetDistance();
    delay(delayedtime);

    SecondDistance = GetDistance();

    speed = (FirstDistance - SecondDistance) / Time;

    Serial.print("the speed (cm/s) is :");
    Serial.println(speed);
    return speed;
  }

The sensor calculates the distance because of the .dist, I've tried writing .speed but an error pops up that says that the library does not include anything called speed!What should I do?

That's not a call to 'GetSpeed()'. That's a declaration. To call 'GetSpeed()' you leave off the return type:

    GetSpeed();

Your 'GetDistance()' is not using 'sensor'. See the examples from the HCSR04 library for how to get the distance. Just replace all of 'GetDistance()' with 'sensor.measureDistanceCm()'

Hi,
I think you are using the not using the library at all.

Try this code, you may have to swap the 5 and 6 around in this code to make it usable with your project.

UltraSonicDistanceSensor distanceSensor(5, 6);

This is the example code, see how easy it is to get a distance reading.

#include <HCSR04.h>

UltraSonicDistanceSensor distanceSensor(5, 6);  // Initialize sensor that uses digital pins 13 and 12.

void setup () {
    Serial.begin(9600);  // We initialize serial connection so that we could print values from sensor.
}

void loop () {
    // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
    Serial.println(distanceSensor.measureDistanceCm());
    delay(500);
}

Tom.. :smiley: :+1: :coffee: :australia:

#include <HCSR04.h>

const int led = 2;
const int buzzer = 3;
const int trigPin = 5;
const int echoPin = 6;

UltraSonicDistanceSensor sensor(trigPin, echoPin);

void setup()
{
  Serial.begin(9600);
  
  pinMode(buzzer, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop()
{
  float speed = GetSpeed();
  
  if (speed > 3.0)
  {
    digitalWrite(led, HIGH);
    tone(buzzer, 3000);
    delay(2000);
    digitalWrite(led, LOW);
    noTone(buzzer);
  }
}

float GetSpeed()
{
  float firstDistance = sensor.measureDistanceCm();
  delay(1000);
  float secondDistance = sensor.measureDistanceCm();

  float speed = firstDistance - secondDistance;

  Serial.print("the speed (cm/s) is :");
  Serial.println(speed);

  return speed;
}