Ultrasonic

Dependencies:   HC_SR04_Ultrasonic_Library mbed

Fork of Nucleo_UltrasonicHelloWorld by EJ Teb

Committer:
Jorge_Beltran
Date:
Sat Dec 05 18:20:59 2015 +0000
Revision:
2:b5709fea6640
Parent:
1:4a5586eb1765
Ultrasonic

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ejteb 0:1704ea055c4f 1 #include "mbed.h"
ejteb 0:1704ea055c4f 2 #include "ultrasonic.h"
ejteb 0:1704ea055c4f 3
Jorge_Beltran 2:b5709fea6640 4 Serial pc(USBTX, USBRX);
Jorge_Beltran 2:b5709fea6640 5
ejteb 0:1704ea055c4f 6 void dist(int distance)
ejteb 0:1704ea055c4f 7 {
ejteb 1:4a5586eb1765 8 //put code here to happen when the distance is changed
ejteb 1:4a5586eb1765 9 printf("Distance changed to %dmm\r\n", distance);
ejteb 0:1704ea055c4f 10 }
Jorge_Beltran 2:b5709fea6640 11 void dist2(int distance2)
Jorge_Beltran 2:b5709fea6640 12 {
Jorge_Beltran 2:b5709fea6640 13 //put code here to happen when the distance is changed
Jorge_Beltran 2:b5709fea6640 14 printf("Distance2 changed to %dmm\r\n", distance2);
Jorge_Beltran 2:b5709fea6640 15 }
ejteb 0:1704ea055c4f 16
ejteb 1:4a5586eb1765 17 ultrasonic mu(D8, D9, .1, 1, &dist); //Set the trigger pin to D8 and the echo pin to D9
ejteb 1:4a5586eb1765 18 //have updates every .1 seconds and a timeout after 1
ejteb 1:4a5586eb1765 19 //second, and call dist when the distance changes
Jorge_Beltran 2:b5709fea6640 20 ultrasonic mu2(D0, D1, .1, 1, &dist2); //Set the trigger pin to D0 and the echo pin to D1
Jorge_Beltran 2:b5709fea6640 21 //have updates every .1 seconds and a timeout after 1
Jorge_Beltran 2:b5709fea6640 22 //second, and call dist when the distance changes
ejteb 0:1704ea055c4f 23
ejteb 0:1704ea055c4f 24 int main()
ejteb 0:1704ea055c4f 25 {
Jorge_Beltran 2:b5709fea6640 26 int CDistance = 0;
Jorge_Beltran 2:b5709fea6640 27 int CDistance2 = 0;
Jorge_Beltran 2:b5709fea6640 28 pc.baud(115200);
ejteb 1:4a5586eb1765 29 mu.startUpdates();//start mesuring the distance
Jorge_Beltran 2:b5709fea6640 30 mu2.startUpdates();//start mesuring the distance
ejteb 0:1704ea055c4f 31 while(1)
ejteb 0:1704ea055c4f 32 {
ejteb 1:4a5586eb1765 33 //Do something else here
ejteb 1:4a5586eb1765 34 mu.checkDistance(); //call checkDistance() as much as possible, as this is where
ejteb 1:4a5586eb1765 35 //the class checks if dist needs to be called.
Jorge_Beltran 2:b5709fea6640 36 mu2.checkDistance(); //call checkDistance() as much as possible, as this is where
Jorge_Beltran 2:b5709fea6640 37 //the class checks if dist needs to be called.
Jorge_Beltran 2:b5709fea6640 38 CDistance = mu.getCurrentDistance();
Jorge_Beltran 2:b5709fea6640 39 CDistance2 = mu2.getCurrentDistance();
Jorge_Beltran 2:b5709fea6640 40 pc.printf("Distance1 changed to %dmm, Distance2 changed to %dmm\r\n", CDistance, CDistance2);
Jorge_Beltran 2:b5709fea6640 41 wait(0.50);
ejteb 0:1704ea055c4f 42 }
ejteb 0:1704ea055c4f 43 }