Jan Hrebec / Mbed 2 deprecated senzorpriblizeni

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // autor: Jan Hřebec
00002 
00003 // snímač vzdálenosti (není to moc hezký kód)
00004 
00005 #include "mbed.h"
00006 
00007 #define SAMPLE 200
00008 
00009 DigitalOut led(PA_3);  //pin9  LED
00010 DigitalOut buzz(PA_4); //pin10 reproduktor
00011 AnalogIn foto(PA_5);   //pin11 vstup fototranzistoru
00012 
00013 int main()
00014 {
00015     float prumerBez[SAMPLE];
00016     float prumerS[SAMPLE];
00017     int ukazatelBez = 0;
00018     int ukazatelS = 0;
00019     int cas = 0;
00020     int perioda = 0;
00021     while(1) {               //hlavní smyčka programu
00022         wait_us(10);
00023         if(cas % 200 == 0){ //blikani ledkou
00024             led = 0;
00025         }
00026         if(cas % 200 == 100){
00027             led = 1;
00028         }
00029         if(cas%10 == 5){ //zapis hodnoty do prumeru
00030             if(cas % 200 < 100){
00031                 prumerBez[ukazatelBez] = foto;
00032                 ukazatelBez = (ukazatelBez + 1) % SAMPLE;    
00033             } else {
00034                 prumerS[ukazatelS] = foto;
00035                 ukazatelS = (ukazatelS + 1) % SAMPLE;
00036             }
00037         }
00038         if(perioda == 0){ //slabý signál, reproduktor nehraje
00039             buzz = 0;
00040         } else { // reproduktor hraje
00041             if(cas % perioda > perioda / 2){
00042                 buzz = 0;
00043             } else {
00044                 buzz = 1;
00045             }
00046         }
00047         if(cas % 200 == 0){ // aktualizace frekvence reproduktoru
00048             float prumer = 0;
00049             for (int i = 0; i < SAMPLE; i++){
00050                 prumer += prumerS[i];
00051                 prumer -= prumerBez[i];
00052             }
00053             if (prumer > 0.5){
00054                 prumer = sqrt(prumer + 1) * 10;
00055                 perioda = (float)1000 / prumer;
00056             } else {
00057                 perioda = 0;
00058             }
00059         }
00060         cas++;
00061     }
00062 }