-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathultrasonicDriver.cpp
More file actions
30 lines (28 loc) · 804 Bytes
/
Copy pathultrasonicDriver.cpp
File metadata and controls
30 lines (28 loc) · 804 Bytes
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
#include "ultrasonic_driver.h"
void UltrasonicDriver::Init() {
pinMode(ECHO_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
}
void UltrasonicDriver::GetDistance(uint16_t &distance) {
unsigned int pulseWidth = 0;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
pulseWidth = ((unsigned int)pulseIn(ECHO_PIN, HIGH) / 58);
distance = pulseWidth;
}
void UltrasonicDriver::Test() {
unsigned int pulseWidth;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
pulseWidth = ((unsigned int)pulseIn(ECHO_PIN, HIGH) / 58);
Serial.print("ULTRASONIC=");
Serial.print(pulseWidth);
Serial.println(" cm");
delay(60);
}