Translated comments into English. Added serial display through PC using TeraTerm
Fork of Ultrasonico_HC-SR04 by
main.cpp@1:0244dd451883, 2015-07-14 (annotated)
- Committer:
- nmoorthy2001
- Date:
- Tue Jul 14 09:32:34 2015 +0000
- Revision:
- 1:0244dd451883
- Parent:
- 0:34f7d9ef2f4b
Translated the comments into English
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nmoorthy2001 | 1:0244dd451883 | 1 | /* Program for using Ultrasonic Sensor - Modified by Moorthy Muthukrishnan. |
nmoorthy2001 | 1:0244dd451883 | 2 | April 2015. */ |
nmoorthy2001 | 1:0244dd451883 | 3 | /* Trigger is supplied through Port E, Pin 5 for sending the pulse |
nmoorthy2001 | 1:0244dd451883 | 4 | Uses interrupt in Port A, pin 16 to determine the pulse width */ |
icarobrito | 0:34f7d9ef2f4b | 5 | |
icarobrito | 0:34f7d9ef2f4b | 6 | #include "mbed.h" |
nmoorthy2001 | 1:0244dd451883 | 7 | #include "TextLCD.h" |
icarobrito | 0:34f7d9ef2f4b | 8 | |
nmoorthy2001 | 1:0244dd451883 | 9 | Serial pc(USBTX,USBRX); //Serial port to communicate with the PC |
nmoorthy2001 | 1:0244dd451883 | 10 | TextLCD lcd(PTA13, PTD5, PTD0, PTD1, PTD2, PTD3, TextLCD::LCD16x2); // RS, E, D4-D7 |
nmoorthy2001 | 1:0244dd451883 | 11 | DigitalOut trig(PTE5,0); //Configuring the pin for Trigger |
nmoorthy2001 | 1:0244dd451883 | 12 | InterruptIn echo(PTA16); //Configuring the pin for Echo |
nmoorthy2001 | 1:0244dd451883 | 13 | Timer temp; |
nmoorthy2001 | 1:0244dd451883 | 14 | float tdist=0, distcm=0, distin=0, dist0=0; |
icarobrito | 0:34f7d9ef2f4b | 15 | |
nmoorthy2001 | 1:0244dd451883 | 16 | // Function for starting the timer to mark the first edge of echo |
nmoorthy2001 | 1:0244dd451883 | 17 | void iniP() |
nmoorthy2001 | 1:0244dd451883 | 18 | { |
nmoorthy2001 | 1:0244dd451883 | 19 | temp.start(); |
nmoorthy2001 | 1:0244dd451883 | 20 | return; |
nmoorthy2001 | 1:0244dd451883 | 21 | } |
nmoorthy2001 | 1:0244dd451883 | 22 | |
nmoorthy2001 | 1:0244dd451883 | 23 | //Function for stopping the timer to mark the end of echo pulse |
nmoorthy2001 | 1:0244dd451883 | 24 | void finP() |
nmoorthy2001 | 1:0244dd451883 | 25 | { |
nmoorthy2001 | 1:0244dd451883 | 26 | |
nmoorthy2001 | 1:0244dd451883 | 27 | tdist = temp.read_us(); //Read the timer in Microseconds |
nmoorthy2001 | 1:0244dd451883 | 28 | distcm = tdist/58; //Calculate the distance in "cm" |
nmoorthy2001 | 1:0244dd451883 | 29 | distin = tdist/148; //Calculate the distance in "inch" |
icarobrito | 0:34f7d9ef2f4b | 30 | |
nmoorthy2001 | 1:0244dd451883 | 31 | temp.stop(); //Stop the timer |
nmoorthy2001 | 1:0244dd451883 | 32 | temp.reset(); //Reset the timer |
nmoorthy2001 | 1:0244dd451883 | 33 | return; |
nmoorthy2001 | 1:0244dd451883 | 34 | } |
nmoorthy2001 | 1:0244dd451883 | 35 | |
nmoorthy2001 | 1:0244dd451883 | 36 | int main() |
nmoorthy2001 | 1:0244dd451883 | 37 | { |
nmoorthy2001 | 1:0244dd451883 | 38 | pc.printf("Starting the Distance sensor\n"); |
nmoorthy2001 | 1:0244dd451883 | 39 | lcd.locate(0, 0); |
nmoorthy2001 | 1:0244dd451883 | 40 | lcd.printf("Distance sensor\n"); |
icarobrito | 0:34f7d9ef2f4b | 41 | |
nmoorthy2001 | 1:0244dd451883 | 42 | while(1) |
nmoorthy2001 | 1:0244dd451883 | 43 | { |
nmoorthy2001 | 1:0244dd451883 | 44 | trig=1; //make the trigger = 1 |
nmoorthy2001 | 1:0244dd451883 | 45 | wait_us(10); // 10us pulse |
nmoorthy2001 | 1:0244dd451883 | 46 | trig=0; //Make trigger = 0 |
icarobrito | 0:34f7d9ef2f4b | 47 | |
nmoorthy2001 | 1:0244dd451883 | 48 | echo.rise(&iniP); //Rising edte of the pulse |
nmoorthy2001 | 1:0244dd451883 | 49 | echo.fall(&finP); //Falling edge of the pulse |
icarobrito | 0:34f7d9ef2f4b | 50 | |
nmoorthy2001 | 1:0244dd451883 | 51 | // display the distance in cm |
nmoorthy2001 | 1:0244dd451883 | 52 | if(distcm != dist0) |
nmoorthy2001 | 1:0244dd451883 | 53 | { |
nmoorthy2001 | 1:0244dd451883 | 54 | dist0 = distcm; |
nmoorthy2001 | 1:0244dd451883 | 55 | if (distcm < 3000.0) |
nmoorthy2001 | 1:0244dd451883 | 56 | { |
nmoorthy2001 | 1:0244dd451883 | 57 | pc.printf("Distance detected by the sensor %.2f cm \n\n",distcm); |
nmoorthy2001 | 1:0244dd451883 | 58 | lcd.locate(0,1); |
nmoorthy2001 | 1:0244dd451883 | 59 | lcd.printf("Distance=%.2fcm\n", distcm); |
icarobrito | 0:34f7d9ef2f4b | 60 | } |
nmoorthy2001 | 1:0244dd451883 | 61 | else |
nmoorthy2001 | 1:0244dd451883 | 62 | { |
nmoorthy2001 | 1:0244dd451883 | 63 | pc.printf("Error in distance measurement \n\n"); |
nmoorthy2001 | 1:0244dd451883 | 64 | lcd.locate(0,1); |
nmoorthy2001 | 1:0244dd451883 | 65 | lcd.printf("Error\n"); |
nmoorthy2001 | 1:0244dd451883 | 66 | } |
icarobrito | 0:34f7d9ef2f4b | 67 | } |
nmoorthy2001 | 1:0244dd451883 | 68 | wait_ms(3000); //Wait before sending the next pulse |
nmoorthy2001 | 1:0244dd451883 | 69 | } |
icarobrito | 0:34f7d9ef2f4b | 70 | |
nmoorthy2001 | 1:0244dd451883 | 71 | } |
nmoorthy2001 | 1:0244dd451883 | 72 | |
nmoorthy2001 | 1:0244dd451883 | 73 | // End of main |