Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HCSR04/hcsr04.cpp@0:0f497d629677, 2020-11-19 (annotated)
- Committer:
- grupo_17_2020
- Date:
- Thu Nov 19 12:07:27 2020 +0000
- Revision:
- 0:0f497d629677
el codigo completo del Asistente de Servicio Personal Interaccional - 01
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| grupo_17_2020 | 0:0f497d629677 | 1 | #include "hcsr04.h" |
| grupo_17_2020 | 0:0f497d629677 | 2 | #include "mbed.h" |
| grupo_17_2020 | 0:0f497d629677 | 3 | /* |
| grupo_17_2020 | 0:0f497d629677 | 4 | *HCSR04.cpp |
| grupo_17_2020 | 0:0f497d629677 | 5 | */ |
| grupo_17_2020 | 0:0f497d629677 | 6 | HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {} |
| grupo_17_2020 | 0:0f497d629677 | 7 | long HCSR04::echo_duration() { |
| grupo_17_2020 | 0:0f497d629677 | 8 | |
| grupo_17_2020 | 0:0f497d629677 | 9 | timer.reset(); //reset timer |
| grupo_17_2020 | 0:0f497d629677 | 10 | trig=0; // trigger low |
| grupo_17_2020 | 0:0f497d629677 | 11 | wait_us(2); // wait |
| grupo_17_2020 | 0:0f497d629677 | 12 | trig=1; // trigger high |
| grupo_17_2020 | 0:0f497d629677 | 13 | wait_us(10); |
| grupo_17_2020 | 0:0f497d629677 | 14 | trig=0; // trigger low |
| grupo_17_2020 | 0:0f497d629677 | 15 | while(!echo); // start pulseIN |
| grupo_17_2020 | 0:0f497d629677 | 16 | timer.start(); |
| grupo_17_2020 | 0:0f497d629677 | 17 | while(echo); |
| grupo_17_2020 | 0:0f497d629677 | 18 | timer.stop(); |
| grupo_17_2020 | 0:0f497d629677 | 19 | return timer.read_us(); |
| grupo_17_2020 | 0:0f497d629677 | 20 | |
| grupo_17_2020 | 0:0f497d629677 | 21 | } |
| grupo_17_2020 | 0:0f497d629677 | 22 | |
| grupo_17_2020 | 0:0f497d629677 | 23 | //return distance in cm |
| grupo_17_2020 | 0:0f497d629677 | 24 | long HCSR04::distance(){ |
| grupo_17_2020 | 0:0f497d629677 | 25 | duration = echo_duration(); |
| grupo_17_2020 | 0:0f497d629677 | 26 | distance_cm = (duration/2)/29.1 ; |
| grupo_17_2020 | 0:0f497d629677 | 27 | return distance_cm; |
| grupo_17_2020 | 0:0f497d629677 | 28 | |
| grupo_17_2020 | 0:0f497d629677 | 29 | } |