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.
Dependencies: mbed WakeUp coapRadioClient DHT11
distanceMeter.cpp
- Committer:
- Ka_myk
- Date:
- 2019-01-25
- Revision:
- 7:2393eb882f82
- Parent:
- 6:5102672e8ff4
File content as of revision 7:2393eb882f82:
#include "distanceMeter.h"
#include "mbed.h"
void DistanceMeter::echoUp() {
timer.start();
}
void DistanceMeter::echoDown() {
timer.stop();
timeUs = timer.read_us();
timer.reset();
timeReady = true;
}
DistanceMeter::DistanceMeter(PinName echoPin, PinName triggerPin): timeReady(false), echo(echoPin), trigger(triggerPin) {
trigger = 0;
}
void DistanceMeter::initialize() {
echo.rise(this, &DistanceMeter::echoUp);
echo.fall(this, &DistanceMeter::echoDown);
}
int DistanceMeter::read() {
trigger = 1;
wait_us(10);
trigger = 0;
while(!timeReady)
wait_us(100);
timeReady = false;
return timeUs * 17 / 1000; // dist in cm, (time * 340) / (2 * 10000)
}
int DistanceMeter::getSensorId() {
return 0;
}