From 48fd3fd3ef761110d3aace9256c74a222b5b658d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Abonnel?= Date: Sun, 15 May 2022 18:25:58 +0000 Subject: [PATCH] Add new file --- arduino/Update sketchDistanceSerial.ino | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 arduino/Update sketchDistanceSerial.ino diff --git a/arduino/Update sketchDistanceSerial.ino b/arduino/Update sketchDistanceSerial.ino new file mode 100644 index 0000000..0b8a008 --- /dev/null +++ b/arduino/Update sketchDistanceSerial.ino @@ -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; +}