NXPCup_Cachan / Mbed 2 deprecated Bibli_Ultrason

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
theoduval
Date:
Tue Mar 10 16:55:59 2020 +0000
Commit message:
biblio ultrason SRF05

Changed in this revision

bibli.cpp Show annotated file Show diff for this revision Revisions of this file
bibli.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bibli.cpp	Tue Mar 10 16:55:59 2020 +0000
@@ -0,0 +1,57 @@
+#include "bibli.h"
+
+int getDistance()
+{
+    static DigitalOut UltrasonTriger(p18);
+    static DigitalIn UltrasonEcho(p17);
+    
+    static Timer timer;
+    float ultrason_distance=-1;
+    
+    static short etat = 0;
+    
+    switch(etat)
+    {
+        case 0:
+            UltrasonTriger = 1; // envoie une impulsion de 10us
+            timer.start();
+            etat = 1;
+            break;
+            
+        case 1:
+            if(timer.read_us() > 10) // fin de l'impulsion
+            {
+                UltrasonTriger = 0;
+                timer.reset();
+                etat = 2;   
+            }
+            break;
+             
+        case 2:
+            if(UltrasonEcho.read()==1)  // Si on a une reception
+            {
+                timer.start(); // lance timer pour mersurer l'impulsion
+                etat = 3;
+            }
+            if(timer.read_ms()>30) etat = 4;  // Si au bout de 30ms pas de reception alors aucune mesure
+            break;
+            
+        case 3:
+            if(UltrasonEcho.read()==0)  // Mesurer le temps d'impulsion de la reception
+            {
+                ultrason_distance=timer.read_us()/58-8; // Calcul de la distance
+                timer.stop();
+                timer.reset();
+                etat = 0;
+            }
+            if(timer.read_ms()>30) etat = 4;
+            break;
+            
+        case 4:
+            timer.stop();
+            timer.reset();
+            etat = 0;
+            break;
+    }
+    return ultrason_distance;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bibli.h	Tue Mar 10 16:55:59 2020 +0000
@@ -0,0 +1,8 @@
+#ifndef BIBLI_H
+#define BIBLI_H
+#include "mbed.h"
+
+// FONCTIONS
+int getDistance();
+
+#endif // BIBLI_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Mar 10 16:55:59 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file