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.
Fork of HC_SR04_Ultrasonic_Library by
ultrasonic.h@6:c0f8b860f55d, 2016-04-30 (annotated)
- Committer:
- knotbeer
- Date:
- Sat Apr 30 05:40:42 2016 +0000
- Revision:
- 6:c0f8b860f55d
- Parent:
- 5:182e800b81f0
API Documentation
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ejteb | 0:6aa04a8c8d4c | 1 | #ifndef MBED_ULTRASONIC_H |
| ejteb | 0:6aa04a8c8d4c | 2 | #define MBED_ULTRASONIC_H |
| ejteb | 0:6aa04a8c8d4c | 3 | |
| ejteb | 0:6aa04a8c8d4c | 4 | #include "mbed.h" |
| ejteb | 0:6aa04a8c8d4c | 5 | |
| knotbeer | 5:182e800b81f0 | 6 | /** ultrasonic class. |
| knotbeer | 5:182e800b81f0 | 7 | * Used for HC-SR04 ultrasonic range sensor |
| knotbeer | 5:182e800b81f0 | 8 | * |
| knotbeer | 5:182e800b81f0 | 9 | * Example: |
| knotbeer | 5:182e800b81f0 | 10 | * @code |
| knotbeer | 5:182e800b81f0 | 11 | * #include "mbed.h" |
| knotbeer | 5:182e800b81f0 | 12 | * #include "ultrasonic.h" |
| knotbeer | 5:182e800b81f0 | 13 | * |
| knotbeer | 5:182e800b81f0 | 14 | * ultrasonic usensor(PA_6, PA_7, 0.1, 0.25, &dist); |
| knotbeer | 5:182e800b81f0 | 15 | * |
| knotbeer | 5:182e800b81f0 | 16 | * int main() { |
| knotbeer | 5:182e800b81f0 | 17 | * //start mesuring the distance |
| knotbeer | 5:182e800b81f0 | 18 | * usensor.startUpdates(); |
| knotbeer | 5:182e800b81f0 | 19 | * while(1){ |
| knotbeer | 5:182e800b81f0 | 20 | * usensor.checkDistance(); |
| knotbeer | 5:182e800b81f0 | 21 | * } |
| knotbeer | 5:182e800b81f0 | 22 | * } |
| knotbeer | 5:182e800b81f0 | 23 | * @endcode |
| knotbeer | 5:182e800b81f0 | 24 | */ |
| ejteb | 0:6aa04a8c8d4c | 25 | class ultrasonic |
| ejteb | 0:6aa04a8c8d4c | 26 | { |
| knotbeer | 5:182e800b81f0 | 27 | public: |
| knotbeer | 5:182e800b81f0 | 28 | /**iniates the class with the specified trigger pin, echo pin, update speed and timeout |
| knotbeer | 5:182e800b81f0 | 29 | * @param trigPin The "trigger" pin for the ultrasonic sensor. This is DigitalOut. |
| knotbeer | 5:182e800b81f0 | 30 | * @param echoPin The "echo" pin for the ultrasonic sensor. This is InterruptIn. |
| knotbeer | 5:182e800b81f0 | 31 | * @param updateSpeed How often the ultrasonic sensor pings and listens for a response. |
| knotbeer | 5:182e800b81f0 | 32 | * @param timeout How long to wait before giving up on listening for a ping. |
| knotbeer | 5:182e800b81f0 | 33 | **/ |
| knotbeer | 5:182e800b81f0 | 34 | ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout); |
| knotbeer | 5:182e800b81f0 | 35 | /**iniates the class with the specified trigger pin, echo pin, update speed, timeout and method to call when the distance changes |
| knotbeer | 5:182e800b81f0 | 36 | * @param trigPin The "trigger" pin for the ultrasonic sensor. This is DigitalOut. |
| knotbeer | 5:182e800b81f0 | 37 | * @param echoPin The "echo" pin for the ultrasonic sensor. This is InterruptIn. |
| knotbeer | 5:182e800b81f0 | 38 | * @param updateSpeed How often the ultrasonic sensor pings and listens for a response. |
| knotbeer | 5:182e800b81f0 | 39 | * @param timeout How long to wait before giving up on listening for a ping. |
| knotbeer | 5:182e800b81f0 | 40 | * @param onUpdate(int) The function to be called when the distance is detected to have changed. |
| knotbeer | 5:182e800b81f0 | 41 | **/ |
| knotbeer | 5:182e800b81f0 | 42 | ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int)); |
| knotbeer | 5:182e800b81f0 | 43 | /** returns the last measured distance**/ |
| knotbeer | 5:182e800b81f0 | 44 | int getCurrentDistance(void); |
| knotbeer | 6:c0f8b860f55d | 45 | /** auses measuring the distance**/ |
| knotbeer | 5:182e800b81f0 | 46 | void pauseUpdates(void); |
| knotbeer | 6:c0f8b860f55d | 47 | /** tarts mesuring the distance**/ |
| knotbeer | 5:182e800b81f0 | 48 | void startUpdates(void); |
| knotbeer | 5:182e800b81f0 | 49 | /**attachs the method to be called when the distances changes |
| knotbeer | 5:182e800b81f0 | 50 | * @param method(int) The function to be called when the distance is detected to have changed. |
| knotbeer | 5:182e800b81f0 | 51 | */ |
| knotbeer | 5:182e800b81f0 | 52 | void attachOnUpdate(void method(int)); |
| knotbeer | 5:182e800b81f0 | 53 | /** |
| knotbeer | 5:182e800b81f0 | 54 | * Method: |
| knotbeer | 5:182e800b81f0 | 55 | * changes the speed at which updates are made |
| knotbeer | 5:182e800b81f0 | 56 | * @param updateSpeed how often to update the ultrasonic sensor |
| knotbeer | 5:182e800b81f0 | 57 | **/ |
| knotbeer | 5:182e800b81f0 | 58 | void changeUpdateSpeed(float updateSpeed); |
| knotbeer | 5:182e800b81f0 | 59 | /**gets whether the distance has been changed since the last call of isUpdated() or checkDistance()**/ |
| knotbeer | 5:182e800b81f0 | 60 | int isUpdated(void); |
| knotbeer | 5:182e800b81f0 | 61 | /**gets the speed at which updates are made**/ |
| knotbeer | 5:182e800b81f0 | 62 | float getUpdateSpeed(void); |
| knotbeer | 5:182e800b81f0 | 63 | /**call this as often as possible in your code, eg. at the end of a while(1) loop, |
| knotbeer | 5:182e800b81f0 | 64 | and it will check whether the method you have attached needs to be called**/ |
| knotbeer | 5:182e800b81f0 | 65 | void checkDistance(void); |
| knotbeer | 5:182e800b81f0 | 66 | private: |
| knotbeer | 5:182e800b81f0 | 67 | DigitalOut _trig; |
| knotbeer | 5:182e800b81f0 | 68 | InterruptIn _echo; |
| knotbeer | 5:182e800b81f0 | 69 | Timer _t; |
| knotbeer | 5:182e800b81f0 | 70 | Timeout _tout; |
| knotbeer | 5:182e800b81f0 | 71 | int _distance; |
| knotbeer | 5:182e800b81f0 | 72 | float _updateSpeed; |
| knotbeer | 5:182e800b81f0 | 73 | int start; |
| knotbeer | 5:182e800b81f0 | 74 | int end; |
| knotbeer | 5:182e800b81f0 | 75 | volatile int done; |
| knotbeer | 5:182e800b81f0 | 76 | void (*_onUpdateMethod)(int); |
| knotbeer | 5:182e800b81f0 | 77 | void _startT(void); |
| knotbeer | 5:182e800b81f0 | 78 | void _updateDist(void); |
| knotbeer | 5:182e800b81f0 | 79 | void _startTrig(void); |
| knotbeer | 5:182e800b81f0 | 80 | float _timeout; |
| knotbeer | 5:182e800b81f0 | 81 | int d; |
| ejteb | 0:6aa04a8c8d4c | 82 | }; |
| ejteb | 0:6aa04a8c8d4c | 83 | #endif |
