Help me with this

I'm try to create a Speed Trap

using 2 ultrasonic sensor

the flow is:

from pointA to pointB, the distance is 1meter,

when the Car pass through pointA, the countdown start.

and end through pointB, which the countdown stop, and then the seconds collect will compute as Meter per seconds..

im just a beginner for this project

help for the codes thanks

Have you at least got the sensors hooked up and reading values which will tell you the car is in the beam?

almost

i’m using two HC-SR04

so i just need the speed of the car… totally rc Car

in m/s

the distance between of two sensor is 1meter

Well just use millis() to set a variable say startTime = millis() when the first beam is broken, and another endTime=millis() when the second one is broken and then endTime-startTime is how long it took to travel the 1m.

BUT.... loop() will run a zillion times in that short time and the car will probably still be breaking the first beam for many passes through loop. That means you might accidentally restart the timer. So you need a boolean flag called say "timingStarted" which is initialised false. When you check the first beam, only start the timer if if the flag is still false, and set it true when you do start it. So next time through loop() while the first beam is probably still broken, you don't restart the timer becasue the flag is true and you only start if it's false.

Then when the second beam is broken and you take the end time, set the flag back to false so the timer is ready for another vehicle.

Summary (not code.... just the steps)

  • initialise timingStarted as false
  • if first beam is broken AND timingStarted is false, set startTime = millis() and set timingStarted to true
    (so by implication, next time thru loop() if first beam is broken and timingStarted is true, do nothing.)
  • if timingstarted is true AND second beam broken, take the end time and set timingStarted to false
  • calculate the speed.
#include <NewPing.h>
unsigned long time1;
#define TRIGGER_PIN  4  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     2  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define TRIGGER_PIN2  8  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN2     7
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);
int threshold = 700;        //value below sensors are trigerd
float Speed;              // declaration of Speed variable
float timing;
unsigned long int calcTimeout = 0; // initialisation of timeout variable


void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
if (uS < threshold) {
   time1 = millis();
   startCalculation();
}
}

void startCalculation() {
   unsigned int uS2 = sonar2.ping();
  calcTimeout = millis(); // asign time to timeout variable 
  //we wait for trigger of sensor 2 to start calculation - otherwiuse timeout
  while (!(uS2 < threshold)) {
    uS2 = digitalRead(ECHO_PIN2);  
    if (millis() - calcTimeout > 5000) return;
  }
  timing = ((float) millis() - (float) time1) / 1000.0; //computes time in seconds
  Speed = 0.4 / timing;  //speed in m/s given a separation distance of 40 cm
  delay(100);
  Serial.print(Speed);
  Serial.print("\n");  
}

this is some of my search

i think this is not because one sensor only is working in this codes, and i think just ping is working in serial monitor

can you help in codes

thank you sir

You do realise that the sonar sends out a pulse periodically and waits for an echo. Also the pulse is a cone shape.

This means the sensor will trip at random points depending on the time of the pulse being sent.

For your system to work, you need a "tripwire", IE a laser light onto a sensor so that the beam is continuously lit and will always be broken at the same point of the race way.

You may be able to reduce the timing error by having a very short distance to thr car and high frequency for the pulses.

Weedpharma

sir can you give a sample codes
using ultrasonic sensor

just like the sample codes i post

thank you sir