done

Dependents:   AwesomeAlarm_BLE

Committer:
Nestordp
Date:
Thu Feb 05 19:00:51 2015 +0000
Revision:
0:d1d7bb1c1f6c
Child:
1:56b2ae08c35e
Child:
3:12d6d751f4fc
Biblioteca para uso do sonar HC-SR04, implementado na FRDM-LZ25Z

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nestordp 0:d1d7bb1c1f6c 1 #ifndef HCSR04_H
Nestordp 0:d1d7bb1c1f6c 2 #define HCSR04_H
Nestordp 0:d1d7bb1c1f6c 3
Nestordp 0:d1d7bb1c1f6c 4 #include "mbed.h"
Nestordp 0:d1d7bb1c1f6c 5
Nestordp 0:d1d7bb1c1f6c 6
Nestordp 0:d1d7bb1c1f6c 7
Nestordp 0:d1d7bb1c1f6c 8 /** Sonar HC-SR04 example.
Nestordp 0:d1d7bb1c1f6c 9 * @code
Nestordp 0:d1d7bb1c1f6c 10 * #include "mbed.h"
Nestordp 0:d1d7bb1c1f6c 11 * #include "HCSR04.h"
Nestordp 0:d1d7bb1c1f6c 12 *
Nestordp 0:d1d7bb1c1f6c 13 * DigitalOut myled(LED1);
Nestordp 0:d1d7bb1c1f6c 14 * Serial pc(USBTX,USBRX);
Nestordp 0:d1d7bb1c1f6c 15 *
Nestordp 0:d1d7bb1c1f6c 16 * HCSR04 sonar(PTB0, PTD3);
Nestordp 0:d1d7bb1c1f6c 17 *
Nestordp 0:d1d7bb1c1f6c 18 * int main() {
Nestordp 0:d1d7bb1c1f6c 19 * while(1) {
Nestordp 0:d1d7bb1c1f6c 20 * printf("Distancia detectada pelo sensor Frente %.2f cm \n", sonar.echo());
Nestordp 0:d1d7bb1c1f6c 21 * wait_ms(1000);
Nestordp 0:d1d7bb1c1f6c 22 * }
Nestordp 0:d1d7bb1c1f6c 23 * }
Nestordp 0:d1d7bb1c1f6c 24 *
Nestordp 0:d1d7bb1c1f6c 25 * @endcode
Nestordp 0:d1d7bb1c1f6c 26 */
Nestordp 0:d1d7bb1c1f6c 27
Nestordp 0:d1d7bb1c1f6c 28 class HCSR04 {
Nestordp 0:d1d7bb1c1f6c 29 public:
Nestordp 0:d1d7bb1c1f6c 30 /** Constructor, create HC_SR04 instance
Nestordp 0:d1d7bb1c1f6c 31 *
Nestordp 0:d1d7bb1c1f6c 32 * @param trigger TRIG pin
Nestordp 0:d1d7bb1c1f6c 33 * @param echo ECHO pin
Nestordp 0:d1d7bb1c1f6c 34 */
Nestordp 0:d1d7bb1c1f6c 35 HCSR04(PinName trigger, PinName echo);
Nestordp 0:d1d7bb1c1f6c 36
Nestordp 0:d1d7bb1c1f6c 37 /** Faz uma leitura do sonar
Nestordp 0:d1d7bb1c1f6c 38 *
Nestordp 0:d1d7bb1c1f6c 39 * @returns Tempo do pulso echo em microsegundos*/
Nestordp 0:d1d7bb1c1f6c 40 float readEcho(void);
Nestordp 0:d1d7bb1c1f6c 41
Nestordp 0:d1d7bb1c1f6c 42 /** Mede a distância em centímetros "cm"
Nestordp 0:d1d7bb1c1f6c 43 *
Nestordp 0:d1d7bb1c1f6c 44 *@returns Distância em cm
Nestordp 0:d1d7bb1c1f6c 45 */
Nestordp 0:d1d7bb1c1f6c 46 float getCm(void);
Nestordp 0:d1d7bb1c1f6c 47
Nestordp 0:d1d7bb1c1f6c 48 /** Mede a distência em polegadas "in"
Nestordp 0:d1d7bb1c1f6c 49 *
Nestordp 0:d1d7bb1c1f6c 50 *@returns Distência em in*/
Nestordp 0:d1d7bb1c1f6c 51 float getIn(void);
Nestordp 0:d1d7bb1c1f6c 52
Nestordp 0:d1d7bb1c1f6c 53 private:
Nestordp 0:d1d7bb1c1f6c 54 float tdist; //Leitura do tempo transcorrido
Nestordp 0:d1d7bb1c1f6c 55 float distcm; //
Nestordp 0:d1d7bb1c1f6c 56 float distin;
Nestordp 0:d1d7bb1c1f6c 57
Nestordp 0:d1d7bb1c1f6c 58 DigitalOut _t; //Configuração do pino de Trigger
Nestordp 0:d1d7bb1c1f6c 59 DigitalIn _e; //Configuração do pino de Echo
Nestordp 0:d1d7bb1c1f6c 60 Timer _tempo;
Nestordp 0:d1d7bb1c1f6c 61
Nestordp 0:d1d7bb1c1f6c 62 };
Nestordp 0:d1d7bb1c1f6c 63
Nestordp 0:d1d7bb1c1f6c 64 #endif