Smartage application
Dependencies: BufferedSerial SX1276GenericLib USBDeviceHT mbed Crypto X_NUCLEO_IKS01A2
Fork of STM32L0_LoRa by
hcsr04.cpp@31:6bf5a868695f, 2018-09-17 (annotated)
- Committer:
- marcozecchini
- Date:
- Mon Sep 17 20:42:51 2018 +0000
- Revision:
- 31:6bf5a868695f
- Parent:
- 29:04e1489f8fe2
Bug fixed 3;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 29:04e1489f8fe2 | 1 | #include "hcsr04.h" |
marcozecchini | 29:04e1489f8fe2 | 2 | #include "mbed.h" |
marcozecchini | 29:04e1489f8fe2 | 3 | /* |
marcozecchini | 29:04e1489f8fe2 | 4 | *HCSR04.cpp |
marcozecchini | 29:04e1489f8fe2 | 5 | */ |
marcozecchini | 29:04e1489f8fe2 | 6 | HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {} |
marcozecchini | 29:04e1489f8fe2 | 7 | long HCSR04::echo_duration() { |
marcozecchini | 29:04e1489f8fe2 | 8 | |
marcozecchini | 29:04e1489f8fe2 | 9 | timer.reset(); //reset timer |
marcozecchini | 29:04e1489f8fe2 | 10 | trig=0; // trigger low |
marcozecchini | 29:04e1489f8fe2 | 11 | wait_us(2); // wait |
marcozecchini | 29:04e1489f8fe2 | 12 | trig=1; // trigger high |
marcozecchini | 29:04e1489f8fe2 | 13 | wait_us(10); |
marcozecchini | 29:04e1489f8fe2 | 14 | trig=0; // trigger low |
marcozecchini | 29:04e1489f8fe2 | 15 | while(!echo); // start pulseIN |
marcozecchini | 29:04e1489f8fe2 | 16 | timer.start(); |
marcozecchini | 29:04e1489f8fe2 | 17 | while(echo); |
marcozecchini | 29:04e1489f8fe2 | 18 | timer.stop(); |
marcozecchini | 29:04e1489f8fe2 | 19 | return timer.read_us(); |
marcozecchini | 29:04e1489f8fe2 | 20 | |
marcozecchini | 29:04e1489f8fe2 | 21 | } |
marcozecchini | 31:6bf5a868695f | 22 | |
marcozecchini | 31:6bf5a868695f | 23 | //return distance in cm |
marcozecchini | 31:6bf5a868695f | 24 | long HCSR04::distance(){ |
marcozecchini | 31:6bf5a868695f | 25 | duration = echo_duration(); |
marcozecchini | 31:6bf5a868695f | 26 | distance_cm = (duration/2)/29.1 ; |
marcozecchini | 31:6bf5a868695f | 27 | return distance_cm; |
marcozecchini | 31:6bf5a868695f | 28 | |
marcozecchini | 31:6bf5a868695f | 29 | } |
marcozecchini | 29:04e1489f8fe2 | 30 |