done
HCSR04.cpp@2:9daf23ed9d84, 2015-02-05 (annotated)
- Committer:
- Nestordp
- Date:
- Thu Feb 05 23:37:56 2015 +0000
- Revision:
- 2:9daf23ed9d84
- Parent:
- 0:d1d7bb1c1f6c
Carrinho Labirinto
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nestordp | 0:d1d7bb1c1f6c | 1 | #include "HCSR04.h" |
Nestordp | 0:d1d7bb1c1f6c | 2 | #include "mbed.h" |
Nestordp | 0:d1d7bb1c1f6c | 3 | |
Nestordp | 0:d1d7bb1c1f6c | 4 | |
Nestordp | 0:d1d7bb1c1f6c | 5 | HCSR04::HCSR04(PinName trigger, PinName echo) : _t(trigger), _e(echo) { |
Nestordp | 0:d1d7bb1c1f6c | 6 | wait(0.1); |
Nestordp | 0:d1d7bb1c1f6c | 7 | } |
Nestordp | 0:d1d7bb1c1f6c | 8 | |
Nestordp | 0:d1d7bb1c1f6c | 9 | float HCSR04::getCm(void){ |
Nestordp | 0:d1d7bb1c1f6c | 10 | distcm = readEcho()/58; |
Nestordp | 0:d1d7bb1c1f6c | 11 | return distcm; |
Nestordp | 0:d1d7bb1c1f6c | 12 | } |
Nestordp | 0:d1d7bb1c1f6c | 13 | |
Nestordp | 0:d1d7bb1c1f6c | 14 | float HCSR04::getIn(void){ |
Nestordp | 0:d1d7bb1c1f6c | 15 | distin = readEcho()/148; |
Nestordp | 0:d1d7bb1c1f6c | 16 | return distin; |
Nestordp | 0:d1d7bb1c1f6c | 17 | } |
Nestordp | 0:d1d7bb1c1f6c | 18 | |
Nestordp | 0:d1d7bb1c1f6c | 19 | float HCSR04::readEcho(void){ |
Nestordp | 2:9daf23ed9d84 | 20 | _t = 1; //Inicio do trigger |
Nestordp | 0:d1d7bb1c1f6c | 21 | wait_us(10); //10us de pulso |
Nestordp | 2:9daf23ed9d84 | 22 | _t = 0; //Fim do trigger |
Nestordp | 2:9daf23ed9d84 | 23 | _tempo.reset(); //Reset para o próximo ciclo |
Nestordp | 0:d1d7bb1c1f6c | 24 | while(!_e); |
Nestordp | 0:d1d7bb1c1f6c | 25 | _tempo.start(); |
Nestordp | 0:d1d7bb1c1f6c | 26 | while(_e); |
Nestordp | 2:9daf23ed9d84 | 27 | _tempo.stop(); //Paro o temporizador |
Nestordp | 0:d1d7bb1c1f6c | 28 | tdist = _tempo.read_us(); //Leitura do tempo transcorrido |
Nestordp | 2:9daf23ed9d84 | 29 | //_tempo.reset(); //Reset para o próximo ciclo |
Nestordp | 0:d1d7bb1c1f6c | 30 | return tdist; |
Nestordp | 0:d1d7bb1c1f6c | 31 | } |