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

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Simple Program to use SFR05 or SFR04
00003     Divide by 5.8 to give mm resolution from microseconds read of Echo pulse
00004     Version 1.0b
00005     M.Simpson 17/10/2016
00006 */
00007 #include "mbed.h"
00008 
00009 #define TRIGGER_PIN D8                                          //Connect your Ultra Sonic device to these pins
00010 #define ECHO_PIN    D7                                          //Modify for you platform used 'Arduino' Header on F401RE NUCLEO
00011 
00012 DigitalOut Trigger(TRIGGER_PIN);                                //Instance of the DigitalOut class called 'Trigger'
00013 DigitalIn  Echo(ECHO_PIN);                                      //Instance of the DigitalIn class called 'Echo'
00014 
00015 Timer pulse;                                                    //Instance of the Timer class called 'pulse'
00016 
00017 float GetDistance();                                            //Function Prototype
00018 
00019 int main() {
00020      float Distance;                                            //Assign a local variable for the main function
00021      pulse.start();                                             //Start the instance of the class timer        
00022      
00023      while(true)                                                //Continuous loop always true
00024      {
00025         Distance=GetDistance();                                 //Get the value returned from the function GetDistance()
00026         printf("Distance %.0fmm\n\r",Distance);                 //Send the value to the serial port for monitoring purposes
00027         wait_ms(250);                                           //Wait for 250ms
00028      }    
00029 }
00030  
00031 float GetDistance(){                                            //Function Name to be called
00032      int EchoPulseWidth=0,EchoStart=0,EchoEnd=0;                //Assign and set to zero the local variables for this function
00033      Trigger=1;                                                 //Signal goes High i.e. 3V3
00034      wait_us(100);                                              //Wait 100us to give a pulse width
00035      Trigger=0;                                                 //Signal goes Low i.e. 0V
00036      pulse.reset();                                             //Rest the instance of the Timer Class
00037      while(Echo==0&&EchoStart<25000){                           //wait for Echo to go high
00038             EchoStart=pulse.read_us();                          //AND with timeout to prevent blocking!      
00039      }
00040      while(Echo==1&&(EchoEnd-EchoStart<25000)){                 //wait for echo to return low
00041         EchoEnd=pulse.read_us();                                //or'd with timeout to prevent blocking!   
00042      }
00043      EchoPulseWidth=EchoEnd-EchoStart;                          //time period in us
00044      return (float)EchoPulseWidth/5.8f;                         //calculate distance in mm and return the value as a float
00045 }