Example for my HC-SR04 Ultrasonic Sensor library.

Dependencies:   C12832 HCSR04 mbed

Fork of Utrasonic_Example by Leong Kum Loong (NYP)

main.cpp

Committer:
reesey
Date:
2017-12-04
Revision:
3:a7677858ce0b
Parent:
0:bb6f81d15af7

File content as of revision 3:a7677858ce0b:

#include "mbed.h"
#include "C12832.h"
#include "HCSR04.h"
/*
    Written by Leong Kum Loong.
    04 Dec 2017, REV. 0
    
    When HCSR04.h reference is included, DO NOT declare pin p21 & p22 as it is used by my library.
*/

C12832 lcd(p5, p7, p6, p8, p11);
DigitalOut led(LED1);
InterruptIn joyCtr(p14);

int unit = 1;
char str[] = "Distance: %.2f cm        ";

void config() {
    if(unit == 1){
        unit = 2;
        strcpy(str, "Distance: %.3f inch        ");
    } else {
        unit = 1;
        strcpy(str, "Distance: %.2f cm        ");
    }
    getDistance(unit);  //Unit 1 for CM & 2 for INCH
}

int main() {
    joyCtr.rise(config);
    getDistance();  //Start retrieving distance.
    lcd.cls();
    
    while(1) {
        lcd.locate(0,1);
        lcd.printf(str, readDistance());    //Read retrieved distance.
        
        led = !led;
        wait_ms(100);   //**** Wait function will cause delay to the ultrasonic sensor next read cycle.
    }
}