Create this program

Dependencies:   mbed HCSR04 HMC6352 PID TextLCD

Committer:
KoiShin_Sakana
Date:
Tue Aug 11 13:11:05 2015 +0000
Revision:
6:44c3bfbe2553
Parent:
3:901d18b901b4
update dribbler.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 3:901d18b901b4 17 ~Ultrasonic();
KoiShin_Sakana 2:a8bbf677e774 18 int measureDist(void);
KoiShin_Sakana 2:a8bbf677e774 19
KoiShin_Sakana 1:d0cada9a5381 20 private:
KoiShin_Sakana 1:d0cada9a5381 21 HCSR04* hcsr04;
KoiShin_Sakana 1:d0cada9a5381 22 };
KoiShin_Sakana 1:d0cada9a5381 23
KoiShin_Sakana 2:a8bbf677e774 24 // initialize
KoiShin_Sakana 1:d0cada9a5381 25 Ultrasonic::Ultrasonic(PinName trig, PinName echo) {
KoiShin_Sakana 1:d0cada9a5381 26 hcsr04 = new HCSR04(trig, echo);
KoiShin_Sakana 1:d0cada9a5381 27 }
KoiShin_Sakana 1:d0cada9a5381 28
KoiShin_Sakana 3:901d18b901b4 29 Ultrasonic::~Ultrasonic() {
KoiShin_Sakana 3:901d18b901b4 30 delete hcsr04;
KoiShin_Sakana 3:901d18b901b4 31 }
KoiShin_Sakana 3:901d18b901b4 32
KoiShin_Sakana 2:a8bbf677e774 33 // return distance from something
KoiShin_Sakana 2:a8bbf677e774 34 int Ultrasonic::measureDist(void) {
KoiShin_Sakana 1:d0cada9a5381 35 wait(0.1);
KoiShin_Sakana 2:a8bbf677e774 36 int dist = hcsr04->distance();
KoiShin_Sakana 2:a8bbf677e774 37 return dist;
KoiShin_Sakana 1:d0cada9a5381 38 }
KoiShin_Sakana 1:d0cada9a5381 39
KoiShin_Sakana 1:d0cada9a5381 40 #endif
KoiShin_Sakana 1:d0cada9a5381 41
KoiShin_Sakana 2:a8bbf677e774 42 /**
KoiShin_Sakana 1:d0cada9a5381 43 * example program
KoiShin_Sakana 1:d0cada9a5381 44
KoiShin_Sakana 1:d0cada9a5381 45 #include "mbed.h"
KoiShin_Sakana 1:d0cada9a5381 46 #include "ultrasonic.h"
KoiShin_Sakana 1:d0cada9a5381 47
KoiShin_Sakana 1:d0cada9a5381 48 int main(void) {
KoiShin_Sakana 1:d0cada9a5381 49 Ultrasonic ultrasonic(D12, D13);
KoiShin_Sakana 1:d0cada9a5381 50 while (1) {
KoiShin_Sakana 2:a8bbf677e774 51 pc.printf("%d\r\n", ultrasonic.measureDist());
KoiShin_Sakana 1:d0cada9a5381 52 }
KoiShin_Sakana 1:d0cada9a5381 53 }
KoiShin_Sakana 2:a8bbf677e774 54 */