Chrono pour mesurer vitesse pour 1 tour (capteur analogique)

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
alainpegatoquet
Date:
Tue Nov 28 11:35:11 2017 +0000
Parent:
0:cf0fbe1e01d4
Commit message:
Chrono valid? (capteur a 20 cm distance de la ligne)

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Nov 28 09:45:23 2017 +0000
+++ b/main.cpp	Tue Nov 28 11:35:11 2017 +0000
@@ -1,4 +1,6 @@
 #include "mbed.h"
+#define DIST 15
+#define DEBUG 0
 
 // Global variables
 Serial pc(USBTX, USBRX);
@@ -6,6 +8,9 @@
 Timer timer1;
 
 float IrSensor;
+float dist_new = 0.0;
+float dist_old = 0.0;
+int elapsed_time = 0;
 // Prototype
 float distance(float);
 
@@ -13,14 +18,30 @@
 int main(void)
 {
     pc.printf("Debut du programme...\n");
+    timer1.start();
     
     while(1) {
         IrSensor=Ain.read();        // IrSensor belongs to [0.0; 1.0]
+        //pc.printf("Distance en cm : %.1f \n\r",distance(IrSensor*3.3)); 
+#if DEBUG
         pc.printf("%f \n\r",IrSensor*3.3);  // Output voltage from 0 to 3.3V
-        
         pc.printf("Distance en cm : %.1f \n\r",distance(IrSensor*3.3));        
-        wait (0.5);
+#endif  
+        dist_old = dist_new;
+        dist_new = distance(IrSensor*3.3);     
+        if ((dist_old > 40) && ((dist_new> 0) && (dist_new < 30)))
+        {
+            elapsed_time = timer1.read_ms();
+            timer1.reset();
+            pc.printf("************* %2.2f sec *************\r\n",(float)elapsed_time/1000.0);      
+            wait_ms(1000);      
+        }        
+        wait(0.1);
+        float val = timer1.read_ms()/1000.0;
+        pc.printf("%3.2f sec\r",val); 
+        
     }
+    timer1.stop();
 }
 
 // Calcul de la distance