Development and testing of ultrasonic distance measurement library for device HC-SR04.
Dependencies: TextLCD_improved mbed Distance_HC_SR04
main.cpp@5:1a69f40177b0, 2015-12-21 (annotated)
- Committer:
- dzoni
- Date:
- Mon Dec 21 20:54:09 2015 +0000
- Branch:
- CLASS_IMPLEMENTATION
- Revision:
- 5:1a69f40177b0
- Parent:
- 4:bb5819be6ac3
- Child:
- 7:cdde5cc8b2b3
Class implementation completed and successfully tested.
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 | 0:6fd0fbcfc7e1 | 17 | lcd.cls(); |
dzoni | 0:6fd0fbcfc7e1 | 18 | lcd.printf("Row 1"); |
dzoni | 5:1a69f40177b0 | 19 | lcd.locate(0, 1); |
dzoni | 0:6fd0fbcfc7e1 | 20 | lcd.printf("Row 2"); |
dzoni | 0:6fd0fbcfc7e1 | 21 | |
dzoni | 4:bb5819be6ac3 | 22 | Distance_HC_SR04 distFront(PB_9, PA_6); |
dzoni | 4:bb5819be6ac3 | 23 | uint32_t ticks_us; |
dzoni | 5:1a69f40177b0 | 24 | float distance; |
dzoni | 0:6fd0fbcfc7e1 | 25 | |
dzoni | 4:bb5819be6ac3 | 26 | while (true) { |
dzoni | 4:bb5819be6ac3 | 27 | ticks_us = distFront.measureTicks(); |
dzoni | 4:bb5819be6ac3 | 28 | distance = distFront.getDistance(); |
dzoni | 5:1a69f40177b0 | 29 | |
dzoni | 4:bb5819be6ac3 | 30 | lcd.cls(); |
dzoni | 0:6fd0fbcfc7e1 | 31 | |
dzoni | 4:bb5819be6ac3 | 32 | switch (distFront.getState()) { |
dzoni | 4:bb5819be6ac3 | 33 | case COMPLETED: |
dzoni | 4:bb5819be6ac3 | 34 | lcd.printf("Dist.: %u", ticks_us); |
dzoni | 5:1a69f40177b0 | 35 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 36 | lcd.printf("Dist.: %.3f", distance); |
dzoni | 5:1a69f40177b0 | 37 | |
dzoni | 4:bb5819be6ac3 | 38 | break; |
dzoni | 5:1a69f40177b0 | 39 | |
dzoni | 4:bb5819be6ac3 | 40 | case TIMEOUT: |
dzoni | 4:bb5819be6ac3 | 41 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 42 | lcd.locate(0, 1); |
dzoni | 0:6fd0fbcfc7e1 | 43 | lcd.printf("TIMEOUT"); |
dzoni | 5:1a69f40177b0 | 44 | |
dzoni | 4:bb5819be6ac3 | 45 | break; |
dzoni | 5:1a69f40177b0 | 46 | |
dzoni | 4:bb5819be6ac3 | 47 | case ERROR_SIG: |
dzoni | 4:bb5819be6ac3 | 48 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 49 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 50 | lcd.printf("ERROR_SIG"); |
dzoni | 5:1a69f40177b0 | 51 | |
dzoni | 4:bb5819be6ac3 | 52 | break; |
dzoni | 5:1a69f40177b0 | 53 | |
dzoni | 4:bb5819be6ac3 | 54 | case OUT_OF_RANGE_MIN: |
dzoni | 4:bb5819be6ac3 | 55 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 56 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 57 | lcd.printf("OUT_OF_RANGE_MIN"); |
dzoni | 5:1a69f40177b0 | 58 | |
dzoni | 4:bb5819be6ac3 | 59 | break; |
dzoni | 5:1a69f40177b0 | 60 | |
dzoni | 4:bb5819be6ac3 | 61 | case OUT_OF_RANGE_MAX: |
dzoni | 4:bb5819be6ac3 | 62 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 63 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 64 | lcd.printf("OUT_OF_RANGE_MAX"); |
dzoni | 5:1a69f40177b0 | 65 | |
dzoni | 4:bb5819be6ac3 | 66 | break; |
dzoni | 5:1a69f40177b0 | 67 | |
dzoni | 4:bb5819be6ac3 | 68 | default: |
dzoni | 4:bb5819be6ac3 | 69 | lcd.printf("Dist.: ---"); |
dzoni | 5:1a69f40177b0 | 70 | lcd.locate(0, 1); |
dzoni | 4:bb5819be6ac3 | 71 | lcd.printf("OTHER"); |
dzoni | 5:1a69f40177b0 | 72 | |
dzoni | 4:bb5819be6ac3 | 73 | break; |
dzoni | 0:6fd0fbcfc7e1 | 74 | } |
dzoni | 0:6fd0fbcfc7e1 | 75 | wait_ms(100); |
dzoni | 0:6fd0fbcfc7e1 | 76 | } |
dzoni | 0:6fd0fbcfc7e1 | 77 | } |