Sensors with Distance

Dependencies:   HC_SR04_Ultrasonic_Library mbed

Fork of LPC1768_HCSR04_HelloWorld by jim hamblen

main.cpp

Committer:
bjs9
Date:
2018-02-28
Revision:
3:65b2a102e23e
Parent:
2:3566c2d1e86b

File content as of revision 3:65b2a102e23e:

#include "mbed.h"
#include "ultrasonic.h"


Serial pc(USBTX, USBRX);
 void dist(int distance)
{
    //put code here to execute when the distance has changed
    float feet_distance = distance * 0.00328084f;
    pc.printf("Distance %d mm\rt\n", distance);
    pc.printf("Distance %.5f ft\r\n", feet_distance);
}

ultrasonic mu(p6, p7, .1, 1, &dist);    //Set the trigger pin to D8 and the echo pin to D9
                                        //have updates every .1 seconds and a timeout after 1
                                        //second, and call dist when the distance changes

int main()
{
    mu.startUpdates();//start measuring the distance
    while(1)
    {
        //Do something else here
        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
                                //the class checks if dist needs to be called.
    }
}