For CW1
Dependencies: mbed HC_SR04_Ultrasonic_Library
main.cpp@0:4fbd246540da, 2021-09-26 (annotated)
- Committer:
- michaelveloo
- Date:
- Sun Sep 26 10:48:03 2021 +0000
- Revision:
- 0:4fbd246540da
CW1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaelveloo | 0:4fbd246540da | 1 | #include "mbed.h" |
michaelveloo | 0:4fbd246540da | 2 | #include "ultrasonic.h" |
michaelveloo | 0:4fbd246540da | 3 | void dist(int distance) |
michaelveloo | 0:4fbd246540da | 4 | { |
michaelveloo | 0:4fbd246540da | 5 | //put code here to happen when the distance is changed |
michaelveloo | 0:4fbd246540da | 6 | printf("Distance changed to %dmm\r\n", distance); |
michaelveloo | 0:4fbd246540da | 7 | } |
michaelveloo | 0:4fbd246540da | 8 | ultrasonic mu(p8, p9, .1, 1, &dist); //Set the trigger pin to p8 and the echo pin to p9 |
michaelveloo | 0:4fbd246540da | 9 | //have updates every .1 seconds and a timeout after 1 |
michaelveloo | 0:4fbd246540da | 10 | //second, and call dist when the distance changes |
michaelveloo | 0:4fbd246540da | 11 | int main() |
michaelveloo | 0:4fbd246540da | 12 | { |
michaelveloo | 0:4fbd246540da | 13 | mu.startUpdates();//start mesuring the distance |
michaelveloo | 0:4fbd246540da | 14 | while(1) |
michaelveloo | 0:4fbd246540da | 15 | { |
michaelveloo | 0:4fbd246540da | 16 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where |
michaelveloo | 0:4fbd246540da | 17 | //the class checks if dist needs to be called. |
michaelveloo | 0:4fbd246540da | 18 | } |
michaelveloo | 0:4fbd246540da | 19 | } |