Ultrasonic

Dependents:   2014_Ensoul_Capstone Thrower_KRAI ECE4180Finalproject 4180Lab3Part2

Revision:
0:4a3021604777
Child:
1:d8a93321fc01
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ultrasonic.cpp	Thu Jan 09 07:10:03 2014 +0000
@@ -0,0 +1,40 @@
+#include "Ultrasonic.h"
+#include "mbed.h"
+
+Ultrasonic::Ultrasonic(PinName tx, PinName rx) : _tx(tx),_rx(rx)
+{
+    _tx.input();
+    t.reset();
+    triger.attach(this, &Ultrasonic::triger_func, 0.1);
+    _rx.rise(this, &Ultrasonic::start_func);
+    _rx.fall(this, &Ultrasonic::end_func);
+}
+
+void Ultrasonic::triger_func()
+{
+    _tx.output();
+    _tx = 1;
+    wait_us(10);
+    _tx = 0;
+    _tx.input();
+
+}
+
+void Ultrasonic::start_func()
+{
+    t.reset();
+    t.start();
+}
+
+void Ultrasonic::end_func()
+{
+    t.stop();
+    if (( t > 0.00012 ) & ( t < 0.0187)) {
+        eco = 331.5*(t/2.0)*1000; // 30mm ~ 3000mm
+    }
+}
+
+float Ultrasonic::read()
+{
+    return eco;
+}
\ No newline at end of file