/*############################################################# Program Name : ex1_HCSR04_measure Author : Grant Phillips Date Modified : 15/01/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example program that demonstrates the use of an HC-SR04 ultrasonic distance sensor. The output is displayed on a PC using the Serial Wire Viewer (SWV). See the SWV examples for an explanation of the use of this feature. Requirements : * STM32F4-Discovery Board Circuit : * The HC-SR04 ultrasonic sensor connected as: VCC - +5V TRIG - PB8 ECHO - PA1 GND - GND * A wire link between PB3 and pin6 of the SWD connector (CN2) as explained in the SWV examples section ##############################################################*/ #include "mbed.h" #include "SWO.h" #include "HCSR04.h" SWO_Channel SWO; HCSR04 distance(PB_8, PA_1); //Create a HCSR04 object with TRIG on PB8 and ECHO on PA1. //Measurements will now start automatically in the background. int main() { while(1){ //Use the read_us() function to get this distance pulse in microseconds (us) //or read_cm() to get the distance in centimeters (cm). SWO.printf("Distance = %d (us) %f (cm)\n", distance.read_us(), distance.read_cm()); } }