Kim Nielsen / Mbed 2 deprecated PRO1

Dependencies:   mbed

Fork of PRO1 by E2016-S1-Team5

Embed: (wiki syntax)

« Back to documentation index

HCSR04 Class Reference

HCSR04 Class Reference

A distance measurement class using ultrasonic sensor HC-SR04. More...

#include <HCSR04.h>

Public Member Functions

 HCSR04 (PinName echoPin, PinName triggerPin)
 Receives two PinName variables.
float getDistance_cm ()
 Calculates the distance in cm, with the calculation time of approximatelly 23.7 ms.
float getDistance_mm ()
 Calculates the distance in mm, with the calculation time of approximatelly 23.7 ms.
void setRanges (float minRange, float maxRange)
 Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm.
float getMinRange ()
 Retreives the minimum sensor range set by the user.
float getMaxRange ()
 Retreives the maximum sensor range set by the user.

Detailed Description

A distance measurement class using ultrasonic sensor HC-SR04.

Example of use:

 #include "mbed.h"
 #include "HCSR04.h"

 Serial pc(USBTX, USBRX);

 int main() {
     HCSR04 sensor(p5, p7);
     sensor.setRanges(10, 110);
     pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r",
       sensor.getMinRange(), sensor.getMaxRange());
     while(1) {
         pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm());
         wait_ms(500);
     }
 }

Definition at line 25 of file HCSR04.h.


Constructor & Destructor Documentation

HCSR04 ( PinName  echoPin,
PinName  triggerPin 
)

Receives two PinName variables.

Parameters:
echoPinmbed pin to which the echo signal is connected to
triggerPinmbed pin to which the trigger signal is connected to

Definition at line 4 of file HCSR04.cpp.


Member Function Documentation

float getDistance_cm (  )

Calculates the distance in cm, with the calculation time of approximatelly 23.7 ms.

Returns:
distance of the measuring object in cm.

Definition at line 42 of file HCSR04.cpp.

float getDistance_mm (  )

Calculates the distance in mm, with the calculation time of approximatelly 23.7 ms.

Returns:
distance of the measuring object in mm.

Definition at line 47 of file HCSR04.cpp.

float getMaxRange (  )

Retreives the maximum sensor range set by the user.

Returns:
the maximum sensor range set by the user in cm.

Definition at line 65 of file HCSR04.cpp.

float getMinRange (  )

Retreives the minimum sensor range set by the user.

Returns:
the minimum sensor range set by the user in cm.

Definition at line 61 of file HCSR04.cpp.

void setRanges ( float  minRange,
float  maxRange 
)

Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm.

Parameters:
minRangeMinimum range in cm. Must be between 2 cm and maxRange.
maxRangeMaximum range in cm. Must be between minRange and 400 cm.

Definition at line 52 of file HCSR04.cpp.