UltrasonicRanger_Example V1.0

Dependencies:   mbed RangeFinder

Committer:
mbedAustin
Date:
Sat Aug 29 04:27:42 2015 +0000
Revision:
1:285d47b768ac
Parent:
0:186bb2174995
Child:
2:fb4f4763a3f7
Updated example code and underlying libraries to work properly

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NickRyder 0:186bb2174995 1 /* Copyright (c) 2012 Nick Ryder, University of Oxford
NickRyder 0:186bb2174995 2 * nick.ryder@physics.ox.ac.uk
NickRyder 0:186bb2174995 3 *
NickRyder 0:186bb2174995 4 * MIT License
NickRyder 0:186bb2174995 5 *
NickRyder 0:186bb2174995 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
NickRyder 0:186bb2174995 7 * and associated documentation files (the "Software"), to deal in the Software without restriction,
NickRyder 0:186bb2174995 8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
NickRyder 0:186bb2174995 9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
NickRyder 0:186bb2174995 10 * furnished to do so, subject to the following conditions:
NickRyder 0:186bb2174995 11 *
NickRyder 0:186bb2174995 12 * The above copyright notice and this permission notice shall be included in all copies or
NickRyder 0:186bb2174995 13 * substantial portions of the Software.
NickRyder 0:186bb2174995 14 *
NickRyder 0:186bb2174995 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
NickRyder 0:186bb2174995 16 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NickRyder 0:186bb2174995 17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
NickRyder 0:186bb2174995 18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
NickRyder 0:186bb2174995 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
NickRyder 0:186bb2174995 20 */
NickRyder 0:186bb2174995 21
NickRyder 0:186bb2174995 22 #include "mbed.h"
NickRyder 0:186bb2174995 23
NickRyder 0:186bb2174995 24 #include "RangeFinder.h"
NickRyder 0:186bb2174995 25
NickRyder 0:186bb2174995 26 // Seeed ultrasound range finder
mbedAustin 1:285d47b768ac 27 RangeFinder rf(D0, 10, 5800.0, 100000);
NickRyder 0:186bb2174995 28 DigitalOut led(LED1);
NickRyder 0:186bb2174995 29
NickRyder 0:186bb2174995 30 int main() {
NickRyder 0:186bb2174995 31 led = 1;
NickRyder 0:186bb2174995 32 float d;
NickRyder 0:186bb2174995 33 while (1) {
NickRyder 0:186bb2174995 34 d = rf.read_m();
NickRyder 0:186bb2174995 35 if (d == -1.0) {
NickRyder 0:186bb2174995 36 printf("Timeout Error.\n");
NickRyder 0:186bb2174995 37 } else if (d > 5.0) {
NickRyder 0:186bb2174995 38 // Seeed's sensor has a maximum range of 4m, it returns
NickRyder 0:186bb2174995 39 // something like 7m if the ultrasound pulse isn't reflected.
NickRyder 0:186bb2174995 40 printf("No object within detection range.\n");
NickRyder 0:186bb2174995 41 } else {
NickRyder 0:186bb2174995 42 printf("Distance = %f m.\n", d);
NickRyder 0:186bb2174995 43 }
NickRyder 0:186bb2174995 44 wait(0.5);
NickRyder 0:186bb2174995 45 led = !led;
NickRyder 0:186bb2174995 46 }
NickRyder 0:186bb2174995 47 }