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@5:1afe17a20a5b, 2015-08-13 (annotated)
- Committer:
 - overkiller
 - Date:
 - Thu Aug 13 15:52:19 2015 +0000
 - Revision:
 - 5:1afe17a20a5b
 - Child:
 - 6:9eb153e1d472
 
sensor code
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 | 5:1afe17a20a5b | 6 | float pulseIn(DigitalIn pin) | 
| overkiller | 5:1afe17a20a5b | 7 | { | 
| overkiller | 5:1afe17a20a5b | 8 | Timer durationTimer; | 
| overkiller | 5:1afe17a20a5b | 9 | float tempDuration = 0; | 
| overkiller | 5:1afe17a20a5b | 10 | while(!pin); //wait for high | 
| overkiller | 5:1afe17a20a5b | 11 | durationTimer.start(); | 
| overkiller | 5:1afe17a20a5b | 12 | while(pin); //wait for low | 
| overkiller | 5:1afe17a20a5b | 13 | tempDuration = durationTimer.read_us(); | 
| overkiller | 5:1afe17a20a5b | 14 | return tempDuration; | 
| overkiller | 5:1afe17a20a5b | 15 | } | 
| overkiller | 5:1afe17a20a5b | 16 | float getDistance(int sensorID) | 
| overkiller | 5:1afe17a20a5b | 17 | { | 
| overkiller | 5:1afe17a20a5b | 18 | if(sensorID == 0) | 
| overkiller | 5:1afe17a20a5b | 19 | { | 
| overkiller | 5:1afe17a20a5b | 20 | float duration = 0; | 
| overkiller | 5:1afe17a20a5b | 21 | pinTrigg = 0; | 
| overkiller | 5:1afe17a20a5b | 22 | wait_us(2); | 
| overkiller | 5:1afe17a20a5b | 23 | pinTrigg = 1; | 
| overkiller | 5:1afe17a20a5b | 24 | wait_us(10); | 
| overkiller | 5:1afe17a20a5b | 25 | pinTrigg = 0; | 
| overkiller | 5:1afe17a20a5b | 26 | duration = pulseIn(pinEcho); | 
| overkiller | 5:1afe17a20a5b | 27 | duration /= 58; //datasheet! | 
| overkiller | 5:1afe17a20a5b | 28 | return duration; | 
| overkiller | 5:1afe17a20a5b | 29 | } | 
| overkiller | 5:1afe17a20a5b | 30 | return 0; | 
| overkiller | 5:1afe17a20a5b | 31 | } | 
| overkiller | 5:1afe17a20a5b | 32 | #endif //SR04_H |