Code Monkey home page Code Monkey logo

arduino-hc-sr04-library's Introduction

Arduino HC-SR04 library

An arduino library to handle HC-SR04 distance sensor.

Basic useage

#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
}

Installion

  • 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"

Functions

Initialize
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
 */
readDisctance
unsigned short readDisctance();

/*
 *  Read disctance
 *  Returns:
 *      the disctance in mm.
 */
readDisctanceInCm
unsigned short readDisctanceInCm();

/*
 *  Read disctance in cm
 *  Returns:
 *      the disctance in cm
 *      (rounded value from mm value
 *      with mathematically corrent rounding).
 */
readAvgDisctanceInMm
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.
 */
readAvgDisctanceInCm
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).
 */
setCalibration
void setCalibration(double calibrationMultiplier);

/*
 *  Set calibration multiplier
 *  Default is 1.0
 */
getCalibration
double getCalibration();

/*
 *  Returns:
 *      Current calibration multiplier.
 */
setOffset
void setOffset(long offsetValueInMm);

/*
 *  Set offset for disctance measurement.
 *  Default is 0 mm.
 */
getOffset
long getOffset();

/*
 *  Returns:
 *      Current ofset offset for disctance measurement.
 */
setDelayBetweenAvgMeasurementsInMs
void setDelayBetweenAvgMeasurementsInMs(unsigned short delayInMs);

/*
 * Returns the average of measured disctance in cm.
 * Parameter:
 *     delayInMs:
 */
readAccurateDisctanceInMm
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.
 */
readAccurateDisctanceInCm
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.
 */

ToDo list:

  • try out the example with real hardwares.
  • implement "safety" functions e.g: isTooClose() and isTooFar()

arduino-hc-sr04-library's People

Contributors

bbkbarbar avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

arduino-hc-sr04-library's Issues

returns 0 after first measurement

Hello,
I am using example sketch and after first measurement it returns 0.
First measurement is normal.
If I use basic hcsr04 code manually it reads normal also.
What could be the cause?
I use wemos d1 mini

Doc for calibration

Would you be able to expand on the doc for using the calibration functions.
I still get spurious readings even though I'm using the function that drops high and low values and averages the rest.

If you get chance references to Dicstance need updating to Distance!

Thanks for your work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.