Development and testing of ultrasonic distance measurement library for device HC-SR04.
Dependencies: TextLCD_improved mbed Distance_HC_SR04
main.cpp@7:cdde5cc8b2b3, 2015-12-21 (annotated)
- Committer:
- dzoni
- Date:
- Mon Dec 21 21:55:02 2015 +0000
- Branch:
- CLASS_IMPLEMENTATION
- Revision:
- 7:cdde5cc8b2b3
- Parent:
- 5:1a69f40177b0
Small touches...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dzoni | 0:6fd0fbcfc7e1 | 1 | /* |
dzoni | 4:bb5819be6ac3 | 2 | * MAIN.CPP |
dzoni | 0:6fd0fbcfc7e1 | 3 | */ |
dzoni | 0:6fd0fbcfc7e1 | 4 | #include "mbed.h" |
dzoni | 0:6fd0fbcfc7e1 | 5 | |
dzoni | 0:6fd0fbcfc7e1 | 6 | #include "TextLCD.h" |
dzoni | 5:1a69f40177b0 | 7 | #include "Distance_HC_SR04.h" |
dzoni | 2:aba8d0d53190 | 8 | |
dzoni | 2:aba8d0d53190 | 9 | |
dzoni | 0:6fd0fbcfc7e1 | 10 | int main() { |
dzoni | 0:6fd0fbcfc7e1 | 11 | |
dzoni | 5:1a69f40177b0 | 12 | TextLCD lcd(PA_8, PA_7, PA_9, PA_1, PB_5, PA_10, TextLCD::LCD16x2); |
dzoni | 5:1a69f40177b0 | 13 | |
dzoni | 0:6fd0fbcfc7e1 | 14 | wait_ms(250); |
dzoni | 0:6fd0fbcfc7e1 | 15 | lcd.cls(); |
dzoni | 0:6fd0fbcfc7e1 | 16 | |
dzoni | 4:bb5819be6ac3 | 17 | Distance_HC_SR04 distFront(PB_9, PA_6); |
dzoni | 4:bb5819be6ac3 | 18 | uint32_t ticks_us; |
dzoni | 5:1a69f40177b0 | 19 | float distance; |
dzoni | 0:6fd0fbcfc7e1 | 20 | |
dzoni | 4:bb5819be6ac3 | 21 | while (true) { |
dzoni | 4:bb5819be6ac3 | 22 | ticks_us = distFront.measureTicks(); |
dzoni | 4:bb5819be6ac3 | 23 | distance = distFront.getDistance(); |
dzoni | 5:1a69f40177b0 | 24 | |
dzoni | 4:bb5819be6ac3 | 25 | lcd.cls(); |
dzoni | 0:6fd0fbcfc7e1 | 26 | |
dzoni | 4:bb5819be6ac3 | 27 | switch (distFront.getState()) { |
dzoni | 4:bb5819be6ac3 | 28 | case COMPLETED: |
dzoni | 4:bb5819be6ac3 | 29 | lcd.printf("Dist.: %u", ticks_us); |
dzoni | 5:1a69f40177b0 | 30 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 31 | lcd.printf("Dist.: %.3f", distance); |
dzoni | 5:1a69f40177b0 | 32 | |
dzoni | 4:bb5819be6ac3 | 33 | break; |
dzoni | 5:1a69f40177b0 | 34 | |
dzoni | 4:bb5819be6ac3 | 35 | case TIMEOUT: |
dzoni | 4:bb5819be6ac3 | 36 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 37 | lcd.locate(0, 1); |
dzoni | 0:6fd0fbcfc7e1 | 38 | lcd.printf("TIMEOUT"); |
dzoni | 5:1a69f40177b0 | 39 | |
dzoni | 4:bb5819be6ac3 | 40 | break; |
dzoni | 5:1a69f40177b0 | 41 | |
dzoni | 4:bb5819be6ac3 | 42 | case ERROR_SIG: |
dzoni | 4:bb5819be6ac3 | 43 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 44 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 45 | lcd.printf("ERROR_SIG"); |
dzoni | 5:1a69f40177b0 | 46 | |
dzoni | 4:bb5819be6ac3 | 47 | break; |
dzoni | 5:1a69f40177b0 | 48 | |
dzoni | 4:bb5819be6ac3 | 49 | case OUT_OF_RANGE_MIN: |
dzoni | 4:bb5819be6ac3 | 50 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 51 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 52 | lcd.printf("OUT_OF_RANGE_MIN"); |
dzoni | 5:1a69f40177b0 | 53 | |
dzoni | 4:bb5819be6ac3 | 54 | break; |
dzoni | 5:1a69f40177b0 | 55 | |
dzoni | 4:bb5819be6ac3 | 56 | case OUT_OF_RANGE_MAX: |
dzoni | 4:bb5819be6ac3 | 57 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 58 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 59 | lcd.printf("OUT_OF_RANGE_MAX"); |
dzoni | 5:1a69f40177b0 | 60 | |
dzoni | 4:bb5819be6ac3 | 61 | break; |
dzoni | 5:1a69f40177b0 | 62 | |
dzoni | 4:bb5819be6ac3 | 63 | default: |
dzoni | 4:bb5819be6ac3 | 64 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 65 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 66 | lcd.printf("OTHER"); |
dzoni | 5:1a69f40177b0 | 67 | |
dzoni | 4:bb5819be6ac3 | 68 | break; |
dzoni | 0:6fd0fbcfc7e1 | 69 | } |
dzoni | 0:6fd0fbcfc7e1 | 70 | wait_ms(100); |
dzoni | 0:6fd0fbcfc7e1 | 71 | } |
dzoni | 0:6fd0fbcfc7e1 | 72 | } |