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@3:3297ea6e3ae1, 2015-12-07 (annotated)
- Committer:
- tbjazic
- Date:
- Mon Dec 07 09:38:35 2015 +0000
- Revision:
- 3:3297ea6e3ae1
- Parent:
- 2:f86b1e3609b3
- Child:
- 4:052ac3f5c938
Setting and displaying sensor ranges added. Displaying the distance in mm.
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 | 0:ebee649c5b1b | 3 | |
tbjazic | 3:3297ea6e3ae1 | 4 | Serial pc(USBTX, USBRX); |
tbjazic | 0:ebee649c5b1b | 5 | |
tbjazic | 0:ebee649c5b1b | 6 | int main() { |
tbjazic | 3:3297ea6e3ae1 | 7 | HCSR04 sensor(p5, p7); |
tbjazic | 3:3297ea6e3ae1 | 8 | sensor.setRanges(10, 110); |
tbjazic | 3:3297ea6e3ae1 | 9 | pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r", |
tbjazic | 3:3297ea6e3ae1 | 10 | sensor.getMinRange(), sensor.getMaxRange()); |
tbjazic | 0:ebee649c5b1b | 11 | while(1) { |
tbjazic | 3:3297ea6e3ae1 | 12 | pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm()); |
tbjazic | 3:3297ea6e3ae1 | 13 | wait_ms(500); |
tbjazic | 0:ebee649c5b1b | 14 | } |
tbjazic | 0:ebee649c5b1b | 15 | } |