Chris Powers / Mbed 2 deprecated YT_001_HCSR04

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Powers
Date:
Wed Jun 03 15:31:47 2020 +0000
Commit message:
Version 0.1

Changed in this revision

main.cpp 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
diff -r 000000000000 -r 777a2656a150 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jun 03 15:31:47 2020 +0000
@@ -0,0 +1,58 @@
+#include "mbed.h"
+// Trigger PIN
+DigitalOut trigger(D9);
+// Echo PIN
+DigitalIn echo(D8);
+// Serial connection
+Serial pc(USBTX, USBRX, 115200);
+// Timer to get the time between the 
+Timer get_time;
+
+// Prototyping
+float sensor(void);
+
+// Function will return distance in cm
+float sensor(void)
+{
+    float distance;
+    
+    // Send HIGH for 10 us to start measurement
+    trigger.write(1);
+    wait_us(10);
+    trigger.write(0);
+    // Wait for the ECHO to change to HIGH
+    while(echo.read() == 0) {}
+    // Start the timer and let it run until ECHO is LOW again
+    get_time.reset();
+    get_time.start();
+    while(echo.read() == 1) {}
+    get_time.stop();
+    // Calculate the distance from the time in microseconds
+    distance = get_time.read_us();
+    distance = distance * 0.03432f / 2.0f;
+    
+    return distance;
+    
+}
+
+
+int main(void)
+{
+    // Set the trigger output to LOW at the start of the programm
+    trigger = 0;
+    // Welcome text
+    pc.printf("\nHC-SR04 Sensor\n");
+
+    // Main loop to measure the distance
+    while(1)
+    {
+        wait(0.5);
+        // Print the actual distance to the serial output
+        pc.printf("Distance: %.2fcm \n", sensor());
+        
+    }
+    
+    return 0;
+}
+
+
diff -r 000000000000 -r 777a2656a150 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Jun 03 15:31:47 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file