Rhomb.io demo program for HC-SR04 Ultrasonic sensor
Dependencies: HC_SR04_Ultrasonic_Library
main.cpp@2:521be417cc8a, 2019-04-04 (annotated)
- Committer:
- rmir2
- Date:
- Thu Apr 04 06:50:43 2019 +0000
- Revision:
- 2:521be417cc8a
- Parent:
- 1:4a5586eb1765
Working fine at Deimos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rmir2 | 2:521be417cc8a | 1 | //Medidor de distancia ultrasonico. Trigger: IO3, Echo: IO4 |
rmir2 | 2:521be417cc8a | 2 | |
ejteb | 0:1704ea055c4f | 3 | #include "mbed.h" |
ejteb | 0:1704ea055c4f | 4 | #include "ultrasonic.h" |
ejteb | 0:1704ea055c4f | 5 | |
ejteb | 0:1704ea055c4f | 6 | void dist(int distance) |
ejteb | 0:1704ea055c4f | 7 | { |
ejteb | 1:4a5586eb1765 | 8 | //put code here to happen when the distance is changed |
ejteb | 1:4a5586eb1765 | 9 | printf("Distance changed to %dmm\r\n", distance); |
ejteb | 0:1704ea055c4f | 10 | } |
ejteb | 0:1704ea055c4f | 11 | |
rmir2 | 2:521be417cc8a | 12 | ultrasonic mu(IO3, IO4, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9 |
ejteb | 1:4a5586eb1765 | 13 | //have updates every .1 seconds and a timeout after 1 |
ejteb | 1:4a5586eb1765 | 14 | //second, and call dist when the distance changes |
ejteb | 0:1704ea055c4f | 15 | |
ejteb | 0:1704ea055c4f | 16 | int main() |
ejteb | 0:1704ea055c4f | 17 | { |
ejteb | 1:4a5586eb1765 | 18 | mu.startUpdates();//start mesuring the distance |
ejteb | 0:1704ea055c4f | 19 | while(1) |
ejteb | 0:1704ea055c4f | 20 | { |
ejteb | 1:4a5586eb1765 | 21 | //Do something else here |
ejteb | 1:4a5586eb1765 | 22 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where |
ejteb | 1:4a5586eb1765 | 23 | //the class checks if dist needs to be called. |
ejteb | 0:1704ea055c4f | 24 | } |
ejteb | 0:1704ea055c4f | 25 | } |