Translated comments into English. Added serial display through PC using TeraTerm

Dependencies:   TextLCD mbed

Fork of Ultrasonico_HC-SR04 by Icaro Brito

Revision:
1:0244dd451883
Parent:
0:34f7d9ef2f4b
--- a/main.cpp	Wed Nov 12 17:53:57 2014 +0000
+++ b/main.cpp	Tue Jul 14 09:32:34 2015 +0000
@@ -1,50 +1,73 @@
-/* Programa para utilização do sensor Ultrasom
-*/
-/*Interrupções de pinos,só são possíveis nas portas A e D*/
+/* Program for using Ultrasonic Sensor - Modified by Moorthy Muthukrishnan.
+    April 2015.  */
+/* Trigger is supplied through Port E, Pin 5 for sending the pulse
+   Uses interrupt in Port A, pin 16  to determine the pulse width  */
 
 #include "mbed.h"
-
- Serial pc(USBTX,USBRX);  //Configuração da comunicação serial para enviar o valor do sensor
- DigitalOut trig(PTE5,0);  //Configuração do pino de Trigger
- InterruptIn echo(PTA16);   //Configuração da interrupção por pino de Echo 
- Timer tempo;
- float tdist=0, distcm=0, distin=0, dist0=0;
+#include "TextLCD.h"
 
-    void iniP(){            //Rotina para receber o pulso inicial do pino Echo
-        tempo.start();      //Rotina para iniciar o contador
-        return;
-        }
-        
-    void finP(){ //Rotina para pegar o tempo final do pulso
+ Serial pc(USBTX,USBRX);  //Serial port to communicate with the PC
+ TextLCD lcd(PTA13, PTD5, PTD0, PTD1, PTD2, PTD3, TextLCD::LCD16x2); // RS, E, D4-D7
+ DigitalOut trig(PTE5,0);  //Configuring the pin for Trigger
+ InterruptIn echo(PTA16);   //Configuring the pin for Echo 
+ Timer temp;
+ float tdist=0, distcm=0, distin=0, dist0=0;
     
-        tdist = tempo.read_us();  //Leitura do tempo transcorrido
-        distcm = tdist/58;         //Cálculo da distância detectada em "cm"
-        distin = tdist/148;        //Cálculo da distância detectada em "in"
-        
-        tempo.stop();           //Páro o temporizador
-        tempo.reset();          //Reset para o próximo ciclo
-        return;
-        }
+// Function for starting the timer to mark the first edge of echo
+void iniP()
+{            
+    temp.start();      
+    return;
+}
+
+//Function for stopping the timer to mark the end of echo pulse      
+void finP()
+{ 
+    
+    tdist = temp.read_us();  //Read the timer in Microseconds
+    distcm = tdist/58;       //Calculate the distance in "cm"
+    distin = tdist/148;      //Calculate the distance in "inch"
         
-        
-        
-    int main(){
+    temp.stop();           //Stop the timer
+    temp.reset();          //Reset the timer
+    return;
+}
+             
+int main()
+{
+    pc.printf("Starting the Distance sensor\n"); 
+    lcd.locate(0, 0);
+    lcd.printf("Distance sensor\n");
         
-        while(1){
+    while(1)
+    {  
+        trig=1;             //make the trigger = 1
+        wait_us(10);        // 10us pulse
+        trig=0;             //Make trigger = 0
             
-            trig=1;             //Inicio do trigger
-            wait_us(10);        // 10us de pulso
-            trig=0;             //Fim do trigger
+        echo.rise(&iniP);   //Rising edte of the pulse
+        echo.fall(&finP);   //Falling edge of the pulse
             
-            echo.rise(&iniP);   //leitura do inicio do pulso de retorno
-            echo.fall(&finP);   //Leitura do final do pulso de retorno
-            
-            if(distcm != dist0){ //rotina para evitar que se envie muitos valores
-                dist0 = distcm;
-                printf("Distancia detectada pelo sensor %.2f cm \n",distcm); 
+        // display the distance in cm
+        if(distcm != dist0)
+        { 
+            dist0 = distcm;
+            if (distcm < 3000.0)
+            {
+                pc.printf("Distance detected by the sensor %.2f cm \n\n",distcm);
+                lcd.locate(0,1);
+                lcd.printf("Distance=%.2fcm\n", distcm);
             }
-           wait_ms(60);     //rotina para evitar que o pulso de "trigger" seja confundido com o "echo"           
+            else 
+            {
+                 pc.printf("Error in distance measurement \n\n");
+                 lcd.locate(0,1);
+                 lcd.printf("Error\n");
+            }   
         }
+           wait_ms(3000);     //Wait before sending the next pulse           
+    }
              
-         
-    }
\ No newline at end of file
+}
+
+// End of main 
\ No newline at end of file