First Draft, serial print change based on distance
Embed:
(wiki syntax)
Show/hide line numbers
ultrasonic.h
00001 #ifndef ULTRASONIC_H 00002 #define ULTRASONIC_H 00003 00004 #include "mbed.h" 00005 00006 /** Ultrasonic Class 00007 @brief Acknowledgements to EJ Teb 00008 @brief Library for interfacing with ultrasonic sensor 00009 00010 00011 Example: 00012 00013 @code 00014 00015 #include "mbed.h" 00016 #include "ultrasonic.h" 00017 00018 void dist(int distance) 00019 { 00020 printf("Distance changed to %dmm\r\n", distance); 00021 00022 } 00023 00024 00025 ultrasonic mu(PTD0, PTC12, .1, 1, &dist); //Set the trigger pin to PTD0 and the echo pin to PTC12 00026 //have updates every .1 seconds and a timeout after 1 00027 //second, and call dist when the distance changes 00028 00029 int main() 00030 { 00031 mu.startUpdates();//start mesuring the distance 00032 while(1) 00033 { 00034 //Do something else here 00035 mu.checkDistance(); //call checkDistance() as much as possible, as this is where 00036 //the class checks if dist needs to be called. 00037 } 00038 } 00039 00040 00041 * @endcode 00042 */ 00043 class ultrasonic 00044 { 00045 public: 00046 /**iniates the class with the specified trigger pin, echo pin, update speed and timeout**/ 00047 ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout); 00048 /**iniates the class with the specified trigger pin, echo pin, update speed, timeout and method to call when the distance changes**/ 00049 ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int)); 00050 /** returns the last measured distance**/ 00051 int getCurrentDistance(void); 00052 /**pauses measuring the distance**/ 00053 void pauseUpdates(void); 00054 /**starts mesuring the distance**/ 00055 void startUpdates(void); 00056 /**attachs the method to be called when the distances changes**/ 00057 void attachOnUpdate(void method(int)); 00058 /**changes the speed at which updates are made**/ 00059 void changeUpdateSpeed(float updateSpeed); 00060 /**gets whether the distance has been changed since the last call of isUpdated() or checkDistance()**/ 00061 int isUpdated(void); 00062 /**gets the speed at which updates are made**/ 00063 float getUpdateSpeed(void); 00064 /**call this as often as possible in your code, eg. at the end of a while(1) loop, 00065 and it will check whether the method you have attached needs to be called**/ 00066 void checkDistance(void); 00067 private: 00068 DigitalOut _trig; 00069 InterruptIn _echo; 00070 Timer _t; 00071 Timeout _tout; 00072 int _distance; 00073 float _updateSpeed; 00074 int start; 00075 int end; 00076 volatile int done; 00077 void (*_onUpdateMethod)(int); 00078 void _startT(void); 00079 void _updateDist(void); 00080 void _startTrig(void); 00081 float _timeout; 00082 int d; 00083 }; 00084 #endif
Generated on Mon Aug 22 2022 16:53:08 by
1.7.2