Cheap ultrasonic range finder works with interrupts. Fork of original https://os.mbed.com/components/HC-SR04/ and update for MbedOS6+

Dependents:   Nucleo_UltrasonicHelloWorld xxx_Sonar-HC-SR04_Lsg

Committer:
JohnnyK
Date:
Thu Jun 10 18:53:38 2021 +0000
Revision:
6:9808e2c61247
Parent:
5:6fd0e9c7ead4
add example into .h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ejteb 0:6aa04a8c8d4c 1 #ifndef MBED_ULTRASONIC_H
ejteb 0:6aa04a8c8d4c 2 #define MBED_ULTRASONIC_H
ejteb 0:6aa04a8c8d4c 3
JohnnyK 6:9808e2c61247 4 /**
JohnnyK 6:9808e2c61247 5 @code
JohnnyK 6:9808e2c61247 6
JohnnyK 6:9808e2c61247 7 #include "mbed.h"
JohnnyK 6:9808e2c61247 8 #include "ultrasonic.h"
JohnnyK 6:9808e2c61247 9
JohnnyK 6:9808e2c61247 10 void dist(int distance)
JohnnyK 6:9808e2c61247 11 {
JohnnyK 6:9808e2c61247 12 //put code here to happen when the distance is changed
JohnnyK 6:9808e2c61247 13 printf("Distance changed to %dmm\r\n", distance);
JohnnyK 6:9808e2c61247 14 }
JohnnyK 6:9808e2c61247 15
JohnnyK 6:9808e2c61247 16 ultrasonic mu(D8, D9, 100ms, 1s, &dist); //Set the trigger pin to D8 and the echo pin to D9
JohnnyK 6:9808e2c61247 17 //have updates every .1 seconds and a timeout after 1
JohnnyK 6:9808e2c61247 18 //second, and call dist when the distance changes
JohnnyK 6:9808e2c61247 19
JohnnyK 6:9808e2c61247 20 int main()
JohnnyK 6:9808e2c61247 21 {
JohnnyK 6:9808e2c61247 22 mu.startUpdates();//start mesuring the distance
JohnnyK 6:9808e2c61247 23 while(1)
JohnnyK 6:9808e2c61247 24 {
JohnnyK 6:9808e2c61247 25 //Do something else here
JohnnyK 6:9808e2c61247 26 mu.checkDistance(); //call checkDistance() as much as possible, as this is where
JohnnyK 6:9808e2c61247 27 //the class checks if dist needs to be called.
JohnnyK 6:9808e2c61247 28 }
JohnnyK 6:9808e2c61247 29 }
JohnnyK 6:9808e2c61247 30
JohnnyK 6:9808e2c61247 31 @endcode
JohnnyK 6:9808e2c61247 32 */
JohnnyK 6:9808e2c61247 33
ejteb 0:6aa04a8c8d4c 34 #include "mbed.h"
ejteb 0:6aa04a8c8d4c 35
ejteb 0:6aa04a8c8d4c 36 class ultrasonic
ejteb 0:6aa04a8c8d4c 37 {
ejteb 0:6aa04a8c8d4c 38 public:
ejteb 2:cc1143d36567 39 /**iniates the class with the specified trigger pin, echo pin, update speed and timeout**/
JohnnyK 5:6fd0e9c7ead4 40 ultrasonic(PinName trigPin, PinName echoPin, Kernel::Clock::duration_u32 updateSpeed, Kernel::Clock::duration_u32 timeout);
ejteb 2:cc1143d36567 41 /**iniates the class with the specified trigger pin, echo pin, update speed, timeout and method to call when the distance changes**/
JohnnyK 5:6fd0e9c7ead4 42 ultrasonic(PinName trigPin, PinName echoPin, Kernel::Clock::duration_u32 updateSpeed, Kernel::Clock::duration_u32 timeout, void onUpdate(int));
ejteb 2:cc1143d36567 43 /** returns the last measured distance**/
ejteb 0:6aa04a8c8d4c 44 int getCurrentDistance(void);
ejteb 2:cc1143d36567 45 /**pauses measuring the distance**/
ejteb 0:6aa04a8c8d4c 46 void pauseUpdates(void);
ejteb 2:cc1143d36567 47 /**starts mesuring the distance**/
ejteb 0:6aa04a8c8d4c 48 void startUpdates(void);
ejteb 2:cc1143d36567 49 /**attachs the method to be called when the distances changes**/
ejteb 0:6aa04a8c8d4c 50 void attachOnUpdate(void method(int));
ejteb 2:cc1143d36567 51 /**changes the speed at which updates are made**/
JohnnyK 5:6fd0e9c7ead4 52 void changeUpdateSpeed(Kernel::Clock::duration_u32 updateSpeed);
ejteb 2:cc1143d36567 53 /**gets whether the distance has been changed since the last call of isUpdated() or checkDistance()**/
ejteb 0:6aa04a8c8d4c 54 int isUpdated(void);
ejteb 2:cc1143d36567 55 /**gets the speed at which updates are made**/
JohnnyK 5:6fd0e9c7ead4 56 Kernel::Clock::duration_u32 getUpdateSpeed(void);
ejteb 2:cc1143d36567 57 /**call this as often as possible in your code, eg. at the end of a while(1) loop,
ejteb 2:cc1143d36567 58 and it will check whether the method you have attached needs to be called**/
ejteb 2:cc1143d36567 59 void checkDistance(void);
ejteb 0:6aa04a8c8d4c 60 private:
ejteb 0:6aa04a8c8d4c 61 DigitalOut _trig;
ejteb 0:6aa04a8c8d4c 62 InterruptIn _echo;
ejteb 0:6aa04a8c8d4c 63 Timer _t;
ejteb 0:6aa04a8c8d4c 64 Timeout _tout;
ejteb 0:6aa04a8c8d4c 65 int _distance;
JohnnyK 5:6fd0e9c7ead4 66 Kernel::Clock::duration_u32 _updateSpeed;
ejteb 0:6aa04a8c8d4c 67 int start;
ejteb 0:6aa04a8c8d4c 68 int end;
ejteb 0:6aa04a8c8d4c 69 volatile int done;
ejteb 0:6aa04a8c8d4c 70 void (*_onUpdateMethod)(int);
ejteb 0:6aa04a8c8d4c 71 void _startT(void);
ejteb 0:6aa04a8c8d4c 72 void _updateDist(void);
ejteb 0:6aa04a8c8d4c 73 void _startTrig(void);
JohnnyK 5:6fd0e9c7ead4 74 Kernel::Clock::duration_u32 _timeout;
ejteb 0:6aa04a8c8d4c 75 int d;
ejteb 0:6aa04a8c8d4c 76 };
ejteb 0:6aa04a8c8d4c 77 #endif