A program that demonstrates the development of library, using as an example an ultrasonic distance sensor HC-SR04.
Dependencies: mbed HCSR04 AutomationElements
The purpose of this program is to encourage students to develope their own classes. Instructions how to follow the development of this program and class HCSR04 for ultrasonic distance measurement are given here.
Revision 3:3297ea6e3ae1, committed 2015-12-07
- Comitter:
- tbjazic
- Date:
- Mon Dec 07 09:38:35 2015 +0000
- Parent:
- 2:f86b1e3609b3
- Child:
- 4:052ac3f5c938
- Commit message:
- Setting and displaying sensor ranges added. Displaying the distance in mm.
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 |
--- a/HCSR04.lib Sat Dec 05 09:07:15 2015 +0000 +++ b/HCSR04.lib Mon Dec 07 09:38:35 2015 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/HCSR04/#8871082486ac +https://developer.mbed.org/teams/TVZ-Mechatronics-Team/code/HCSR04/#aae70f15357f
--- a/main.cpp Sat Dec 05 09:07:15 2015 +0000 +++ b/main.cpp Mon Dec 07 09:38:35 2015 +0000 @@ -1,17 +1,15 @@ -/** Revision 2: Separating the class declaration, implementation and main() function into files. */ - #include "mbed.h" #include "HCSR04.h" -Serial pc(USBTX, USBRX); // communication with terminal +Serial pc(USBTX, USBRX); int main() { - HCSR04 sensor(p5, p7); // instantiate the sensor object + HCSR04 sensor(p5, p7); + sensor.setRanges(10, 110); + pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r", + sensor.getMinRange(), sensor.getMaxRange()); while(1) { - /** Print the result in cm to the terminal with 1 decimal place - * (number 5 after % means that total of 5 digits will be reserved - * for printing the number, including the dot and one decimal place). */ - pc.printf("Distance: %5.1f cm\r", sensor.getDistance_cm()); - wait_ms(1000-25); + pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm()); + wait_ms(500); } } \ No newline at end of file