Create this program

Dependencies:   mbed HCSR04 HMC6352 PID TextLCD

Committer:
KoiShin_Sakana
Date:
Thu Aug 06 00:19:29 2015 +0000
Revision:
2:a8bbf677e774
Parent:
1:d0cada9a5381
Child:
3:901d18b901b4
create communication.h, control.h, infrared.h, main.cpp, pinDef.h and ultrasonic.h; update compass.h and motor.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KoiShin_Sakana 2:a8bbf677e774 1 /**
KoiShin_Sakana 2:a8bbf677e774 2 * @file : ultrasonic.h (1.0)
KoiShin_Sakana 2:a8bbf677e774 3 * @brief : measure distance from something
KoiShin_Sakana 2:a8bbf677e774 4 * @author : Shinnosuke KOIKE
KoiShin_Sakana 2:a8bbf677e774 5 * @date : 2015/08/04
KoiShin_Sakana 2:a8bbf677e774 6 */
KoiShin_Sakana 2:a8bbf677e774 7
KoiShin_Sakana 1:d0cada9a5381 8 #ifndef ULTRASONIC_H
KoiShin_Sakana 1:d0cada9a5381 9 #define ULTRASONIC_H
KoiShin_Sakana 1:d0cada9a5381 10
KoiShin_Sakana 1:d0cada9a5381 11 #include "mbed.h"
KoiShin_Sakana 1:d0cada9a5381 12 #include "hcsr04.h"
KoiShin_Sakana 1:d0cada9a5381 13
KoiShin_Sakana 1:d0cada9a5381 14 class Ultrasonic {
KoiShin_Sakana 2:a8bbf677e774 15 public:
KoiShin_Sakana 2:a8bbf677e774 16 Ultrasonic(PinName trig, PinName echo);
KoiShin_Sakana 2:a8bbf677e774 17 int measureDist(void);
KoiShin_Sakana 2:a8bbf677e774 18
KoiShin_Sakana 1:d0cada9a5381 19 private:
KoiShin_Sakana 1:d0cada9a5381 20 HCSR04* hcsr04;
KoiShin_Sakana 1:d0cada9a5381 21 };
KoiShin_Sakana 1:d0cada9a5381 22
KoiShin_Sakana 2:a8bbf677e774 23 // initialize
KoiShin_Sakana 1:d0cada9a5381 24 Ultrasonic::Ultrasonic(PinName trig, PinName echo) {
KoiShin_Sakana 1:d0cada9a5381 25 hcsr04 = new HCSR04(trig, echo);
KoiShin_Sakana 1:d0cada9a5381 26 }
KoiShin_Sakana 1:d0cada9a5381 27
KoiShin_Sakana 2:a8bbf677e774 28 // return distance from something
KoiShin_Sakana 2:a8bbf677e774 29 int Ultrasonic::measureDist(void) {
KoiShin_Sakana 1:d0cada9a5381 30 wait(0.1);
KoiShin_Sakana 2:a8bbf677e774 31 int dist = hcsr04->distance();
KoiShin_Sakana 2:a8bbf677e774 32 return dist;
KoiShin_Sakana 1:d0cada9a5381 33 }
KoiShin_Sakana 1:d0cada9a5381 34
KoiShin_Sakana 1:d0cada9a5381 35 #endif
KoiShin_Sakana 1:d0cada9a5381 36
KoiShin_Sakana 2:a8bbf677e774 37 /**
KoiShin_Sakana 1:d0cada9a5381 38 * example program
KoiShin_Sakana 1:d0cada9a5381 39
KoiShin_Sakana 1:d0cada9a5381 40 #include "mbed.h"
KoiShin_Sakana 1:d0cada9a5381 41 #include "ultrasonic.h"
KoiShin_Sakana 1:d0cada9a5381 42
KoiShin_Sakana 1:d0cada9a5381 43 int main(void) {
KoiShin_Sakana 1:d0cada9a5381 44 Ultrasonic ultrasonic(D12, D13);
KoiShin_Sakana 1:d0cada9a5381 45 while (1) {
KoiShin_Sakana 2:a8bbf677e774 46 pc.printf("%d\r\n", ultrasonic.measureDist());
KoiShin_Sakana 1:d0cada9a5381 47 }
KoiShin_Sakana 1:d0cada9a5381 48 }
KoiShin_Sakana 2:a8bbf677e774 49 */