Help me, speed with hc-sr04

hi, can you help me with the code for the speed every 1 second and print it.
I saw tutorials that subtracted distance 1 - distance 2 and the speed came out, but I do it and it doesn't come out.

escribe o pega el código aquí

int TRIG = 10;			// trigger en pin 10
int ECO = 9;			// echo en pin 9
int LED = 3;			// LED en pin 3
int DURACION;
int DISTANCIA;
int distancia1=0;
int distancia2=0;

void setup()
{
  pinMode(TRIG, OUTPUT); 	// trigger como salida
  pinMode(ECO, INPUT);		// echo como entrada
  pinMode(LED, OUTPUT);		// LED como salida
  Serial.begin(9600);  		// inicializacion de comunicacion serial a 9600 bps

}

void loop()
{
  
  digitalWrite(TRIG, HIGH); 		// generacion del pulso a enviar
  delay(1);				            // al pin conectado al trigger
  digitalWrite(TRIG, LOW);	     	// del sensor
  
  DURACION = pulseIn(ECO, HIGH);	// con funcion pulseIn se espera un pulso
  	
  DISTANCIA = DURACION / 58.2;		// distancia medida en centimetros
  
  distancia1=DISTANCIA
  
  Serial.print("La distancia en cm es:");
  Serial.println(DISTANCIA);	// envio de valor de distancia por monitor serial
  delay(200);				// demora entre datos

  if (DISTANCIA <= 50 && DISTANCIA >= 0){	// si distancia entre 0 y 20 cms.
    digitalWrite(LED, HIGH);	// enciende LED
  }
  else{
    digitalWrite(LED, LOW);			// apaga LED
    }
   
}

try changing delay(1) to delayMicroseconds(8);

I don't see where you are trying to calculate speed?

distance1 = ultrasonicRead(); //calls ultrasoninicRead() function below
   
   delay(1000);//giving a time gap of 1 sec
   
   distance2 = ultrasonicRead(); //calls ultrasoninicRead() function below
   
   //formula change in distance divided by change in time
   Speed = (distance2 - distance1)/1.0; //as the time gap is 1 sec we divide it by 1.
   
  //Displaying Speed
 Serial.print("velocidad en cm/s  :"); 
 Serial.println(Speed);

I was trying this way but the "ultrasonicRead ()" does not recognize me

Post your complete code (for post #3). I suspect thast you pulled this from the internet somewhere and did not copy evevrything; ultrasonicRead() is probably a function that contains this part of your code

  digitalWrite(TRIG, HIGH); 		// generacion del pulso a enviar
  delay(1);				            // al pin conectado al trigger
  digitalWrite(TRIG, LOW);	     	// del sensor
  
  DURACION = pulseIn(ECO, HIGH);	// con funcion pulseIn se espera un pulso
  	
  DISTANCIA = DURACION / 58.2;		// distancia medida en centimetros

Rewriting it for your scenario will probably be

uint32_t ultrasonicRead()
{
  digitalWrite(TRIG, HIGH); 		// generacion del pulso a enviar
  delay(1);				            // al pin conectado al trigger
  digitalWrite(TRIG, LOW);	     	// del sensor
  
  uint32_t duration = pulseIn(ECO, HIGH);	// con funcion pulseIn se espera un pulso
  	
  return (duration / 58.2);		// distancia medida en centimetros
}

Code not tested nor compiled.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.