Ultralyd

Dependencies:   mbed SRF05

Fork of Ultralydsensor by Fredrik Bjørken

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Fredrik.cpp Source File

Fredrik.cpp

00001 /*
00002 #include "mbed.h"
00003 
00004 DigitalOut TriggerPin(p17);  //Sender signal
00005 DigitalIn EchoPin(p18);     //Mottar signal
00006 DigitalIn  Br(p15);
00007 DigitalOut myled(LED1);
00008 int valg;
00009 float avstand;
00010 void flushSerialBuffer(void);
00011 Timer tid;
00012 
00013 Serial pc(USBTX, USBRX);
00014 BusOut bargraph(p21,p22,p23,p24,p25,p26,p27,p28, p29, p30);
00015 
00016 Timer echo;   //Timer for avlesing av echosignal.
00017 //Sender ut ett trigger signal på 10us 
00018 void triggerfunc()
00019 {
00020     TriggerPin = 1;
00021     wait_us(10);
00022     TriggerPin = 0;
00023 }
00024 
00025 
00026 main()
00027 {
00028     pc.printf("Tast 1 for avstandsmaaling: \n ");
00029     pc.printf("Tast 2 for bevegelsesmaaling: \n ");
00030     pc.printf("Tast 3 for hastighetsmaaling: \n ");
00031     pc.scanf("%d",&valg);
00032 
00033     int meny=valg;
00034 
00035 
00036 
00037     switch (meny) {
00038         case 1:
00039             while(1) {
00040                 triggerfunc();
00041                 while(!EchoPin);
00042                 echo.start();
00043                 while(EchoPin);
00044                 echo.stop();
00045                 if(EchoPin==0) {
00046 
00047                     pc.printf(" tiden lyden brukte er %f s. \n",echo.read()/2);
00048                     avstand=(echo.read()* 340* 100) /2;
00049                     pc.printf(" Avstanden er %.2f \n",avstand);
00050                 }
00051                 pc.printf("trykk en knapp for flere maalinger \n");
00052                 flushSerialBuffer();
00053                 while(!pc.getc()==' ')
00054                     {}
00055                 echo.reset();
00056             }
00057 
00058 
00059         case 2:
00060 
00061             float tiden;
00062             while(1) {
00063 
00064 
00065                 triggerfunc();
00066                 while(!EchoPin);
00067                 echo.start();
00068                 while(EchoPin);
00069                 echo.stop();
00070                 if(EchoPin==0) {
00071 
00072                     if(tiden==!echo.read_ms()) {
00073                         pc.printf("Bevegelse \n");
00074                     }
00075                     tiden=echo.read_ms();
00076                     echo.reset();
00077                     wait(0.05);
00078 
00079                 }
00080             }
00081 
00082         case 3:
00083 
00084 
00085             float fart;
00086             float avstand;
00087             float avstand2;
00088             Timer tida;
00089             float strekning;
00090             while(1) {
00091                 
00092                 tida.start();
00093                 triggerfunc();
00094                 while(!EchoPin);
00095                 echo.start();
00096                 while(EchoPin);
00097                 echo.stop();
00098 
00099                 //Fart = avstand / tid
00100                 
00101                 avstand =(echo.read()* 340* 100) /2;
00102                 float tid1=echo.read();
00103                 echo.reset();
00104                 
00105                 float tida1=tida.read();
00106                 tida.reset();
00107                 tida.start();
00108                 triggerfunc();
00109                 while(!EchoPin);
00110                 echo.start();
00111                 while(EchoPin);
00112                 echo.stop();
00113                 tida.stop();
00114                 
00115                 
00116                 avstand2 =(echo.read()* 340* 100) /2;
00117                 
00118                 strekning= avstand-avstand2;
00119                 fart=strekning/tida.read();
00120                 printf("farten til objektet er : %.4f \n",fart);
00121                 echo.reset();
00122                 wait(1);
00123             }
00124     }
00125 }
00126 
00127 void flushSerialBuffer(void)
00128 {
00129     while (pc.readable()) {
00130         (void) pc.getc();
00131         return;
00132     }
00133 }
00134 */