Program using a HC-SR04 Ultrasonic sensor.

Dependencies:   mbed

Fork of Ultrasonico_HC-SR04 by Icaro Brito

Just a translation to english, used for Youth club of electronic in Roznov

Committer:
b34196
Date:
Wed Dec 20 14:40:23 2017 +0000
Revision:
1:ad8ec262e808
Parent:
0:34f7d9ef2f4b
english version used to play with kids in Roznov

Who changed what in which revision?

UserRevisionLine numberNew contents of line
b34196 1:ad8ec262e808 1 /* Program for Ultrasonic sensor use
icarobrito 0:34f7d9ef2f4b 2 */
b34196 1:ad8ec262e808 3 /*Pin interruptions are only possible with ports A and D*/
icarobrito 0:34f7d9ef2f4b 4
icarobrito 0:34f7d9ef2f4b 5 #include "mbed.h"
icarobrito 0:34f7d9ef2f4b 6
b34196 1:ad8ec262e808 7 Serial pc(USBTX,USBRX); //Configuration of the serial communication to send the value of the sensor
b34196 1:ad8ec262e808 8 DigitalOut trig(PTE3,0); //Trigger pin configuration
b34196 1:ad8ec262e808 9 InterruptIn echo(PTA16); //Echo pin interrupt configuration
icarobrito 0:34f7d9ef2f4b 10 Timer tempo;
icarobrito 0:34f7d9ef2f4b 11 float tdist=0, distcm=0, distin=0, dist0=0;
icarobrito 0:34f7d9ef2f4b 12
b34196 1:ad8ec262e808 13 void iniP(){ //Routine to receive the initial pulse of the Echo pin
b34196 1:ad8ec262e808 14 tempo.start(); //Routine to start the counter
icarobrito 0:34f7d9ef2f4b 15 return;
icarobrito 0:34f7d9ef2f4b 16 }
icarobrito 0:34f7d9ef2f4b 17
b34196 1:ad8ec262e808 18 void finP(){ //Routine to get the end time of the pulse
icarobrito 0:34f7d9ef2f4b 19
b34196 1:ad8ec262e808 20 tdist = tempo.read_us(); //Reading the elapsed time
b34196 1:ad8ec262e808 21 distcm = tdist/58; // distance "cm"
b34196 1:ad8ec262e808 22 distin = tdist/148; // distance "in"
icarobrito 0:34f7d9ef2f4b 23
b34196 1:ad8ec262e808 24 tempo.stop(); //Stop the timer
b34196 1:ad8ec262e808 25 tempo.reset(); //reset to next cycle
icarobrito 0:34f7d9ef2f4b 26 return;
icarobrito 0:34f7d9ef2f4b 27 }
icarobrito 0:34f7d9ef2f4b 28
icarobrito 0:34f7d9ef2f4b 29
icarobrito 0:34f7d9ef2f4b 30
icarobrito 0:34f7d9ef2f4b 31 int main(){
b34196 1:ad8ec262e808 32 printf("Ultrasonic ranging test \n");
icarobrito 0:34f7d9ef2f4b 33 while(1){
icarobrito 0:34f7d9ef2f4b 34
b34196 1:ad8ec262e808 35 trig=1; //trigger start
b34196 1:ad8ec262e808 36 wait_us(10); // 10us puls
b34196 1:ad8ec262e808 37 trig=0; //end of trigger
icarobrito 0:34f7d9ef2f4b 38
b34196 1:ad8ec262e808 39 echo.rise(&iniP); //return pulse start reading
b34196 1:ad8ec262e808 40 echo.fall(&finP); //Reading the end of the return pulse
icarobrito 0:34f7d9ef2f4b 41
b34196 1:ad8ec262e808 42 if(distcm != dist0){ //routine to avoid sending too many values
icarobrito 0:34f7d9ef2f4b 43 dist0 = distcm;
b34196 1:ad8ec262e808 44 printf("Distance detected by sensor %.2f cm \r\n",distcm);
icarobrito 0:34f7d9ef2f4b 45 }
b34196 1:ad8ec262e808 46 wait_ms(60); //routine to prevent the trigger pulse from being confused with the "echo"
icarobrito 0:34f7d9ef2f4b 47 }
icarobrito 0:34f7d9ef2f4b 48
icarobrito 0:34f7d9ef2f4b 49
icarobrito 0:34f7d9ef2f4b 50 }