ultrasonic

Dependencies:   EthernetNetIf HTTPServer TextLCD mbed

Fork of ethernet_test_http by Yasushi TAUCHI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "TextLCD.h"
00003 #include "mbed.h"
00004 #include "adc.h"
00005 #include "EthernetNetIf.h"
00006 #include "HTTPServer.h"
00007 
00008 #define SAMPLE_RATE     150000
00009 #define NUM 3000
00010 Ticker ticker;
00011 EthernetNetIf ethif(IpAddr(192,168,11,26), IpAddr(255,255,255,0), IpAddr(192,168,11,1), IpAddr(192,168,11,1));
00012     HTTPServer server;
00013     LocalFileSystem local("local");
00014     FILE *fp; 
00015 TextLCD lcd(p24, p26, p27, p28, p29, p30);
00016 int x;
00017 int volt_max;
00018 DigitalOut mled1(LED1);
00019 DigitalOut mled2(LED2);
00020 DigitalOut mled3(LED3);
00021 DigitalOut mled4(LED4);
00022 InterruptIn in_p6(p6);
00023 DigitalIn di(p5); 
00024 DigitalOut check(p8);
00025 
00026 ADC adc(SAMPLE_RATE, 1); 
00027 
00028 int main()
00029 {
00030 int volt[2000];
00031 int a[5];
00032 int n=0, m=0, p=0, v=0, q=0;
00033 int v1, v2, v3;
00034 int min;
00035     check = 0;
00036     
00037     adc.setup(p19,1);
00038             volt_max=0;
00039             mled1 = 1;
00040     Base::add_rpc_class<DigitalOut>();
00041 
00042     lcd.cls();
00043     lcd.locate(0,0);
00044     lcd.printf("Program init..  ");
00045 
00046     if (ethif.setup()) {
00047         error("Ethernet setup failed.");
00048         return 1;
00049     }
00050     
00051     wait(1);
00052     server.addHandler<SimpleHandler>("/hello");
00053     server.addHandler<RPCHandler>("/rpc");
00054     FSHandler::mount("/local", "/");
00055     server.addHandler<FSHandler>("/");
00056     server.bind(80);
00057     
00058 while(1){
00059     n=0; q=0; p=0; m=0; v=0;
00060 if(di){ 
00061             mled2= 1;
00062             for(n=1;n<2000;n++){
00063                 adc.select(p19);         // Measure pin 19
00064                 adc.start();             // Start ADC conversion
00065                 while(!adc.done(p19));   // Wait for it to complete
00066                 volt[n]=adc.read(p19);
00067                 mled3 = 1;
00068                         }
00069          mled4=1;
00070          mled1=mled3=0;                                        //while loop end
00071          
00072          volt_max=1;
00073             for( n = 100; n < 1800; n++ ){
00074                     if(volt_max<volt[n]*3300/4096){
00075                     volt_max=volt[n]*3300/4096;
00076                     m=n;
00077                     }  
00078         if(v==0){
00079             if(volt_max > 570){
00080                     p=m;
00081                     v=1;
00082                     x=p*100/610;
00083                                 }
00084                 }    
00085                     }      
00086     v1=3720.9*exp(-0.009*x);
00087     v2=2589.6*exp(-0.035*x);
00088     v3=828.39*exp(-0.041*x); 
00089     
00090     a[0]=abs(volt_max-v1);
00091     a[1]=abs(volt_max-v2);
00092     a[2]=abs(volt_max-v3);
00093     
00094     if(x > 29 && x < 151){    
00095     min=a[0];
00096     for(n = 1 ; n < 3 ; n++ ){
00097         if(min > a[n]){
00098             min=a[n];
00099             q=n;
00100             }
00101         }
00102     lcd.cls();        
00103 /*    if(q==0){
00104                 lcd.locate(0,0);
00105                 lcd.printf("acrylic plate");
00106             }
00107         else if(q==1){
00108                 lcd.locate(0,0);
00109                 lcd.printf("towel");
00110                       }
00111         else if(q==2){
00112                 lcd.locate(0,0);
00113                 lcd.printf("sponge");
00114                       } 
00115             }
00116     else { 
00117           lcd.locate(0,0);
00118           lcd.printf("Undeterminable");
00119           }                    */
00120         lcd.locate(0,1);
00121         lcd.printf("%d[mV]  %d[cm]",volt_max,x);
00122         if (x>50 && x<120) check = 1;
00123         else check = 0;
00124         }
00125         }
00126     }}