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.
Revision 1:221a6fd3d62f, committed 2017-03-16
- Comitter:
- martinsimpson
- Date:
- Thu Mar 16 10:38:40 2017 +0000
- Parent:
- 0:2b4e5d360246
- Commit message:
- Modified for two wire connections 1.0b
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Oct 17 10:15:28 2016 +0000
+++ b/main.cpp Thu Mar 16 10:38:40 2017 +0000
@@ -1,12 +1,16 @@
/*
- Simple Program to use SFR05 or SFR04 in Single Pin Mode
+ Simple Program to use SFR05 or SFR04
Divide by 5.8 to give mm resolution from microseconds read of Echo pulse
- Version 1.0a
+ Version 1.0b
M.Simpson 17/10/2016
*/
#include "mbed.h"
-DigitalInOut TriggerEcho(D8); //Instance of the DigitalInOut class called 'TriggerEcho'
+#define TRIGGER_PIN D8 //Connect your Ultra Sonic device to these pins
+#define ECHO_PIN D7 //Modify for you platform used 'Arduino' Header on F401RE NUCLEO
+
+DigitalOut Trigger(TRIGGER_PIN); //Instance of the DigitalOut class called 'Trigger'
+DigitalIn Echo(ECHO_PIN); //Instance of the DigitalIn class called 'Echo'
Timer pulse; //Instance of the Timer class called 'pulse'
@@ -26,18 +30,16 @@
float GetDistance(){ //Function Name to be called
int EchoPulseWidth=0,EchoStart=0,EchoEnd=0; //Assign and set to zero the local variables for this function
- TriggerEcho.output(); //Make the DigitalInOut an Output to drive a signal
- TriggerEcho.write(1); //Signal goes High i.e. 3V3
+ Trigger=1; //Signal goes High i.e. 3V3
wait_us(100); //Wait 100us to give a pulse width
- TriggerEcho.write(0); //Signal goes Low i.e. 0V
- TriggerEcho.input(); //Make the DigitalInOut an INPUT to accept a signal
+ Trigger=0; //Signal goes Low i.e. 0V
pulse.reset(); //Rest the instance of the Timer Class
- while(TriggerEcho.read()==0&&EchoStart<25000){ //wait for Echo to go high
+ while(Echo==0&&EchoStart<25000){ //wait for Echo to go high
EchoStart=pulse.read_us(); //AND with timeout to prevent blocking!
}
- while(TriggerEcho.read()==1&&(EchoEnd-EchoStart<25000)){ //wait for echo to return low
+ while(Echo==1&&(EchoEnd-EchoStart<25000)){ //wait for echo to return low
EchoEnd=pulse.read_us(); //or'd with timeout to prevent blocking!
}
EchoPulseWidth=EchoEnd-EchoStart; //time period in us
- return (float)EchoPulseWidth/5.8f; //calculate distance in mm and reurn the value as a float
-}
\ No newline at end of file
+ return (float)EchoPulseWidth/5.8f; //calculate distance in mm and return the value as a float
+}