Sergio Moyano / Mbed 2 deprecated p2_analogin23r

Dependencies:   mbed

Fork of p2_analoginrty by Sergio Moyano

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hello.cpp Source File

hello.cpp

00001 // Flash an LED while a DigitalIn is true
00002  
00003 #include "mbed.h"
00004  
00005 AnalogIn   ain(p15);
00006 Serial pc(USBTX, USBRX); // tx, rx
00007 
00008 float media=0;
00009 float alpha=0.99;
00010 float actual=0;
00011 float offset=0.001;
00012 
00013 int contador=0;
00014 
00015 char state =0;
00016 char prev_state=0;
00017 int timeini;
00018 int timefi;
00019 Timer timer;
00020 int main() {
00021   
00022     media= ain.read();
00023     timer.start();
00024     while(1) {
00025         actual=ain;
00026       //  pc.printf("actual %f \n\r",actual);
00027        // pc.printf("media %f \n\r",media);
00028         media=media*alpha+(1-alpha)*actual;
00029         if((actual-media) > offset){
00030             state=1;
00031             if(prev_state==0){
00032                 if(contador==0){
00033                       timeini = timer.read_us();  
00034                       contador++; 
00035                 }else if(contador==1){   
00036                       timefi = timer.read_us();  
00037                       pc.printf("Periode %d \n\r",timefi-timeini);
00038                       contador=0;
00039                 }
00040             }
00041         }else if((actual-media) < offset){
00042             state=0;
00043             
00044         }
00045         prev_state = state;
00046         wait(0.0002);
00047     }
00048 }