Add new file

This commit is contained in:
Cédric Abonnel 2022-05-15 18:25:58 +00:00
parent 960cc7ff3e
commit 48fd3fd3ef
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
//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;
digitalWrite(TriggerPin, LOW); //Pause de 4 µs
delayMicroseconds(4);
digitalWrite(TriggerPin, HIGH); //Génération de 10 µs
delayMicroseconds(10);
digitalWrite(TriggerPin, LOW); //Pause
duration = pulseIn(EchoPin, HIGH); // lecture
distanceCm = duration * 10 / 292/ 2; // Vitesse du son converti en cm
return distanceCm;
}