4180 LabTeam - Spring '18 / Mbed 2 deprecated Lab3Part1

Dependencies:   HC_SR04_Ultrasonic_Library mbed

Fork of LPC1768_HCSR04_HelloWorld by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ultrasonic.h"
00003 
00004 
00005 Serial pc(USBTX, USBRX);
00006  void dist(int distance)
00007 {
00008     //put code here to execute when the distance has changed
00009     float feet_distance = distance * 0.00328084f;
00010     pc.printf("Distance %d mm\rt\n", distance);
00011     pc.printf("Distance %.5f ft\r\n", feet_distance);
00012 }
00013 
00014 ultrasonic mu(p6, p7, .1, 1, &dist);    //Set the trigger pin to D8 and the echo pin to D9
00015                                         //have updates every .1 seconds and a timeout after 1
00016                                         //second, and call dist when the distance changes
00017 
00018 int main()
00019 {
00020     mu.startUpdates();//start measuring the distance
00021     while(1)
00022     {
00023         //Do something else here
00024         mu.checkDistance();     //call checkDistance() as much as possible, as this is where
00025                                 //the class checks if dist needs to be called.
00026     }
00027 }