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.
Dependencies: mbed HCSR04 AutomationElements
Revision 6:bca0839e8295, committed 2016-12-10
- Comitter:
- tbjazic
- Date:
- Sat Dec 10 08:28:06 2016 +0000
- Parent:
- 4:052ac3f5c938
- Commit message:
- Filtering with Ticker object corrected.
Changed in this revision
HCSR04.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 052ac3f5c938 -r bca0839e8295 HCSR04.lib --- a/HCSR04.lib Mon Dec 07 10:44:07 2015 +0000 +++ b/HCSR04.lib Sat Dec 10 08:28:06 2016 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/HCSR04/#a667b621f625 +https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/HCSR04/#cf3e4e307d15
diff -r 052ac3f5c938 -r bca0839e8295 main.cpp --- a/main.cpp Mon Dec 07 10:44:07 2015 +0000 +++ b/main.cpp Sat Dec 10 08:28:06 2016 +0000 @@ -4,29 +4,28 @@ Serial pc(USBTX, USBRX); HCSR04 sensor(p5, p7); -float sampleTime = 0.1; +float sampleTime = 0.5; PT1 filter(1, 2, sampleTime); Ticker ticker; float distance; float filteredDistance; -DigitalOut led1(LED1); void calc() { - led1 = 1; - distance = sensor.getDistance_mm(); - filter.in(distance); - filteredDistance = filter.out(); - pc.printf("%7.1f mm %7.1f mm \r", distance, filteredDistance); - led1 = 0; + sensor.startMeasurement(); } int main() { - sensor.setRanges(10, 110); + sensor.setRanges(2, 400); pc.printf("Minimum sensor range = %g cm\n\rMaximum sensor range = %g cm\n\r", sensor.getMinRange(), sensor.getMaxRange()); pc.printf("Sensor: Filtered:\n\r"); ticker.attach(&calc, sampleTime); - - while(1) { - wait_ms(500); + while(true) { + while(!sensor.isNewDataReady()) { + // wait for new data + } + distance = sensor.getDistance_cm(); + filter.in(distance); + filteredDistance = filter.out(); + pc.printf("%7.1f cm %7.1f cm\n\r", distance, filteredDistance); } } \ No newline at end of file