MMlab / Mbed 2 deprecated PING_ultrasonic

Dependencies:   USBDevice mbed

Revision:
0:5d1796a0f9ce
Child:
1:5aa590d1f1bf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Sep 17 09:41:46 2016 +0000
@@ -0,0 +1,98 @@
+#include "mbed.h"
+
+Serial pc(USBTX,USBRX);
+
+DigitalInOut pingPin1(p18);
+DigitalInOut pingPin2(p19);
+
+Timer tmr1, tmr2,tmr;
+Timeout timeouter;
+int flag=0;
+
+long microsecondsToCentimeters(long microseconds);
+
+void reset()
+{
+    flag = 0;
+    tmr.reset();
+}
+
+int main()
+{
+    pc.baud(115200);
+    while (1) {
+        long duration1, duration2,USSDistance1, USSDistance2, cm1, cm2, time;
+
+        pingPin1.output();
+        pingPin1 = 0;
+        wait_us(2);
+        pingPin1 = 1;
+        wait_us(5);
+        pingPin1 = 0;
+
+        pingPin1.input(); // pulseIn
+        tmr1.reset();
+
+        while (!pingPin1); // wait for high
+        tmr1.start();
+        while (pingPin1); // wait for low
+        tmr1.stop();
+        duration1 = tmr1.read_us();
+        USSDistance1 = duration1;// * 0.0170;
+
+        cm1 = microsecondsToCentimeters(USSDistance1);
+
+        if(15<cm1&&cm1<40) {
+            if(flag==0) {
+                flag=1;
+                tmr.reset();
+                tmr.start();
+                timeouter.attach(&reset, 5);
+            }
+            if(flag==1) {
+                timeouter.detach();
+                timeouter.attach(&reset, 5);
+            }
+            time=tmr.read_ms();
+            printf("1: %d  %d[ms]\n", cm1,time);
+        }
+
+        wait_ms(10);
+        
+
+        pingPin2.output();
+        pingPin2 = 0;
+        wait_us(2);
+        pingPin2 = 1;
+        wait_us(5);
+        pingPin2 = 0;
+
+        pingPin2.input(); // pulseIn
+        tmr2.reset();
+
+        while (!pingPin2); // wait for high
+        tmr2.start();
+        while (pingPin2); // wait for low
+        tmr2.stop();
+
+        duration2 = tmr2.read_us();
+        USSDistance2 = duration2;// * 0.0170;
+
+        cm2 = microsecondsToCentimeters(USSDistance2);
+        if(15<cm2&&cm2<40) {
+            time=tmr.read_ms();
+            printf("                 2: %d %d[ms]\n",cm2,time);
+        }
+        wait_ms(10);
+        }
+}
+
+long microsecondsToInches(long microseconds)
+{
+    return microseconds / 74 / 2;
+}
+
+long microsecondsToCentimeters(long microseconds)
+{
+    return microseconds / 29 / 2;
+}
\ No newline at end of file