-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHCSR04.h
36 lines (33 loc) · 1.27 KB
/
HCSR04.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
HCSR04.h - Library for interacting with HCSR04 sensor.
Created by Dirk Köhler.
*/
#ifndef HCSR04_h
#define HCSR04_h
#include "Arduino.h"
class HCSR04
{
public:
HCSR04 (byte triggerPin, byte sensorPin);
//max measureable distance of sensor in mm
const int _sensorMaxDistance = 3000;
//min measureable distance of sensor in mm
const int _sensorMinDistance = 30;
//time for one measurement in microsenconds
const int _measurementInterval = 20;
//get distance in mm, optional prameter can deactivate average measurement, count of measurements for avarage calculation can be set
unsigned int getDistance(boolean average = true, int countAverageMeasurements = 10);
//check if an obstacle is in given range, optional prameter can deactivate average measurement, count of measurements for avarage calculation can be set
boolean getObstacle(int range, boolean average = true, int countAverageMeasurements = 10);
private:
//sonic speed in mm/microsecond
const float sonicSpeed = 0.34;
byte _triggerPin;
byte _sensorPin;
//timeout in microseconds mor one single measurement
unsigned long _echoTimeout;
unsigned long _echoTime;
unsigned long readEchoTime();
unsigned int getAverage(int countAverageMeasurements);
};
#endif