Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 0:c9881f6aef19
- Child:
- 1:a656f3ed73e5
diff -r 000000000000 -r c9881f6aef19 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Nov 13 05:44:18 2018 +0000
@@ -0,0 +1,54 @@
+
+#include "mbed.h"
+
+DigitalOut trigger(PC_12);
+DigitalOut myled(LED1); //monitor trigger
+DigitalOut myled2(LED2); //monitor echo
+DigitalIn echo(PC_10);
+int distance = 0;
+int correction = 0;
+Timer sonar;
+
+int main()
+{
+ sonar.reset();
+// measure actual software polling timer delays
+// delay used later in time correction
+// start timer
+ sonar.start();
+// min software polling delay to read echo pin
+ while (echo==2) {};
+ myled2 = 0;
+// stop timer
+ sonar.stop();
+// read timer
+ correction = sonar.read_us();
+ printf("Approximate software overhead timer delay is %d uS\n\r",correction);
+
+//Loop to read Sonar distance values, scale, and print
+ while(1) {
+// trigger sonar to send a ping
+ trigger = 1;
+ myled = 1;
+ myled2 = 0;
+ sonar.reset();
+ wait_us(10.0);
+ trigger = 0;
+ myled = 0;
+//wait for echo high
+ while (echo==0) {};
+ myled2=echo;
+//echo high, so start timer
+ sonar.start();
+//wait for echo low
+ while (echo==1) {};
+//stop timer and read value
+ sonar.stop();
+//subtract software overhead timer delay and scale to cm
+ distance = (sonar.read_us()-correction)/58.0;
+ myled2 = 0;
+ printf(" %d cm \n\r",distance);
+//wait so that any echo(s) return before sending another ping
+ wait(0.2);
+ }
+}
\ No newline at end of file