Skip to content

Instantly share code, notes, and snippets.

@AnidemDex
Created November 4, 2020 18:33
Show Gist options
  • Save AnidemDex/1619110810f77761a29a501ce0f3ed4d to your computer and use it in GitHub Desktop.
Save AnidemDex/1619110810f77761a29a501ce0f3ed4d to your computer and use it in GitHub Desktop.
#include <NewPing.h>
/*Aqui se configuran los pines donde debemos conectar el sensor*/
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
/*Crear el objeto de la clase NewPing*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
}
void loop() {
// Esperar 1 segundo entre mediciones
delay(1000);
// Obtener medicion de tiempo de viaje del sonido y guardar en variable uS
int uS = sonar.ping_median();
// Imprimir la distancia medida a la consola serial
Serial.print("Distancia: ");
// Calcular la distancia con base en una constante
Serial.print(uS / US_ROUNDTRIP_CM);
Serial.println("cm");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment