han back
/
CLEO_ULTRASONIC
SMART CLEO Ultrasonic
Diff: main.cpp
- Revision:
- 0:e7c4d23dbc05
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Sep 28 03:44:12 2017 +0000 @@ -0,0 +1,36 @@ +#include "mbed.h" +#include "TextLCD.h" +#include "HCSR04.h" + +PinName pin_ECHO = PB_7; +PinName pin_TRIG = PB_10; + +HCSR04 Ultrasonic(pin_ECHO, pin_TRIG); + +// rs, rw, e, d0-d3 +TextLCD lcd(PB_12, PB_13, PB_14, PB_15, PA_9, PA_10, PA_11); + +float Ultrasonic_cm(void); + +int main() { + uint16_t dist = 0; + + lcd.printf(" Ultrasonic\n"); + lcd.printf(" Dist : [cm]"); + + while(1) { + dist = Ultrasonic_cm(); + lcd.locate(8, 1); + lcd.printf("%3d", dist); + wait(0.5); + } +} + +float Ultrasonic_cm(void) +{ + Ultrasonic.startMeasurement(); + + while(!Ultrasonic.isNewDataReady()); + + return Ultrasonic.getDistance_cm(); +} \ No newline at end of file