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
Revision 1:01ecd819e4ee, committed 2016-07-07
- Comitter:
- yruiewyrui3
- Date:
- Thu Jul 07 12:54:59 2016 +0000
- Parent:
- 0:0bda99bb39a4
- Commit message:
- .
Changed in this revision
HCSR04.cpp | Show annotated file Show diff for this revision Revisions of this file |
HCSR04.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 0bda99bb39a4 -r 01ecd819e4ee HCSR04.cpp --- a/HCSR04.cpp Tue Oct 18 14:32:12 2011 +0000 +++ b/HCSR04.cpp Thu Jul 07 12:54:59 2016 +0000 @@ -1,41 +1,37 @@ #include "HCSR04.h" #include "mbed.h" - - -HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {} - -// Trigger Echo -// _______ _____________,,,,,,,,, -// ____| 10us |_________| 150us-25ms, or 38ms if no obstacle -// - -//return echo duration in us (refer to digram above) -long HCSR04::echo_duration() { - timer.reset(); - trig = 0; - wait_us(2); - trig = 1; +/* +*HCSR04.cpp +*/ +HCSR04::HCSR04(PinName t, PinName e, long to) : trig(t), echo(e), time_out(to){} + long HCSR04::echo_duration() { + + timer.reset(); //reset timer + trig=0; // trigger low + wait_us(2); // wait + trig=1; // trigger high wait_us(10); - trig = 0; - while(echo == 0); + trig=0; // trigger low + while(!echo); // start pulseIN timer.start(); - while(echo == 1); + while(echo) + { + if(timer.read_us()>time_out) + break; + } timer.stop(); - return timer.read_us(); + + long czas=timer.read_us(); + if(czas<300) + return 0; + return timer.read_us(); + } - -//return distance to nearest obstacle or returns -1 -//if no obstacle within range -//set sys to cm or inch accordingly -long HCSR04::distance(int sys){ + +//return distance in cm + long HCSR04::distance(){ duration = echo_duration(); - if(duration > 30000) - return -1; - distacne_cm = duration /29 / 2 ; - distance_inc = duration / 74 / 2; - if (sys) - return distacne_cm; - else - return distance_inc; -} + distance_cm = (duration*34)/1000/2 ; + return distance_cm; +} \ No newline at end of file
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