Collin Alexis / Efrei

Files at this revision

API Documentation at this revision

Comitter:
AlexisCollin
Date:
Tue Jun 28 14:00:35 2022 +0000
Commit message:
efrei;

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 000000000000 -r 586f8f975b8c HCSR04.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.cpp	Tue Jun 28 14:00:35 2022 +0000
@@ -0,0 +1,30 @@
+#include "HCSR04.h"
+#include "mbed.h"
+ 
+ 
+HCSR04::HCSR04(PinName trigger, PinName echo) : _t(trigger), _e(echo) {
+    wait(0.1);
+    }
+ 
+float HCSR04::getCm(void){
+    distcm = readEcho()/58;
+    return distcm;
+    }
+ 
+float HCSR04::getIn(void){
+    distin = readEcho()/148;
+    return distin;
+    }
+ 
+float HCSR04::readEcho(void){     
+    _t=1;                       //Inicio do trigger
+    wait_us(10);                //10us de pulso
+    _t=0;                       //Fim do trigger
+    while(!_e);
+    _tempo.start();
+    while(_e);
+    tdist = _tempo.read_us();   //Leitura do tempo transcorrido 
+    _tempo.stop();              //Paro o temporizador
+    _tempo.reset();             //Reset para o próximo ciclo
+    return tdist;
+}
\ No newline at end of file
diff -r 000000000000 -r 586f8f975b8c HCSR04.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.h	Tue Jun 28 14:00:35 2022 +0000
@@ -0,0 +1,65 @@
+#ifndef HCSR04_H
+#define HCSR04_H
+ 
+#include "mbed.h"
+ 
+ 
+ 
+/** 
+ * Sonar HC-SR04 example.
+ * @code
+ * #include "mbed.h"
+ * #include "HCSR04.h"
+ *
+ * DigitalOut myled(LED1);
+ * Serial pc(USBTX,USBRX);     
+ *
+ * HCSR04 sonar(PTD5, PTA13);
+ *
+ * int main() {
+ *     while(1) {
+ *         printf("Distancia detectada pelo sensor Frente %.2f cm \n", sonar.getCm()); 
+ *         wait_ms(1000);
+ *     }
+ * }
+ *
+ * @endcode
+*/
+ 
+class HCSR04 {    
+    public:    
+        /** Constructor, create HCSR04 instance 
+          *
+          * @param trigger TRIG pin
+          * @param echo ECHO pin
+          */
+        HCSR04(PinName trigger, PinName echo);
+ 
+        /** It make a reading of the sonar Faz uma leitura do sonar
+          * 
+          * @returns Tempo do pulso echo em microsegundos*/
+        float readEcho(void);
+        
+        /** It messures the distance in centimeter "cm" 
+          *
+          *@returns Distance in centimeter
+          */
+        float getCm(void);
+        
+        /** Mede a distência em polegadas "in" 
+          *
+          *@returns Distência em in*/
+        float getIn(void);
+        
+    private:
+        float tdist;        //Leitura do tempo transcorrido
+        float distcm;       //Guarda o valor da distanciância em centímetros
+        float distin;       //Guarda o valor da distência em polegadas
+        
+        DigitalOut _t;      //Configuração do pino de Trigger  
+        DigitalIn _e;       //Configuração do pino de Echo
+        Timer _tempo;       //Cria um objeto timer
+        
+};
+ 
+#endif
\ No newline at end of file