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.cpp@1:067e3b441952, 2019-05-06 (annotated)
- Committer:
- marvas
- Date:
- Mon May 06 12:32:07 2019 +0000
- Revision:
- 1:067e3b441952
- Parent:
- 0:86b2086be101
hcsr04 ultrasonik sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
antoniolinux | 0:86b2086be101 | 1 | #include "hcsr04.h" |
antoniolinux | 0:86b2086be101 | 2 | #include "mbed.h" |
antoniolinux | 0:86b2086be101 | 3 | /* |
antoniolinux | 0:86b2086be101 | 4 | *HCSR04.cpp |
antoniolinux | 0:86b2086be101 | 5 | */ |
antoniolinux | 0:86b2086be101 | 6 | HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {} |
marvas | 1:067e3b441952 | 7 | int HCSR04::echo_sure() { |
antoniolinux | 0:86b2086be101 | 8 | |
antoniolinux | 0:86b2086be101 | 9 | timer.reset(); //reset timer |
antoniolinux | 0:86b2086be101 | 10 | trig=0; // trigger low |
antoniolinux | 0:86b2086be101 | 11 | wait_us(2); // wait |
antoniolinux | 0:86b2086be101 | 12 | trig=1; // trigger high |
antoniolinux | 0:86b2086be101 | 13 | wait_us(10); |
antoniolinux | 0:86b2086be101 | 14 | trig=0; // trigger low |
antoniolinux | 0:86b2086be101 | 15 | while(!echo); // start pulseIN |
antoniolinux | 0:86b2086be101 | 16 | timer.start(); |
antoniolinux | 0:86b2086be101 | 17 | while(echo); |
antoniolinux | 0:86b2086be101 | 18 | timer.stop(); |
antoniolinux | 0:86b2086be101 | 19 | return timer.read_us(); |
antoniolinux | 0:86b2086be101 | 20 | |
antoniolinux | 0:86b2086be101 | 21 | } |
antoniolinux | 0:86b2086be101 | 22 | |
antoniolinux | 0:86b2086be101 | 23 | //return distance in cm |
marvas | 1:067e3b441952 | 24 | int HCSR04::mesafe(){ |
marvas | 1:067e3b441952 | 25 | sure = echo_sure(); |
marvas | 1:067e3b441952 | 26 | mesafe_cm = (sure/2)/29.1 ; |
marvas | 1:067e3b441952 | 27 | return mesafe_cm; |
antoniolinux | 0:86b2086be101 | 28 | |
antoniolinux | 0:86b2086be101 | 29 | } |