Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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