Libreria para usar sensor MaxSonar EZ01 con PulseWidth
Revision 0:de1c628c1495, committed 2014-03-27
- Comitter:
- migool
- Date:
- Thu Mar 27 23:29:50 2014 +0000
- Commit message:
- Libreria para usar Sensor MaxSonar EZ01 con PulseWidth
Changed in this revision
MaxSonar.cpp | Show annotated file Show diff for this revision Revisions of this file |
MaxSonar.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r de1c628c1495 MaxSonar.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MaxSonar.cpp Thu Mar 27 23:29:50 2014 +0000 @@ -0,0 +1,29 @@ +#include "MaxSonar.h" +#include "mbed.h" +Timer us; + +MaxSonar::MaxSonar(PinName pin):_pin(pin){ +} + +void MaxSonar::up(){ + us.start(); + } +void MaxSonar::down(){ + us.stop(); + } +float MaxSonar::pulse(){ + _pin.rise(this,&MaxSonar::up); + _pin.fall(this,&MaxSonar::down); + float time=us.read(); + return time; + } +float MaxSonar::getinches(){ + float in=MaxSonar::pulse()/0.0058; + us.reset(); + return in; + } +float MaxSonar::getcm(){ + float cm=MaxSonar::pulse()/0.0058*2.54; + us.reset(); + return cm; + }
diff -r 000000000000 -r de1c628c1495 MaxSonar.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MaxSonar.h Thu Mar 27 23:29:50 2014 +0000 @@ -0,0 +1,18 @@ +#ifndef MBED_MAXSONAR_H +#define MBED_MAXSONAR_H + +#include "mbed.h" + +class MaxSonar{ +public: + MaxSonar(PinName pin); + float getinches(); + float getcm(); +private: + InterruptIn _pin; + void up(); + void down(); + float pulse(); +}; + +#endif \ No newline at end of file