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

Dependencies:   mbed

Committer:
martinsimpson
Date:
Mon Oct 17 10:15:28 2016 +0000
Revision:
0:2b4e5d360246
Child:
1:221a6fd3d62f
SFR05 Using DigitalInOut Class; Version 1.0a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martinsimpson 0:2b4e5d360246 1 /*
martinsimpson 0:2b4e5d360246 2 Simple Program to use SFR05 or SFR04 in Single Pin Mode
martinsimpson 0:2b4e5d360246 3 Divide by 5.8 to give mm resolution from microseconds read of Echo pulse
martinsimpson 0:2b4e5d360246 4 Version 1.0a
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 0:2b4e5d360246 9 DigitalInOut TriggerEcho(D8); //Instance of the DigitalInOut class called 'TriggerEcho'
martinsimpson 0:2b4e5d360246 10
martinsimpson 0:2b4e5d360246 11 Timer pulse; //Instance of the Timer class called 'pulse'
martinsimpson 0:2b4e5d360246 12
martinsimpson 0:2b4e5d360246 13 float GetDistance(); //Function Prototype
martinsimpson 0:2b4e5d360246 14
martinsimpson 0:2b4e5d360246 15 int main() {
martinsimpson 0:2b4e5d360246 16 float Distance; //Assign a local variable for the main function
martinsimpson 0:2b4e5d360246 17 pulse.start(); //Start the instance of the class timer
martinsimpson 0:2b4e5d360246 18
martinsimpson 0:2b4e5d360246 19 while(true) //Continuous loop always true
martinsimpson 0:2b4e5d360246 20 {
martinsimpson 0:2b4e5d360246 21 Distance=GetDistance(); //Get the value returned from the function GetDistance()
martinsimpson 0:2b4e5d360246 22 printf("Distance %.0fmm\n\r",Distance); //Send the value to the serial port for monitoring purposes
martinsimpson 0:2b4e5d360246 23 wait_ms(250); //Wait for 250ms
martinsimpson 0:2b4e5d360246 24 }
martinsimpson 0:2b4e5d360246 25 }
martinsimpson 0:2b4e5d360246 26
martinsimpson 0:2b4e5d360246 27 float GetDistance(){ //Function Name to be called
martinsimpson 0:2b4e5d360246 28 int EchoPulseWidth=0,EchoStart=0,EchoEnd=0; //Assign and set to zero the local variables for this function
martinsimpson 0:2b4e5d360246 29 TriggerEcho.output(); //Make the DigitalInOut an Output to drive a signal
martinsimpson 0:2b4e5d360246 30 TriggerEcho.write(1); //Signal goes High i.e. 3V3
martinsimpson 0:2b4e5d360246 31 wait_us(100); //Wait 100us to give a pulse width
martinsimpson 0:2b4e5d360246 32 TriggerEcho.write(0); //Signal goes Low i.e. 0V
martinsimpson 0:2b4e5d360246 33 TriggerEcho.input(); //Make the DigitalInOut an INPUT to accept a signal
martinsimpson 0:2b4e5d360246 34 pulse.reset(); //Rest the instance of the Timer Class
martinsimpson 0:2b4e5d360246 35 while(TriggerEcho.read()==0&&EchoStart<25000){ //wait for Echo to go high
martinsimpson 0:2b4e5d360246 36 EchoStart=pulse.read_us(); //AND with timeout to prevent blocking!
martinsimpson 0:2b4e5d360246 37 }
martinsimpson 0:2b4e5d360246 38 while(TriggerEcho.read()==1&&(EchoEnd-EchoStart<25000)){ //wait for echo to return low
martinsimpson 0:2b4e5d360246 39 EchoEnd=pulse.read_us(); //or'd with timeout to prevent blocking!
martinsimpson 0:2b4e5d360246 40 }
martinsimpson 0:2b4e5d360246 41 EchoPulseWidth=EchoEnd-EchoStart; //time period in us
martinsimpson 0:2b4e5d360246 42 return (float)EchoPulseWidth/5.8f; //calculate distance in mm and reurn the value as a float
martinsimpson 0:2b4e5d360246 43 }