basic class for the use of the HC-SR04 ultrasound sensor
HC-SR04.cpp@0:b4a6f6bcab30, 2021-10-31 (annotated)
- Committer:
- snapo
- Date:
- Sun Oct 31 10:46:33 2021 +0000
- Revision:
- 0:b4a6f6bcab30
basic class for use of the HC-SR04 ultrasound sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
snapo | 0:b4a6f6bcab30 | 1 | #include "HC-SR04.h" |
snapo | 0:b4a6f6bcab30 | 2 | |
snapo | 0:b4a6f6bcab30 | 3 | |
snapo | 0:b4a6f6bcab30 | 4 | |
snapo | 0:b4a6f6bcab30 | 5 | HCSR04::HCSR04 (PinName echo, PinName trigger): |
snapo | 0:b4a6f6bcab30 | 6 | echo_(echo), trigger_(trigger, PIN_OUTPUT, OpenDrain, 0){}; |
snapo | 0:b4a6f6bcab30 | 7 | |
snapo | 0:b4a6f6bcab30 | 8 | void HCSR04::reading(){ |
snapo | 0:b4a6f6bcab30 | 9 | t_.reset(); |
snapo | 0:b4a6f6bcab30 | 10 | |
snapo | 0:b4a6f6bcab30 | 11 | trigger_ = true; |
snapo | 0:b4a6f6bcab30 | 12 | wait_us(10); |
snapo | 0:b4a6f6bcab30 | 13 | trigger_ = false; |
snapo | 0:b4a6f6bcab30 | 14 | |
snapo | 0:b4a6f6bcab30 | 15 | //waits for the echo pin to activate then starts to measure time |
snapo | 0:b4a6f6bcab30 | 16 | while(!echo_); |
snapo | 0:b4a6f6bcab30 | 17 | t_.start(); |
snapo | 0:b4a6f6bcab30 | 18 | while(echo_); |
snapo | 0:b4a6f6bcab30 | 19 | //time stops being measured once echo is deactivated. |
snapo | 0:b4a6f6bcab30 | 20 | t_.stop(); |
snapo | 0:b4a6f6bcab30 | 21 | //time measured in microseconds to increase the accuracy then the distance is scaled up into mm. |
snapo | 0:b4a6f6bcab30 | 22 | time_us_ = duration_cast<std::chrono::microseconds>(t_.elapsed_time()).count(); |
snapo | 0:b4a6f6bcab30 | 23 | //distance stored in baseDistance_ variable |
snapo | 0:b4a6f6bcab30 | 24 | distance_ = (160*time_us_) / 1000; |
snapo | 0:b4a6f6bcab30 | 25 | thread_sleep_for(1000); |
snapo | 0:b4a6f6bcab30 | 26 | } |
snapo | 0:b4a6f6bcab30 | 27 | |
snapo | 0:b4a6f6bcab30 | 28 | void HCSR04::fastTimeReading(){ |
snapo | 0:b4a6f6bcab30 | 29 | t_.reset(); |
snapo | 0:b4a6f6bcab30 | 30 | |
snapo | 0:b4a6f6bcab30 | 31 | trigger_ = true; |
snapo | 0:b4a6f6bcab30 | 32 | wait_us(10); |
snapo | 0:b4a6f6bcab30 | 33 | trigger_ = false; |
snapo | 0:b4a6f6bcab30 | 34 | |
snapo | 0:b4a6f6bcab30 | 35 | //waits for the echo pin to activate then starts to measure time |
snapo | 0:b4a6f6bcab30 | 36 | while(!echo_); |
snapo | 0:b4a6f6bcab30 | 37 | t_.start(); |
snapo | 0:b4a6f6bcab30 | 38 | while(echo_); |
snapo | 0:b4a6f6bcab30 | 39 | //time stops being measured once echo is deactivated. |
snapo | 0:b4a6f6bcab30 | 40 | t_.stop(); |
snapo | 0:b4a6f6bcab30 | 41 | time_us_ = duration_cast<std::chrono::microseconds>(t_.elapsed_time()).count(); |
snapo | 0:b4a6f6bcab30 | 42 | |
snapo | 0:b4a6f6bcab30 | 43 | } |
snapo | 0:b4a6f6bcab30 | 44 | |
snapo | 0:b4a6f6bcab30 | 45 | int HCSR04::getTime(){ |
snapo | 0:b4a6f6bcab30 | 46 | return time_us_; |
snapo | 0:b4a6f6bcab30 | 47 | } |
snapo | 0:b4a6f6bcab30 | 48 | |
snapo | 0:b4a6f6bcab30 | 49 | int HCSR04::getDistance(){ |
snapo | 0:b4a6f6bcab30 | 50 | return distance_; |
snapo | 0:b4a6f6bcab30 | 51 | } |
snapo | 0:b4a6f6bcab30 | 52 |