Skip to content

Instantly share code, notes, and snippets.

@Hotfirenet
Created October 11, 2021 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hotfirenet/25d7f9e9afdb91cdd1a5dba44d95a6b4 to your computer and use it in GitHub Desktop.
Save Hotfirenet/25d7f9e9afdb91cdd1a5dba44d95a6b4 to your computer and use it in GitHub Desktop.
const byte TRIGGER_PIN = 7; // entrée numérique 7 affectée au trig
const byte ECHO_PIN = 11; // entrée numérique 11 affectées à l'écho
const unsigned long MEASURE_TIMEOUT = 25000UL; // délais avant annulation de mesure : 25ms correspond à 8m à 340m/s
const float SOUND_SPEED = 340.0 / 1000; // Vitesse du son dans l'air en mm/us
void setup() {
Serial.begin(115200);
pinMode(TRIGGER_PIN, OUTPUT);
digitalWrite(TRIGGER_PIN, LOW);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
long measure = pulseIn(ECHO_PIN, HIGH, MEASURE_TIMEOUT);
float distance_mm = measure / 2.0 * SOUND_SPEED;
Serial.print("\t");
Serial.println(distance_mm,0);
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment