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: test_ultrasonic AEB Car_Simulator
Ultrasonic.cpp
00001 #include "mbed.h" 00002 #include "Ultrasonic.h" 00003 00004 00005 Ultrasonic::Ultrasonic(PinName trigger, PinName echo): _trigger(trigger) , _echo(echo) 00006 { 00007 Ultrasonic_init(); 00008 } 00009 00010 int Ultrasonic::read_cm() 00011 { 00012 return DistanceCM; 00013 } 00014 00015 void Ultrasonic::start() 00016 { 00017 timer.start(); 00018 } 00019 00020 void Ultrasonic::stop() 00021 { 00022 DistanceCM = int(timer.read_us()/58); 00023 timer.stop(); 00024 timer.reset(); 00025 timeout.detach(); 00026 } 00027 00028 void Ultrasonic::timeout_err() 00029 { 00030 // Timeout elapsed -> there is some problem 00031 // out the distance to zero to highligth error 00032 00033 DistanceCM = 0; 00034 } 00035 00036 void Ultrasonic::trig() 00037 { 00038 timeout.attach(this, &Ultrasonic::timeout_err, 0.05); 00039 _trigger = 1; 00040 wait_us(500); 00041 _trigger = 0; 00042 } 00043 00044 void Ultrasonic::Ultrasonic_init() 00045 { 00046 DistanceCM = 0; 00047 timer.reset(); 00048 _echo.rise(this,&Ultrasonic::start); 00049 _echo.fall(this,&Ultrasonic::stop); 00050 tick.attach(this,&Ultrasonic::trig,0.06); 00051 }
Generated on Tue Jul 12 2022 20:50:40 by
1.7.2