i want to calculate the time in between two events in arduino. i have two ultrasonic sensors when something passes through them when they are kept at a distance i need to start a timer when it pass the first sensor and i need to stop the timer when it passes through the second how is it possible and can any one share me a code to refer
Detect the passing thru the 1st detector, save the value of millis( ).
Detect the passing thru the 2nd detector, calculate millis( ) minus your saved value above.
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
Show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
Does your timer always start at zero?
lets say the MCU uses millis()
.
unsigned long startcountthingy= 0;
unsigned long endcountthingy=0;
voided ThingyDetectorFunction()
{
if DetectorThingyOneGetStartSignal
{
startcountthingy=millis();
endcountthingy=0;
}
if DetectorThingyTwoGetEndSignal
{
endcountthingy=millis()
unsigned long HowLongThingyDetected = startcountthingy - endcountthingy;
}
}
or something like that
I like that ![]()
But seeing as they're ultrasonic, which measure distance, you'll need to do the normal ultrasonic readings using say newPing() library. Compare the reading you get to your threshold distance which defines "when something passes through" and start the time when the something is close enough.
It gets more complicated than that: since the ultrasonic beam is a cone, the something will remain in the cone for a while as it passes from (say) left to right. Will you want the timing to start when it arrives at the left edge of the cone, or when it leaves the right side of the cone? (And ditto on the other sensor for when the timing should stop). You will need to look at state change detection to see how to start and stop on the edge of the signal.
And another wrinkle is that there's a minimum time required between pings- I can't recall what it is off hand, but the library will tell you: that may or may not be significant in your timing.
Might be better to look at a break beam approach rather? I posted code here the other day for a model rail level crossing; same idea, it just doesn't do any timing.
When I wrote and tested that code, I used two of these thingies:

added later: Which also uses state change detect, since you need to know when the beam becomes broken, not just is broken.
could I use this code for any sensor?
I have the same project you mentioned, and I need to know how and what I should get to build it it's like two sensors to detect the car passing at a specific time and distance and another sensor to see the color of the car if you could help, I would appreciate
If you have a similar project and questions, please start your own thread. Don't hijack another thread. It's unlikely to have exactly the same requirements and we need to know a lot more details about it. Please read this, and then open a new thread:
"You might see an existing topic related to what you want to ask and think about tagging your question on the end. Please don't do this."
A post was split to a new topic: Ultrasonic sensors problem please help
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.