Rhomb.io demo program for HC-SR04 Ultrasonic sensor

Dependencies:   HC_SR04_Ultrasonic_Library

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Medidor de distancia ultrasonico. Trigger: IO3, Echo: IO4
00002 
00003 #include "mbed.h"
00004 #include "ultrasonic.h"
00005 
00006  void dist(int distance)
00007 {
00008     //put code here to happen when the distance is changed
00009     printf("Distance changed to %dmm\r\n", distance);
00010 }
00011 
00012 ultrasonic mu(IO3, IO4, .1, 1, &dist);    //Set the trigger pin to D8 and the echo pin to D9
00013                                         //have updates every .1 seconds and a timeout after 1
00014                                         //second, and call dist when the distance changes
00015 
00016 int main()
00017 {
00018     mu.startUpdates();//start mesuring the distance
00019     while(1)
00020     {
00021         //Do something else here
00022         mu.checkDistance();     //call checkDistance() as much as possible, as this is where
00023                                 //the class checks if dist needs to be called.
00024     }
00025 }