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.
include/SR04.h@6:9eb153e1d472, 2015-08-15 (annotated)
- Committer:
- overkiller
- Date:
- Sat Aug 15 11:47:53 2015 +0000
- Revision:
- 6:9eb153e1d472
- Parent:
- 5:1afe17a20a5b
bugfix
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| overkiller | 5:1afe17a20a5b | 1 | #ifndef SR04_H |
| overkiller | 5:1afe17a20a5b | 2 | #define SR04_H |
| overkiller | 5:1afe17a20a5b | 3 | DigitalOut pinTrigg(PC_10); |
| overkiller | 5:1afe17a20a5b | 4 | DigitalIn pinEcho(PC_12); |
| overkiller | 5:1afe17a20a5b | 5 | const char* distMsg = "Actual distance: %g \n\r"; |
| overkiller | 6:9eb153e1d472 | 6 | float actualDistance = 0; |
| overkiller | 5:1afe17a20a5b | 7 | float pulseIn(DigitalIn pin) |
| overkiller | 5:1afe17a20a5b | 8 | { |
| overkiller | 5:1afe17a20a5b | 9 | Timer durationTimer; |
| overkiller | 5:1afe17a20a5b | 10 | float tempDuration = 0; |
| overkiller | 5:1afe17a20a5b | 11 | while(!pin); //wait for high |
| overkiller | 5:1afe17a20a5b | 12 | durationTimer.start(); |
| overkiller | 5:1afe17a20a5b | 13 | while(pin); //wait for low |
| overkiller | 5:1afe17a20a5b | 14 | tempDuration = durationTimer.read_us(); |
| overkiller | 5:1afe17a20a5b | 15 | return tempDuration; |
| overkiller | 5:1afe17a20a5b | 16 | } |
| overkiller | 5:1afe17a20a5b | 17 | float getDistance(int sensorID) |
| overkiller | 5:1afe17a20a5b | 18 | { |
| overkiller | 5:1afe17a20a5b | 19 | if(sensorID == 0) |
| overkiller | 5:1afe17a20a5b | 20 | { |
| overkiller | 5:1afe17a20a5b | 21 | float duration = 0; |
| overkiller | 5:1afe17a20a5b | 22 | pinTrigg = 0; |
| overkiller | 5:1afe17a20a5b | 23 | wait_us(2); |
| overkiller | 5:1afe17a20a5b | 24 | pinTrigg = 1; |
| overkiller | 5:1afe17a20a5b | 25 | wait_us(10); |
| overkiller | 5:1afe17a20a5b | 26 | pinTrigg = 0; |
| overkiller | 5:1afe17a20a5b | 27 | duration = pulseIn(pinEcho); |
| overkiller | 5:1afe17a20a5b | 28 | duration /= 58; //datasheet! |
| overkiller | 5:1afe17a20a5b | 29 | return duration; |
| overkiller | 5:1afe17a20a5b | 30 | } |
| overkiller | 5:1afe17a20a5b | 31 | return 0; |
| overkiller | 5:1afe17a20a5b | 32 | } |
| overkiller | 5:1afe17a20a5b | 33 | #endif //SR04_H |