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.
Fork of PRO1 by
HCSR04.h
00001 #ifndef HCSR04_H_TVZMT 00002 #define HCSR04_H_TVZMT 00003 00004 /** A distance measurement class using ultrasonic sensor HC-SR04. 00005 * 00006 * Example of use: 00007 * @code 00008 * #include "mbed.h" 00009 * #include "HCSR04.h" 00010 * 00011 * Serial pc(USBTX, USBRX); 00012 * 00013 * int main() { 00014 * HCSR04 sensor(p5, p7); 00015 * sensor.setRanges(10, 110); 00016 * pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r", 00017 * sensor.getMinRange(), sensor.getMaxRange()); 00018 * while(1) { 00019 * pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm()); 00020 * wait_ms(500); 00021 * } 00022 * } 00023 * @endcode 00024 */ 00025 class HCSR04 { 00026 00027 public: 00028 00029 /** Receives two PinName variables. 00030 * @param echoPin mbed pin to which the echo signal is connected to 00031 * @param triggerPin mbed pin to which the trigger signal is connected to 00032 */ 00033 HCSR04(PinName echoPin, PinName triggerPin); 00034 00035 /** Calculates the distance in cm, with the calculation time of approximatelly 23.7 ms. 00036 * @returns distance of the measuring object in cm. 00037 */ 00038 float getDistance_cm(); 00039 00040 /** Calculates the distance in mm, with the calculation time of approximatelly 23.7 ms. 00041 * @returns distance of the measuring object in mm. 00042 */ 00043 float getDistance_mm(); 00044 00045 /** Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm. 00046 * @param minRange Minimum range in cm. Must be between 2 cm and maxRange. 00047 * @param maxRange Maximum range in cm. Must be between minRange and 400 cm. 00048 */ 00049 void setRanges(float minRange, float maxRange); 00050 00051 /** Retreives the minimum sensor range set by the user. 00052 * @returns the minimum sensor range set by the user in cm. 00053 */ 00054 float getMinRange(); 00055 00056 /** Retreives the maximum sensor range set by the user. 00057 * @returns the maximum sensor range set by the user in cm. 00058 */ 00059 float getMaxRange(); 00060 00061 private: 00062 00063 InterruptIn echo; // echo pin 00064 DigitalOut trigger; // trigger pin 00065 Timer timer; // echo pulsewidth measurement 00066 float distance; // store the distance in cm 00067 float minDistance; // minimum measurable distance 00068 float maxDistance; // maximum measurable distance 00069 00070 /** Start the timer. */ 00071 void startTimer(); 00072 00073 /** Stop the timer. */ 00074 void stopTimer(); 00075 00076 /** Initialization. */ 00077 void init(); 00078 00079 /** Start the measurement. */ 00080 void startMeasurement(); 00081 }; 00082 00083 #endif
Generated on Tue Jul 12 2022 22:02:46 by
