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);
}
}
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.
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;
}
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.
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);
}