A library for the HCSR04 sensor. work with interrupt
Dependents: AdrianLysShow AdrianLysShow
A small library for the HCSR04 sensor. works with interrupt on the egde of the echo
link to datasheet: http://www.micropik.com/PDF/HCSR04.pdf
Revision 1:e3a37f4015da, committed 2015-04-29
- Comitter:
- gert_lauritsen
- Date:
- Wed Apr 29 06:12:22 2015 +0000
- Parent:
- 0:9f4365d41bf1
- Commit message:
- Corrected the calfaktor
Changed in this revision
| HCSR04.cpp | Show annotated file Show diff for this revision Revisions of this file |
| HCSR04.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/HCSR04.cpp Tue Apr 28 14:46:20 2015 +0000
+++ b/HCSR04.cpp Wed Apr 29 06:12:22 2015 +0000
@@ -1,6 +1,6 @@
#include "HCSR04.h"
#include "mbed.h"
-#define DistanceOffset 180
+#define DistanceOffset 0
HCSR04::HCSR04(PinName t, PinName e,callback_type _callback) : trig(t), echo(e)
{
echo.rise(this, &HCSR04::StartTimer);
@@ -16,11 +16,12 @@
void HCSR04::DistResult() {
//Getting the distance
- dist_cm =timer.read_us()-DistanceOffset;
- callback(dist_cm*0.034); //time the speed of sound
+ dist_cm =((timer.read_us()-DistanceOffset)*0.034)/2;
+ callback(dist_cm); //time the speed of sound
}
void HCSR04::Trigoff() {
+//setting the trigger low
trig = 0;
}
--- a/HCSR04.h Tue Apr 28 14:46:20 2015 +0000
+++ b/HCSR04.h Wed Apr 29 06:12:22 2015 +0000
@@ -2,9 +2,8 @@
//Class for measure distance with the HCSR04 sensor
/*
Eksemple
-void distance(long Duration) {
- // long distance = sensor.distance(1);
- printf("Distance: %d \r",Duration);
+void distance(long Distance) {
+ printf("Distance: %d \r",Distance);
}
int main()
@@ -17,6 +16,7 @@
}
}
+Datasheet: http://www.micropik.com/PDF/HCSR04.pdf
*/
#include "mbed.h"
typedef void (*callback_type)(long);
Gert Lauritsen