Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HC-SR04 by
main.cpp@4:052ac3f5c938, 2015-12-07 (annotated)
- Committer:
- tbjazic
- Date:
- Mon Dec 07 10:44:07 2015 +0000
- Revision:
- 4:052ac3f5c938
- Parent:
- 3:3297ea6e3ae1
- Child:
- 5:889566fb3a85
PT1 filter added
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tbjazic | 0:ebee649c5b1b | 1 | #include "mbed.h" |
| tbjazic | 2:f86b1e3609b3 | 2 | #include "HCSR04.h" |
| tbjazic | 4:052ac3f5c938 | 3 | #include "AutomationElements.h" |
| tbjazic | 0:ebee649c5b1b | 4 | |
| tbjazic | 3:3297ea6e3ae1 | 5 | Serial pc(USBTX, USBRX); |
| tbjazic | 4:052ac3f5c938 | 6 | HCSR04 sensor(p5, p7); |
| tbjazic | 4:052ac3f5c938 | 7 | float sampleTime = 0.1; |
| tbjazic | 4:052ac3f5c938 | 8 | PT1 filter(1, 2, sampleTime); |
| tbjazic | 4:052ac3f5c938 | 9 | Ticker ticker; |
| tbjazic | 4:052ac3f5c938 | 10 | float distance; |
| tbjazic | 4:052ac3f5c938 | 11 | float filteredDistance; |
| tbjazic | 4:052ac3f5c938 | 12 | DigitalOut led1(LED1); |
| tbjazic | 4:052ac3f5c938 | 13 | |
| tbjazic | 4:052ac3f5c938 | 14 | void calc() { |
| tbjazic | 4:052ac3f5c938 | 15 | led1 = 1; |
| tbjazic | 4:052ac3f5c938 | 16 | distance = sensor.getDistance_mm(); |
| tbjazic | 4:052ac3f5c938 | 17 | filter.in(distance); |
| tbjazic | 4:052ac3f5c938 | 18 | filteredDistance = filter.out(); |
| tbjazic | 4:052ac3f5c938 | 19 | pc.printf("%7.1f mm %7.1f mm \r", distance, filteredDistance); |
| tbjazic | 4:052ac3f5c938 | 20 | led1 = 0; |
| tbjazic | 4:052ac3f5c938 | 21 | } |
| tbjazic | 0:ebee649c5b1b | 22 | |
| tbjazic | 0:ebee649c5b1b | 23 | int main() { |
| tbjazic | 3:3297ea6e3ae1 | 24 | sensor.setRanges(10, 110); |
| tbjazic | 4:052ac3f5c938 | 25 | pc.printf("Minimum sensor range = %g cm\n\rMaximum sensor range = %g cm\n\r", sensor.getMinRange(), sensor.getMaxRange()); |
| tbjazic | 4:052ac3f5c938 | 26 | pc.printf("Sensor: Filtered:\n\r"); |
| tbjazic | 4:052ac3f5c938 | 27 | ticker.attach(&calc, sampleTime); |
| tbjazic | 4:052ac3f5c938 | 28 | |
| tbjazic | 0:ebee649c5b1b | 29 | while(1) { |
| tbjazic | 3:3297ea6e3ae1 | 30 | wait_ms(500); |
| tbjazic | 0:ebee649c5b1b | 31 | } |
| tbjazic | 0:ebee649c5b1b | 32 | } |
