a

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HCSR04.cpp Source File

HCSR04.cpp

00001 #include "HCSR04.h"
00002 #include "mbed.h"
00003  
00004  
00005 HCSR04::HCSR04(PinName trigger, PinName echo) : _t(trigger), _e(echo) {
00006     wait(0.1);
00007     }
00008  
00009 float HCSR04::getCm(void){
00010     distcm = readEcho()/58;
00011     return distcm;
00012     }
00013  
00014 float HCSR04::getIn(void){
00015     distin = readEcho()/148;
00016     return distin;
00017     }
00018  
00019 float HCSR04::readEcho(void){     
00020     _t=1;                       //Inicio do trigger
00021     wait_us(10);                //10us de pulso
00022     _t=0;                       //Fim do trigger
00023     while(!_e);
00024     _tempo.start();
00025     while(_e);
00026     tdist = _tempo.read_us();   //Leitura do tempo transcorrido 
00027     _tempo.stop();              //Paro o temporizador
00028     _tempo.reset();             //Reset para o próximo ciclo
00029     return tdist;
00030 }