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.
Dependents: AdrianLysShow AdrianLysShow
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
Generated on Tue Jul 12 2022 23:13:57 by
1.7.2