done

Dependents:   AwesomeAlarm_BLE

Committer:
yungsung
Date:
Thu Jan 09 19:18:48 2020 +0000
Revision:
5:4ec03bbadf69
Parent:
4:be89035b2c42
done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nestordp 3:12d6d751f4fc 1 /**
Nestordp 3:12d6d751f4fc 2 * @author Nestor Pereira-Neto Team: SalvadorEngenharia
Nestordp 3:12d6d751f4fc 3 *
Nestordp 3:12d6d751f4fc 4 * @section LICENSE
Nestordp 3:12d6d751f4fc 5 *
Nestordp 3:12d6d751f4fc 6 * Copyright (c) 2010 ARM Limited
Nestordp 3:12d6d751f4fc 7 *
Nestordp 3:12d6d751f4fc 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
Nestordp 3:12d6d751f4fc 9 * of this software and associated documentation files (the "Software"), to deal
Nestordp 3:12d6d751f4fc 10 * in the Software without restriction, including without limitation the rights
Nestordp 3:12d6d751f4fc 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Nestordp 3:12d6d751f4fc 12 * copies of the Software, and to permit persons to whom the Software is
Nestordp 3:12d6d751f4fc 13 * furnished to do so, subject to the following conditions:
Nestordp 3:12d6d751f4fc 14 *
Nestordp 3:12d6d751f4fc 15 * The above copyright notice and this permission notice shall be included in
Nestordp 3:12d6d751f4fc 16 * all copies or substantial portions of the Software.
Nestordp 3:12d6d751f4fc 17 *
Nestordp 3:12d6d751f4fc 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Nestordp 3:12d6d751f4fc 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Nestordp 3:12d6d751f4fc 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Nestordp 3:12d6d751f4fc 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Nestordp 3:12d6d751f4fc 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Nestordp 3:12d6d751f4fc 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Nestordp 3:12d6d751f4fc 24 * THE SOFTWARE.
Nestordp 3:12d6d751f4fc 25 *
Nestordp 3:12d6d751f4fc 26 * @section DESCRIPTION
Nestordp 3:12d6d751f4fc 27 *
Nestordp 3:12d6d751f4fc 28 * Biblioteca para uso do sonar HC-SR04, possui rotinas para permitir medições
Nestordp 3:12d6d751f4fc 29 * de distências retornado valores em centímetros, polegadas ou o tempo
Nestordp 3:12d6d751f4fc 30 * percorrido pelo som (em microsegundos).
Nestordp 3:12d6d751f4fc 31 * Esta biblioteca pode ser usada com qualquer pino do microcontrolador.
Nestordp 3:12d6d751f4fc 32 */
Nestordp 3:12d6d751f4fc 33
Nestordp 3:12d6d751f4fc 34
Nestordp 0:d1d7bb1c1f6c 35 #ifndef HCSR04_H
Nestordp 0:d1d7bb1c1f6c 36 #define HCSR04_H
Nestordp 0:d1d7bb1c1f6c 37
Nestordp 0:d1d7bb1c1f6c 38 #include "mbed.h"
Nestordp 0:d1d7bb1c1f6c 39
Nestordp 0:d1d7bb1c1f6c 40
Nestordp 0:d1d7bb1c1f6c 41
Nestordp 3:12d6d751f4fc 42 /**
Nestordp 3:12d6d751f4fc 43 * Sonar HC-SR04 example.
Nestordp 0:d1d7bb1c1f6c 44 * @code
Nestordp 0:d1d7bb1c1f6c 45 * #include "mbed.h"
Nestordp 0:d1d7bb1c1f6c 46 * #include "HCSR04.h"
Nestordp 0:d1d7bb1c1f6c 47 *
Nestordp 0:d1d7bb1c1f6c 48 * DigitalOut myled(LED1);
Nestordp 0:d1d7bb1c1f6c 49 * Serial pc(USBTX,USBRX);
Nestordp 0:d1d7bb1c1f6c 50 *
Nestordp 3:12d6d751f4fc 51 * HCSR04 sonar(PTD5, PTA13);
Nestordp 0:d1d7bb1c1f6c 52 *
Nestordp 0:d1d7bb1c1f6c 53 * int main() {
Nestordp 0:d1d7bb1c1f6c 54 * while(1) {
Nestordp 3:12d6d751f4fc 55 * printf("Distancia detectada pelo sensor Frente %.2f cm \n", sonar.getCm());
Nestordp 0:d1d7bb1c1f6c 56 * wait_ms(1000);
Nestordp 0:d1d7bb1c1f6c 57 * }
Nestordp 0:d1d7bb1c1f6c 58 * }
Nestordp 0:d1d7bb1c1f6c 59 *
Nestordp 0:d1d7bb1c1f6c 60 * @endcode
Nestordp 0:d1d7bb1c1f6c 61 */
Nestordp 0:d1d7bb1c1f6c 62
Nestordp 0:d1d7bb1c1f6c 63 class HCSR04 {
Nestordp 0:d1d7bb1c1f6c 64 public:
Nestordp 3:12d6d751f4fc 65 /** Constructor, create HCSR04 instance
Nestordp 0:d1d7bb1c1f6c 66 *
Nestordp 0:d1d7bb1c1f6c 67 * @param trigger TRIG pin
Nestordp 0:d1d7bb1c1f6c 68 * @param echo ECHO pin
Nestordp 0:d1d7bb1c1f6c 69 */
Nestordp 0:d1d7bb1c1f6c 70 HCSR04(PinName trigger, PinName echo);
Nestordp 0:d1d7bb1c1f6c 71
Nestordp 4:be89035b2c42 72 /** It make a reading of the sonar Faz uma leitura do sonar
Nestordp 0:d1d7bb1c1f6c 73 *
Nestordp 0:d1d7bb1c1f6c 74 * @returns Tempo do pulso echo em microsegundos*/
Nestordp 0:d1d7bb1c1f6c 75 float readEcho(void);
Nestordp 0:d1d7bb1c1f6c 76
Nestordp 4:be89035b2c42 77 /** It messures the distance in centimeter "cm"
Nestordp 0:d1d7bb1c1f6c 78 *
Nestordp 4:be89035b2c42 79 *@returns Distance in centimeter
Nestordp 0:d1d7bb1c1f6c 80 */
Nestordp 0:d1d7bb1c1f6c 81 float getCm(void);
Nestordp 0:d1d7bb1c1f6c 82
Nestordp 0:d1d7bb1c1f6c 83 /** Mede a distência em polegadas "in"
Nestordp 0:d1d7bb1c1f6c 84 *
Nestordp 0:d1d7bb1c1f6c 85 *@returns Distência em in*/
Nestordp 0:d1d7bb1c1f6c 86 float getIn(void);
Nestordp 0:d1d7bb1c1f6c 87
Nestordp 0:d1d7bb1c1f6c 88 private:
Nestordp 0:d1d7bb1c1f6c 89 float tdist; //Leitura do tempo transcorrido
Nestordp 3:12d6d751f4fc 90 float distcm; //Guarda o valor da distanciância em centímetros
Nestordp 3:12d6d751f4fc 91 float distin; //Guarda o valor da distência em polegadas
Nestordp 0:d1d7bb1c1f6c 92
Nestordp 0:d1d7bb1c1f6c 93 DigitalOut _t; //Configuração do pino de Trigger
Nestordp 0:d1d7bb1c1f6c 94 DigitalIn _e; //Configuração do pino de Echo
Nestordp 3:12d6d751f4fc 95 Timer _tempo; //Cria um objeto timer
Nestordp 0:d1d7bb1c1f6c 96
Nestordp 0:d1d7bb1c1f6c 97 };
Nestordp 0:d1d7bb1c1f6c 98
Nestordp 0:d1d7bb1c1f6c 99 #endif