Library for controlling ultrasonic ranging module HCSR04 Ported by hiawoood from arduino library orgininally created by ITead studio.
Dependents: JakKrisowy JakKrisowy_ golonkaczosniur
Fork of HCSR04 by
Diff: HCSR04.h
- Revision:
- 1:01ecd819e4ee
- Parent:
- 0:0bda99bb39a4
diff -r 0bda99bb39a4 -r 01ecd819e4ee HCSR04.h --- a/HCSR04.h Tue Oct 18 14:32:12 2011 +0000 +++ b/HCSR04.h Thu Jul 07 12:54:59 2016 +0000 @@ -1,39 +1,49 @@ -//Library for controlling ultrasonic module HCSR04 -//Ported by hiawoood from arduino library orgininally created by ITead studio. -//Instantiate object by supplying the proper pin numbers of "trigger" and "echo" -//e.g. -/* - int main() { - Ultrasonic sensor(p5, p6); - while(1){ - long distance = sensor.distance(CM); - printf("Distance:%d\n"); - wait(0.1); - } - } +/* File: HCSR04.h + * Author: Antonio Buonanno + *Board: STM NUCLEO F401RE, + *Hardware: Ultrasonic Range HC-SR04, + * + *This work derived from Arduino library, + * + * Desc: driver for HCSR04 Ultrasonic Range Finder. The returned range + * is in units of meters. + * + * + * */ - +/* EXAMPLE +#include "mbed.h" +#include "hcsr04.h" -#ifndef HCSR04_H -#define HCSR04_H - +//D12 TRIGGER D11 ECHO + HCSR04 sensor(D12, D11); +int main() { + while(1) { + + long distance = sensor.distance(); + printf("distanza %d \n",distance); + wait(1.0); // 1 sec + + } +} +*/ +#ifndef hcsr04_H +#define hcsr04_H #include "mbed.h" -#define CM 1 -#define INC 0 class HCSR04 { public: - HCSR04(PinName t, PinName e); + HCSR04(PinName t, PinName e, long to); long echo_duration(); - long distance(int sys); - + long distance(); + private: DigitalOut trig; DigitalIn echo; Timer timer; - long duration,distacne_cm,distance_inc; + long duration,distance_cm,time_out; }; - + #endif \ No newline at end of file