This code is for Ultrasonic HCSR04 with FRDM-KL25Z
Dependencies: mbed HC_SR04_Ultrasonic_Library
main.cpp@3:5fb712f78508, 2019-09-03 (annotated)
- Committer:
- mrbhatter
- Date:
- Tue Sep 03 12:28:35 2019 +0000
- Revision:
- 3:5fb712f78508
- Parent:
- 2:3566c2d1e86b
Ultrasonic with FRDM KL25Z
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 | |
ejteb | 0:1704ea055c4f | 4 | void dist(int distance) |
ejteb | 0:1704ea055c4f | 5 | { |
4180_1 | 2:3566c2d1e86b | 6 | //put code here to execute when the distance has changed |
4180_1 | 2:3566c2d1e86b | 7 | printf("Distance %d mm\r\n", distance); |
ejteb | 0:1704ea055c4f | 8 | } |
ejteb | 0:1704ea055c4f | 9 | |
mrbhatter | 3:5fb712f78508 | 10 | ultrasonic mu(PTC, PTC, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 |
ejteb | 1:4a5586eb1765 | 11 | //have updates every .1 seconds and a timeout after 1 |
ejteb | 1:4a5586eb1765 | 12 | //second, and call dist when the distance changes |
ejteb | 0:1704ea055c4f | 13 | |
ejteb | 0:1704ea055c4f | 14 | int main() |
ejteb | 0:1704ea055c4f | 15 | { |
4180_1 | 2:3566c2d1e86b | 16 | mu.startUpdates();//start measuring the distance |
ejteb | 0:1704ea055c4f | 17 | while(1) |
ejteb | 0:1704ea055c4f | 18 | { |
ejteb | 1:4a5586eb1765 | 19 | //Do something else here |
ejteb | 1:4a5586eb1765 | 20 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where |
ejteb | 1:4a5586eb1765 | 21 | //the class checks if dist needs to be called. |
ejteb | 0:1704ea055c4f | 22 | } |
ejteb | 0:1704ea055c4f | 23 | } |