Gert Lauritsen / HCSR04

Dependents:   AdrianLysShow AdrianLysShow

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HCSR04.cpp Source File

HCSR04.cpp

00001 #include "HCSR04.h"
00002 #include "mbed.h"
00003 #define DistanceOffset 0
00004 HCSR04::HCSR04(PinName t, PinName e,callback_type _callback) : trig(t), echo(e) 
00005 {
00006  echo.rise(this, &HCSR04::StartTimer); 
00007  echo.fall(this, &HCSR04::DistResult);
00008  pulsWidth=new Timeout; 
00009  callback = _callback;     
00010 }
00011 
00012 void HCSR04::StartTimer() {
00013    timer.reset();
00014    timer.start(); 
00015 }
00016 
00017 void HCSR04::DistResult() {
00018 //Getting the distance
00019  dist_cm =((timer.read_us()-DistanceOffset)*0.034)/2;
00020  callback(dist_cm);   //time the speed of sound
00021 }
00022 
00023 void HCSR04::Trigoff() {
00024 //setting the trigger low    
00025     trig = 0;
00026 }
00027 
00028 void HCSR04::Trigger() {
00029 //Makes a trigger signal
00030     trig = 1;
00031     pulsWidth->attach_us(this,&HCSR04::Trigoff,10);
00032     //Give it 10 us puls
00033 }
00034