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.
main.cpp@2:f86b1e3609b3, 2015-12-05 (annotated)
- Committer:
- tbjazic
- Date:
- Sat Dec 05 09:07:15 2015 +0000
- Revision:
- 2:f86b1e3609b3
- Parent:
- 1:22043b67c31c
- Child:
- 3:3297ea6e3ae1
Library HCSR04 created.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tbjazic | 2:f86b1e3609b3 | 1 | /** Revision 2: Separating the class declaration, implementation and main() function into files. */ |
tbjazic | 0:ebee649c5b1b | 2 | |
tbjazic | 0:ebee649c5b1b | 3 | #include "mbed.h" |
tbjazic | 2:f86b1e3609b3 | 4 | #include "HCSR04.h" |
tbjazic | 0:ebee649c5b1b | 5 | |
tbjazic | 0:ebee649c5b1b | 6 | Serial pc(USBTX, USBRX); // communication with terminal |
tbjazic | 0:ebee649c5b1b | 7 | |
tbjazic | 0:ebee649c5b1b | 8 | int main() { |
tbjazic | 1:22043b67c31c | 9 | HCSR04 sensor(p5, p7); // instantiate the sensor object |
tbjazic | 0:ebee649c5b1b | 10 | while(1) { |
tbjazic | 1:22043b67c31c | 11 | /** Print the result in cm to the terminal with 1 decimal place |
tbjazic | 0:ebee649c5b1b | 12 | * (number 5 after % means that total of 5 digits will be reserved |
tbjazic | 0:ebee649c5b1b | 13 | * for printing the number, including the dot and one decimal place). */ |
tbjazic | 1:22043b67c31c | 14 | pc.printf("Distance: %5.1f cm\r", sensor.getDistance_cm()); |
tbjazic | 1:22043b67c31c | 15 | wait_ms(1000-25); |
tbjazic | 0:ebee649c5b1b | 16 | } |
tbjazic | 0:ebee649c5b1b | 17 | } |