Development and testing of ultrasonic distance measurement library for device HC-SR04.

Dependencies:   TextLCD_improved mbed Distance_HC_SR04

main.cpp

Committer:
dzoni
Date:
2015-12-21
Branch:
CLASS_IMPLEMENTATION
Revision:
5:1a69f40177b0
Parent:
4:bb5819be6ac3
Child:
7:cdde5cc8b2b3

File content as of revision 5:1a69f40177b0:

/*
 * MAIN.CPP
 */
#include "mbed.h"

#include "TextLCD.h"
#include "Distance_HC_SR04.h"


int main() {

    TextLCD lcd(PA_8, PA_7, PA_9, PA_1, PB_5, PA_10, TextLCD::LCD16x2);
    
    wait_ms(250);
    lcd.cls();

    lcd.cls();
    lcd.printf("Row 1");
    lcd.locate(0, 1);
    lcd.printf("Row 2");

    Distance_HC_SR04 distFront(PB_9, PA_6);
    uint32_t ticks_us;
    float distance;

    while (true) {
        ticks_us = distFront.measureTicks();
        distance = distFront.getDistance();

        lcd.cls();

        switch (distFront.getState()) {
            case COMPLETED:
                lcd.printf("Dist.: %u", ticks_us);
                lcd.locate(0, 1);
                lcd.printf("Dist.: %.3f", distance);

                break;

            case TIMEOUT:
                lcd.printf("Dist.: ---");
                lcd.locate(0, 1);
                lcd.printf("TIMEOUT");

                break;

            case ERROR_SIG:
                lcd.printf("Dist.: ---");
                lcd.locate(0, 1);
                lcd.printf("ERROR_SIG");

                break;

            case OUT_OF_RANGE_MIN:
                lcd.printf("Dist.: ---");
                lcd.locate(0, 1);
                lcd.printf("OUT_OF_RANGE_MIN");

                break;

            case OUT_OF_RANGE_MAX:
                lcd.printf("Dist.: ---");
                lcd.locate(0, 1);
                lcd.printf("OUT_OF_RANGE_MAX");

                break;

            default:
                lcd.printf("Dist.: ---");
                lcd.locate(0, 1);
                lcd.printf("OTHER");

                break;
        }
        wait_ms(100);
    }
}