SEU10f / Mbed 2 deprecated p2_analogin

Dependencies:   mbed

Fork of p2_analogin by Sergio Moyano

hello.cpp

Committer:
Sersd
Date:
2015-09-29
Revision:
0:00529fca1abf

File content as of revision 0:00529fca1abf:

// Flash an LED while a DigitalIn is true
 
#include "mbed.h"
 
AnalogIn   ain(p15);
Serial pc(USBTX, USBRX); // tx, rx

float media=0;
float alpha=0.99;
float actual=0;
float offset=0.001;

int contador=0;

char state =0;
char prev_state=0;
int timeini;
int timefi;
Timer timer;
int main() {
  
    media= ain.read();
    timer.start();
    while(1) {
        actual=ain;
      //  pc.printf("actual %f \n\r",actual);
       // pc.printf("media %f \n\r",media);
        media=media*alpha+(1-alpha)*actual;
        if((actual-media) > offset){
            state=1;
            if(prev_state==0){
                if(contador==0){
                      timeini = timer.read_us();  
                      contador++; 
                }else if(contador==1){   
                      timefi = timer.read_us();  
                      pc.printf("Periode %d \n\r",timefi-timeini);
                      contador=0;
                }
            }
        }else if((actual-media) < offset){
            state=0;
            
        }
        prev_state = state;
        wait(0.0002);
    }
}