Sensors with Distance
Dependencies: HC_SR04_Ultrasonic_Library mbed
Fork of LPC1768_HCSR04_HelloWorld by
main.cpp@3:65b2a102e23e, 2018-02-28 (annotated)
- Committer:
- bjs9
- Date:
- Wed Feb 28 09:55:03 2018 +0000
- Revision:
- 3:65b2a102e23e
- Parent:
- 2:3566c2d1e86b
Sensor with Distance;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ejteb | 0:1704ea055c4f | 1 | #include "mbed.h" |
ejteb | 0:1704ea055c4f | 2 | #include "ultrasonic.h" |
ejteb | 0:1704ea055c4f | 3 | |
bjs9 | 3:65b2a102e23e | 4 | |
bjs9 | 3:65b2a102e23e | 5 | Serial pc(USBTX, USBRX); |
ejteb | 0:1704ea055c4f | 6 | void dist(int distance) |
ejteb | 0:1704ea055c4f | 7 | { |
4180_1 | 2:3566c2d1e86b | 8 | //put code here to execute when the distance has changed |
bjs9 | 3:65b2a102e23e | 9 | float feet_distance = distance * 0.00328084f; |
bjs9 | 3:65b2a102e23e | 10 | pc.printf("Distance %d mm\rt\n", distance); |
bjs9 | 3:65b2a102e23e | 11 | pc.printf("Distance %.5f ft\r\n", feet_distance); |
ejteb | 0:1704ea055c4f | 12 | } |
ejteb | 0:1704ea055c4f | 13 | |
4180_1 | 2:3566c2d1e86b | 14 | ultrasonic mu(p6, p7, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 |
ejteb | 1:4a5586eb1765 | 15 | //have updates every .1 seconds and a timeout after 1 |
ejteb | 1:4a5586eb1765 | 16 | //second, and call dist when the distance changes |
ejteb | 0:1704ea055c4f | 17 | |
ejteb | 0:1704ea055c4f | 18 | int main() |
ejteb | 0:1704ea055c4f | 19 | { |
4180_1 | 2:3566c2d1e86b | 20 | mu.startUpdates();//start measuring the distance |
ejteb | 0:1704ea055c4f | 21 | while(1) |
ejteb | 0:1704ea055c4f | 22 | { |
ejteb | 1:4a5586eb1765 | 23 | //Do something else here |
ejteb | 1:4a5586eb1765 | 24 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where |
ejteb | 1:4a5586eb1765 | 25 | //the class checks if dist needs to be called. |
ejteb | 0:1704ea055c4f | 26 | } |
ejteb | 0:1704ea055c4f | 27 | } |