2022-05-15 20:25:58 +02:00
|
|
|
//Définition des constanntes
|
|
|
|
// PIN Echo
|
|
|
|
const int EchoPin = 8;
|
|
|
|
// PIN Trigger
|
|
|
|
const int TriggerPin = 9;
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(9600);
|
|
|
|
pinMode(TriggerPin, OUTPUT);
|
|
|
|
pinMode(EchoPin, INPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
int cm = ping(TriggerPin, EchoPin);
|
|
|
|
Serial.print("Distance mesurée : ");
|
|
|
|
Serial.println(cm);
|
|
|
|
delay(1000); // 1 s
|
|
|
|
}
|
|
|
|
|
|
|
|
//Calculer la distance
|
|
|
|
int ping(int TriggerPin, int EchoPin) {
|
|
|
|
long duration, distanceCm;
|
|
|
|
|
2022-05-15 20:31:34 +02:00
|
|
|
digitalWrite(TriggerPin, LOW); //Démarrage de la séquence de 4 µs
|
2022-05-15 20:25:58 +02:00
|
|
|
delayMicroseconds(4);
|
2022-05-15 20:31:34 +02:00
|
|
|
digitalWrite(TriggerPin, HIGH); // Impulsion de déclenchement 10 µs
|
2022-05-15 20:25:58 +02:00
|
|
|
delayMicroseconds(10);
|
2022-05-15 20:31:34 +02:00
|
|
|
digitalWrite(TriggerPin, LOW); // Arrêt de l'impulsion
|
2022-05-15 20:25:58 +02:00
|
|
|
|
2022-05-15 20:31:34 +02:00
|
|
|
duration = pulseIn(EchoPin, HIGH); // Émission du signal et lecture
|
2022-05-15 20:25:58 +02:00
|
|
|
|
|
|
|
distanceCm = duration * 10 / 292/ 2; // Vitesse du son converti en cm
|
|
|
|
return distanceCm;
|
|
|
|
}
|