-
Notifications
You must be signed in to change notification settings - Fork 0
/
DectectionManager.h
44 lines (40 loc) · 1.02 KB
/
DectectionManager.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
37
38
39
40
41
42
43
44
#pragma once
#include "Constants.h"
/**
* Class for managing distance detection using HC-SR04 ultrasonic sensor
*/
class DectectionManager {
public:
/**
* Pinout for the trigger pin of the HC-SR04 sensor
*/
const int TRIGGER_PIN = TRIGGER_PIN_NUMBER;
/**
* Pinout for the echo pin of the HC-SR04 sensor
*/
const int ECHO_PIN = ECHO_PIN_NUMBER;
public:
/**
* Initializes the trigger and echo pins for distance detection.
* Must be called inside the Setup function
*/
void Init() {
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
/**
* Returns the distance measured by the sensor in cm
* @return the distance in cm
*/
float GetDistance() {
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = (duration / 2) / SPEED_OF_SOUND; // Using speed of sound as 29.3
Serial.println(distance);
return distance;
}
};