Cronometer with ultrasonic sensor

I'm trying to program a cronometer that measures the time from one point to another by using ultrasonic sensors. The point cronometer is that when the first sensor stops detecting an object, the cronometer starts, and when the second sensor detects this object, the cronometer stops and the time is displayed on a LCD. However, I can't seem to figure out the programming to make the cronometer start and stop with the sensors.

Any ideas?

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Configura los pines para el LCD
const int triggerPin1 = 6; // Pin del primer sensor ultrasónico - Trigger
const int echoPin1 = 7;    // Pin del primer sensor ultrasónico - Echo
const int triggerPin2 = 8; // Pin del segundo sensor ultrasónico - Trigger
const int echoPin2 = 9;    // Pin del segundo sensor ultrasónico - Echo

unsigned long startTime = 0;
unsigned long endTime = 0;
boolean timing = false;

void setup() {
  lcd.begin(16, 2); // Inicializa el LCD con 16 columnas y 2 filas
  pinMode(triggerPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(triggerPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Configura el botón con resistencia de pull-up
  lcd.setCursor(0, 0); // Coordenada para pantalla LCD
  lcd.print("Tiro parabolico"); //Muestra frase en pantalla LCD
  lcd.setCursor(0,1); // Coordenada para pantalla LCD
  lcd.print("Tiempo: "); // Mueustra la palabra en la pantalla LCD
}

void loop() {
  if (!timing) {
    digitalWrite(triggerPin1, LOW);
    delayMicroseconds(2);
    digitalWrite(triggerPin1, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin1, LOW);
    unsigned long duration = pulseIn(echoPin1, HIGH);
    if (duration > 0) {
      startTime = millis();
      timing = true;
    }
  } else {
    digitalWrite(triggerPin2, LOW);
    delayMicroseconds(2);
    digitalWrite(triggerPin2, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin2, LOW);
    unsigned long duration = pulseIn(echoPin2, HIGH);
    if (duration > 0) {
      endTime = millis();
      timing = false;
      unsigned long elapsedTime = endTime - startTime;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Tiempo: ");
      lcd.print(elapsedTime);
      lcd.print(" ms");
      delay(2000);
    }
  }
}

MY suggestion is to make duration a global variable, rather than creating a new variable, or two new variables every time loop() loops. See if that helps.

1 Like

Yes, I modified that part and made the duration variable global, but it's still doing the same

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Configura los pines para el LCD
const int triggerPin1 = 6; // Pin del primer sensor ultrasónico - Trigger
const int echoPin1 = 7;    // Pin del primer sensor ultrasónico - Echo
const int triggerPin2 = 8; // Pin del segundo sensor ultrasónico - Trigger
const int echoPin2 = 9;    // Pin del segundo sensor ultrasónico - Echo

unsigned long startTime = 0;
unsigned long endTime = 0;
boolean timing = false;
unsigned long duration;

void setup() {
  lcd.begin(16, 2); // Inicializa el LCD con 16 columnas y 2 filas
  pinMode(triggerPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(triggerPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  lcd.setCursor(0, 0); // Coordenada para pantalla LCD
  lcd.print("Tiro parabolico"); //Muestra frase en pantalla LCD
  lcd.setCursor(0,1); // Coordenada para pantalla LCD
  lcd.print("Tiempo: "); // Mueustra la palabra en la pantalla LCD
}

void loop() {
  if (!timing) {
    digitalWrite(triggerPin1, LOW);
    delayMicroseconds(4);
    digitalWrite(triggerPin1, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin1, LOW);
    duration = pulseIn(echoPin1, HIGH);
    if (duration > 0) {
      startTime = millis();
      timing = true;
    }
  } else {
    digitalWrite(triggerPin2, LOW);
    delayMicroseconds(4);
    digitalWrite(triggerPin2, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin2, LOW);
    duration = pulseIn(echoPin2, HIGH);
    if (duration > 0) {
      endTime = millis();
      timing = false;
      unsigned long elapsedTime = endTime/1000;
      lcd.setCursor(8, 1);
      lcd.print(elapsedTime);
      lcd.print(" s");
      delay(2000);
    }
  }
}

Yes, it is purely for academic purposes. It's to time how long it takes for an object to reach the second sensor, so when it is detected by the second sensor, the object stays there, so the second sensor should be able to detect it and stop the time.

The object is a little ball of around 2 cm of diameter and it's really close to the first sensor. The cronometer's going to be used to time a parabolic shot, so the distance is not that big. The ball is placed on a spring so that the force that is used is always the same and it land on a support that's just below the second sensor so that it detects the ball that lands there,

And of course! Here's a drawing, I hope it helps you understand better.

1 Like

Of course, there is ONE parameter of the pulseln() that you are ignoring and may have a very large effect on your experiment. That is the timeout period and the returning of zero for the time if you miss the ball.

You should use light barriers that get interrupted by the ball.

most reflective optical sensors have a too small distance. This distance is in the range of 0,x mm up to 5 mm.

You should use a light-barrier where lightsource and light-receiver are separated.

Lightbarriers will switch instantly with no timeout because the speed of light is
3*10^8 m/s and the switching time of such a lightbarrier is in the range of less than a millisecond.

If both lightbarriers are of the same type the delay time between lightbeam interrupted and signal-change will be almost the same = pretty precise time measuring.

best regards Stefan

I have deleted your other cross-post @landa_8.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

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