primjer mjerenja udaljenosti

Dependencies:   mbed

Fork of HC-SR04 by Goji .

Revision:
0:e7a1440e980a
Child:
1:144bd7bdd1d1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jan 28 15:17:47 2015 +0000
@@ -0,0 +1,32 @@
+#include "mbed.h"
+//#include "Pswitch.h"
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+DigitalOut trig (p25);
+DigitalIn  echo (p6);
+//Pswitch sw(p8);
+double pulsedur, distance;
+Timer tmr;
+
+int main()
+{
+    pc.printf("start\n");
+    while(1) {
+        tmr.reset();
+        trig = 1;
+        wait_us(10);
+        trig = 0;
+
+        while (!echo); // wait high
+        tmr.start();
+
+        while (echo);  // wait low
+        tmr.stop();
+
+        pulsedur = tmr.read_us();
+        distance = pulsedur*0.017; //cm
+        pc.printf("pulsedur=%8.2f\n", distance);
+        wait(0.3);
+    }
+}