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
- Committer:
- overkiller
- Date:
- 2015-08-15
- Revision:
- 6:9eb153e1d472
- Parent:
- 5:1afe17a20a5b
File content as of revision 6:9eb153e1d472:
#ifndef SR04_H
#define SR04_H
DigitalOut pinTrigg(PC_10);
DigitalIn pinEcho(PC_12);
const char* distMsg = "Actual distance: %g \n\r";
float actualDistance = 0;
float pulseIn(DigitalIn pin)
{
Timer durationTimer;
float tempDuration = 0;
while(!pin); //wait for high
durationTimer.start();
while(pin); //wait for low
tempDuration = durationTimer.read_us();
return tempDuration;
}
float getDistance(int sensorID)
{
if(sensorID == 0)
{
float duration = 0;
pinTrigg = 0;
wait_us(2);
pinTrigg = 1;
wait_us(10);
pinTrigg = 0;
duration = pulseIn(pinEcho);
duration /= 58; //datasheet!
return duration;
}
return 0;
}
#endif //SR04_H