Program with explinations for ease of use of these Ultra-Sonic sensors to give mm resolution

Dependencies:   mbed

Committer:
martinsimpson
Date:
Thu Mar 16 10:38:40 2017 +0000
Revision:
1:221a6fd3d62f
Parent:
0:2b4e5d360246
Modified for two wire connections 1.0b

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martinsimpson 0:2b4e5d360246 1 /*
martinsimpson 1:221a6fd3d62f 2 Simple Program to use SFR05 or SFR04
martinsimpson 0:2b4e5d360246 3 Divide by 5.8 to give mm resolution from microseconds read of Echo pulse
martinsimpson 1:221a6fd3d62f 4 Version 1.0b
martinsimpson 0:2b4e5d360246 5 M.Simpson 17/10/2016
martinsimpson 0:2b4e5d360246 6 */
martinsimpson 0:2b4e5d360246 7 #include "mbed.h"
martinsimpson 0:2b4e5d360246 8
martinsimpson 1:221a6fd3d62f 9 #define TRIGGER_PIN D8 //Connect your Ultra Sonic device to these pins
martinsimpson 1:221a6fd3d62f 10 #define ECHO_PIN D7 //Modify for you platform used 'Arduino' Header on F401RE NUCLEO
martinsimpson 1:221a6fd3d62f 11
martinsimpson 1:221a6fd3d62f 12 DigitalOut Trigger(TRIGGER_PIN); //Instance of the DigitalOut class called 'Trigger'
martinsimpson 1:221a6fd3d62f 13 DigitalIn Echo(ECHO_PIN); //Instance of the DigitalIn class called 'Echo'
martinsimpson 0:2b4e5d360246 14
martinsimpson 0:2b4e5d360246 15 Timer pulse; //Instance of the Timer class called 'pulse'
martinsimpson 0:2b4e5d360246 16
martinsimpson 0:2b4e5d360246 17 float GetDistance(); //Function Prototype
martinsimpson 0:2b4e5d360246 18
martinsimpson 0:2b4e5d360246 19 int main() {
martinsimpson 0:2b4e5d360246 20 float Distance; //Assign a local variable for the main function
martinsimpson 0:2b4e5d360246 21 pulse.start(); //Start the instance of the class timer
martinsimpson 0:2b4e5d360246 22
martinsimpson 0:2b4e5d360246 23 while(true) //Continuous loop always true
martinsimpson 0:2b4e5d360246 24 {
martinsimpson 0:2b4e5d360246 25 Distance=GetDistance(); //Get the value returned from the function GetDistance()
martinsimpson 0:2b4e5d360246 26 printf("Distance %.0fmm\n\r",Distance); //Send the value to the serial port for monitoring purposes
martinsimpson 0:2b4e5d360246 27 wait_ms(250); //Wait for 250ms
martinsimpson 0:2b4e5d360246 28 }
martinsimpson 0:2b4e5d360246 29 }
martinsimpson 0:2b4e5d360246 30
martinsimpson 0:2b4e5d360246 31 float GetDistance(){ //Function Name to be called
martinsimpson 0:2b4e5d360246 32 int EchoPulseWidth=0,EchoStart=0,EchoEnd=0; //Assign and set to zero the local variables for this function
martinsimpson 1:221a6fd3d62f 33 Trigger=1; //Signal goes High i.e. 3V3
martinsimpson 0:2b4e5d360246 34 wait_us(100); //Wait 100us to give a pulse width
martinsimpson 1:221a6fd3d62f 35 Trigger=0; //Signal goes Low i.e. 0V
martinsimpson 0:2b4e5d360246 36 pulse.reset(); //Rest the instance of the Timer Class
martinsimpson 1:221a6fd3d62f 37 while(Echo==0&&EchoStart<25000){ //wait for Echo to go high
martinsimpson 0:2b4e5d360246 38 EchoStart=pulse.read_us(); //AND with timeout to prevent blocking!
martinsimpson 0:2b4e5d360246 39 }
martinsimpson 1:221a6fd3d62f 40 while(Echo==1&&(EchoEnd-EchoStart<25000)){ //wait for echo to return low
martinsimpson 0:2b4e5d360246 41 EchoEnd=pulse.read_us(); //or'd with timeout to prevent blocking!
martinsimpson 0:2b4e5d360246 42 }
martinsimpson 0:2b4e5d360246 43 EchoPulseWidth=EchoEnd-EchoStart; //time period in us
martinsimpson 1:221a6fd3d62f 44 return (float)EchoPulseWidth/5.8f; //calculate distance in mm and return the value as a float
martinsimpson 1:221a6fd3d62f 45 }