Commit e9250d90e27183aa02cf40994e93203155fead16

Authored by rfoucaul
1 parent 4ad9c17b

Ajout du fichier de programmation des capteurs de distance : version non fonctionnelle

Showing 1 changed file with 52 additions and 0 deletions   Show diff stats
Programmation/Distance/Distance.ino 0 → 100644
... ... @@ -0,0 +1,52 @@
  1 +/*
  2 + * Code d'exemple pour un capteur à ultrasons HC-SR04.
  3 + */
  4 +
  5 +/* Constantes pour les broches */
  6 +const byte TRIGGER_PIN = 9; // Broche TRIGGER
  7 +const byte ECHO_PIN = 8; // Broche ECHO
  8 +
  9 +/* Constantes pour le timeout */
  10 +const unsigned long MEASURE_TIMEOUT = 25000UL; // 25ms = ~8m à 340m/s
  11 +
  12 +/* Vitesse du son dans l'air en mm/us */
  13 +const float SOUND_SPEED = 340.0 / 1000;
  14 +
  15 +/** Fonction setup() */
  16 +void setup() {
  17 +
  18 + /* Initialise le port série */
  19 + Serial.begin(115200);
  20 +
  21 + /* Initialise les broches */
  22 + pinMode(TRIGGER_PIN, OUTPUT);
  23 + digitalWrite(TRIGGER_PIN, LOW); // La broche TRIGGER doit être à LOW au repos
  24 + pinMode(ECHO_PIN, INPUT);
  25 +}
  26 +
  27 +/** Fonction loop() */
  28 +void loop() {
  29 +
  30 + /* 1. Lance une mesure de distance en envoyant une impulsion HIGH de 10µs sur la broche TRIGGER */
  31 + digitalWrite(TRIGGER_PIN, HIGH);
  32 + delayMicroseconds(10);
  33 + digitalWrite(TRIGGER_PIN, LOW);
  34 +
  35 + /* 2. Mesure le temps entre l'envoi de l'impulsion ultrasonique et son écho (si il existe) */
  36 + long measure = pulseIn(ECHO_PIN, HIGH, MEASURE_TIMEOUT);
  37 +
  38 + /* 3. Calcul la distance à partir du temps mesuré */
  39 + float distance_mm = measure / 2.0 * SOUND_SPEED;
  40 +
  41 + /* Affiche les résultats en mm, cm et m */
  42 + Serial.print("Distance: ");
  43 + Serial.print(distance_mm);
  44 + Serial.print("mm (");
  45 + Serial.print(distance_mm / 10.0, 2);
  46 + Serial.print("cm, ");
  47 + Serial.print(distance_mm / 1000.0, 2);
  48 + Serial.println("m)");
  49 +
  50 + /* Délai d'attente pour éviter d'afficher trop de résultats à la seconde */
  51 + delay(500);
  52 +}
... ...