An arduino library to handle HC-SR04 distance sensor.
#include <hcsr04.h>
#define TRIG_PIN 4
#define ECHO_PIN 3
HCSR04 mySensor;
unsigned short distance;
void setup() {
mySensor.init(TRIG_PIN, ECHO_PIN);
}
void loop(){
distance = mySensor.readDisctanceInMm();
// Do something with measured distance value
}
- Clone or download as a .zip file and unpack this repository directly into "libraries" folder of Arduino IDE (e.g.: *c:\Program Files (x86)\Arduino\libraries* )
- Restart Arduino IDE.
- Now you can find and use this library from Sketch > Include library > "Arduino-HC-SR04-library"
void init(unsigned char trigPin, unsigned char echoPin);
/*
* Initialite the ButtonHandler object
* Parameters:
* trigPin: The pin of arduino what is connected to trig pin of HC-SR04 sensor
* echoPin: The pin of arduino what is connected to echo pin of HC-SR04 sensor
*/
unsigned short readDisctance();
/*
* Read disctance
* Returns:
* the disctance in mm.
*/
unsigned short readDisctanceInCm();
/*
* Read disctance in cm
* Returns:
* the disctance in cm
* (rounded value from mm value
* with mathematically corrent rounding).
*/
unsigned short readAvgDisctanceInMm(unsigned short measurementCount);
/*
* Read disctance in mm by multiple measurement
* and calculate average of results
* Parameter:
* measurementCount: The count of measurements what will be used to calculate average
* Returns:
* the average of multiple disctance measurement in mm.
*/
unsigned short readAvgDisctanceInCm(unsigned short measurementCount);
/*
* Read disctance in cm by multiple measurement
* and calculate average of results
* Parameter:
* measurementCount: The count of measurements what will be used to calculate average
* Returns:
* the average of multiple disctance measurement in cm.
* (rounded value from mm value
* with mathematically corrent rounding).
*/
void setCalibration(double calibrationMultiplier);
/*
* Set calibration multiplier
* Default is 1.0
*/
double getCalibration();
/*
* Returns:
* Current calibration multiplier.
*/
void setOffset(long offsetValueInMm);
/*
* Set offset for disctance measurement.
* Default is 0 mm.
*/
long getOffset();
/*
* Returns:
* Current ofset offset for disctance measurement.
*/
void setDelayBetweenAvgMeasurementsInMs(unsigned short delayInMs);
/*
* Returns the average of measured disctance in cm.
* Parameter:
* delayInMs:
*/
unsigned short readAccurateDisctanceInMm();
/*
* Read distance 5 times, drop the minimum and the maximum values
* And return the average of the middle 3 measurement result in mm.
*/
unsigned short readAccurateDisctanceInCm();
/*
* Read distance 5 times, drop the minimum and the maximum values
* And return the average of the middle 3 measurement result in mm.
*/
- try out the example with real hardwares.
- implement "safety" functions e.g: isTooClose() and isTooFar()