First Draft, serial print change based on distance

Committer:
liam94
Date:
Fri Feb 04 18:30:13 2022 +0000
Revision:
17:02676e9bbc73
Parent:
12:b562893a1445
final update to all comments

Who changed what in which revision?

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