Uniformly Accelerated Linear Motion simulator does not show results

Hi everyone,

Wanting to do an Uniformly accelerated linear motion simulator with a HC-SR04. I bulit a 90cm brigde where a ball will start falling. The HC-SR04 will detect the 7cm left wall (and time stamp) until the ball drops there so it will detect a 1 or 2 cm distance(2nd time stamp and red Led turns). Then it’s supposed to stop the timer and do the formula.

Or well, that’s what it’s supposed to do :confused: :confused:

The problem is that when I open the monitor just keps spamming:

“Se ha calculado la aceleración en base de 0.00 seg
con una distancia de 90.00 cm
y es inf m/s^2
Segundos trancurridos:
1” (Read the last part of the code for better understanding)

When I put something near it, just stop spamming. That without showing me any real data because, as I mentioned before, it is supposed to show me that message JUST after I put something near it (like the timer stops and shows me the results).

The code (which does not work)(ignore Spanish notes)

unsigned long tiempo = 0;
unsigned long tiempo2 = 0;
int segundos = 0;
int diferencia = 1000;
const double dist = 90;


#define Pecho 6
#define Ptrig 7
#define Micro 8
long duracion, distancia;

void setup() {
  Serial.begin(9600);
  pinMode(Pecho, INPUT);     // define el pin 6 como entrada (echo)
  pinMode(Ptrig, OUTPUT);    // define el pin 7 como salida  (triger)
  pinMode(13, 1);            // Define el pin 13 como salida
  pinMode(8, 1);

}

void loop() {
  tiempo = millis();
  if (tiempo >= (tiempo2 + diferencia))
  {
    tiempo2 = tiempo;
    segundos = tiempo / 1000;
    Serial.println ("Segundos trancurridos:");
    Serial.println (segundos);

  }
  digitalWrite(Ptrig, LOW);
  delayMicroseconds(2);
  digitalWrite(Ptrig, HIGH);   // genera el pulso de triger por 10ms
  delayMicroseconds(10);
  digitalWrite(Ptrig, LOW);

  duracion = pulseIn(Pecho, HIGH);
  distancia = (duracion / 2) / 29;          // calcula la distancia en centimetros



  while (distancia == 7 || distancia == 8) {} // si la distancia es 7 u 8
  double timeDrop1 = millis();  // captura el tiempo


  while (distancia <= 7 && distancia >= 1) {} // si la distancia es menor a 7cm y mayor a 1cm
  double timeDrop2 = millis(); //captura el tiempo
 digitalWrite(13, 1);     // en alto el pin 13 si la distancia es menor a 10cm
  digitalWrite(8, 1);
 
 
 
  double timeDrop = millis() - timeDrop2;
  double Acc = dist / (0 * timeDrop + 1 / 2 * pow(timeDrop, 2));
 
  //step 2
  delay(600);
  Serial.println();
  Serial.println();
  Serial.print("Se ha calculado la aceleración en base de ");
  Serial.print(timeDrop);
  Serial.println(" seg");
  Serial.print("con una distancia de ");
  Serial.print(dist);
  Serial.println(" cm");
  Serial.print("y es ");
  Serial.print(Acc);
  Serial.println(" m/s^2");


}

Thanks in advance for any help :wink: :slight_smile: :slight_smile:

Macroteen:
Hi everyone,

Wanting to do an Uniformly accelerated linear motion simulator with a HC-SR04. I bulit a 90cm brigde where a ball will start falling. The HC-SR04 will detect the 7cm left wall (and time stamp) until the ball drops there so it will detect a 1 or 2 cm distance(2nd time stamp and red Led turns). ....

Using an Ultrasonic Distance Sensor IMHO is not the right type of sensor to use for this application since it relies on pulse bursts bouncing off objects and therefore could easily 'miss' the passing ball.

something like a 'laser break' sensing circuit would be more appropriate for your application.
this webpage which I searched seems to give a few example...

hope that helps....