Jorge Martin Beltran Avila / Mbed 2 deprecated UltrasoniCota

Dependencies:   HC_SR04_Ultrasonic_Library mbed

Fork of Nucleo_UltrasonicHelloWorld by EJ Teb

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 Serial pc(USBTX, USBRX);
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  void dist2(int distance2)
00012 {
00013     //put code here to happen when the distance is changed
00014     printf("Distance2 changed to %dmm\r\n", distance2);
00015 }
00016 
00017 ultrasonic mu(D8, D9, .1, 1, &dist);    //Set the trigger pin to D8 and the echo pin to D9
00018                                         //have updates every .1 seconds and a timeout after 1
00019                                         //second, and call dist when the distance changes
00020 ultrasonic mu2(D0, D1, .1, 1, &dist2);  //Set the trigger pin to D0 and the echo pin to D1
00021                                         //have updates every .1 seconds and a timeout after 1
00022                                         //second, and call dist when the distance changes 
00023 
00024 int main()
00025 {
00026     int CDistance = 0;
00027     int CDistance2 = 0;
00028     pc.baud(115200);
00029     mu.startUpdates();//start mesuring the distance
00030     mu2.startUpdates();//start mesuring the distance
00031     while(1)
00032     {
00033         //Do something else here
00034         mu.checkDistance();     //call checkDistance() as much as possible, as this is where
00035                                 //the class checks if dist needs to be called.
00036         mu2.checkDistance();     //call checkDistance() as much as possible, as this is where
00037                                 //the class checks if dist needs to be called.
00038         CDistance = mu.getCurrentDistance();
00039         CDistance2 = mu2.getCurrentDistance();
00040         pc.printf("Distance1 changed to %dmm, Distance2 changed to %dmm\r\n", CDistance, CDistance2);
00041         wait(0.50);
00042     }
00043 }