Chronometer with ultrasonic sensor and LCD display

Hello everyone !
I just started using Arduino so my knowledge on the subject is quite limited...

My project is to build a kind of chronometer to measure the time of a circuit loop.
I planned to use an ultrasonic sensor (HC-SR04) that could mesure the distance of an object, if this object is at a distance of 20cm or more of the sensor (beginning of the loop) it will initiate a chronometer, and when the object will come back in front of the sensor, at a distance of 20cm or less, the chronometer will stop and we read the time value on the LCD display.

I watched a lot of videos concerning ultrasonic sensors, LCD display ... but i really dont know how to put everything together that's why i need your help ...

I have really basic arduino stuff : Arduino UNO R3, HC-SR04 sensor, LCD display ...

first practice with every aspect of your arduino

  • the basics of the IDE and programming your Arduino. Get some LEDs blinking
  • understand how to deal with your LCD
  • understand how to deal with your distance sensor

then explore and understand how to deal with time and millis() (study blink without delay)

then try putting everything together, you might want to read about "final State machines", that will help you structure the code in a nice way.

Which, exact, LCD?

Using millis() for non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.

Pretty straight forward.
Write your first sketch to just start timing when the sensor detects anything and stop timing when the sensor again sees anything. This is a simple "toggle" event and only requires you to use millis()

Basic setup is shown here:
https://www.tutorialspoint.com/arduino/arduino_ultrasonic_sensor.htm

Now, you want to create a "state machine", that essentially becomes a compound sketch where events control specific lines of code.

You do your setup, from the earlier experiment you know the value the ultrasonic sensor will return for distance. In one "conditional" statement within loop() you wait for the first event:

If (sensorReading ... complete this for your specific start condition...
unsigned long startTime = millis();

Now we know when the measurement started. We just need to get the end time in a second conditional statement

If (sensorReading ... complete this for your specific end condition...
unsigned long endTime = millis();

The difference between endTime and startTime is your interval.
Since this all happens within the loop(), you just print the difference and the loop starts over.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.