Using two Ticker fucntion

21 Mar 2011

I am using two ticker function in this program. The variable avg[0] to avg[5]and count are getteing reset when void read_ADC2(). Any mistake is there? Kindly help me..

<<code>>#include <mbed.h>

AnalogIn ain_UseA(p15); AnalogIn ain_UseB(p16); AnalogIn ain_UseC(p17); AnalogIn ain_UseD(p18); AnalogIn ain_UseE(p19); AnalogIn ain_UseF(p20);

Ticker ADC; float count1=0; float vdiv = (3.3 / 65535); float avg[6] = {0,0,0,0,0,0}; void read_ADC1(); void read_ADC2(); Serial uart(p26, p25); float ai[6];

int main() {

uart.baud(9600); uart.format(8,Serial::None,1);

count1=0;

ADC.attach(&read_ADC1, 3.0); ADC.attach(&read_ADC2, 15.0);

while( 1 ) { }

} void read_ADC1()

{ ++count1;

avg[0] = (avg[0]+(float)ain_UseA.read_u16()* vdiv); avg[1] = (avg[1]+(float)ain_UseB.read_u16()* vdiv); avg[2] = (avg[2]+(float)ain_UseC.read_u16()* vdiv); avg[3] = (avg[3]+(float)ain_UseD.read_u16()* vdiv); avg[4] = (avg[4]+(float)ain_UseE.read_u16()* vdiv); avg[5] = (avg[5]+(float)ain_UseF.read_u16()* vdiv);

}

void read_ADC2() { ai[0]=avg[0]/count1; ai[1]=avg[1]/count1; ai[2]=avg[2]/count1; ai[3]=avg[3]/count1; ai[4]=avg[4]/count1; ai[5]=avg[5]/count1; uart.printf("%d AI1:%5.3f\r\nAI2:%5.3f\r\nAI3:%5.3f\r\nAI4:%5.3f\r\nAI5:%5.3f\r\nAI6:%5.3f\r\n",count1, ai[0],ai[1],ai[2],ai[3],ai[4],ai[5]); }

<</code>>

21 Mar 2011

ADC.attach(&read_ADC1, 3.0); 
ADC.attach(&read_ADC2, 15.0); 

You are not using two tickers, you are using one. The second ADC.attach(&read_ADC2, 15.0); means read_ADC1() is never called as you have replaced the ticker callback with a new function.

What you want is:-

Ticker ADC1;
Ticker ADC2;

int main() {
    ADC1.attach(&read_ADC1, 3.0); 
    ADC2.attach(&read_ADC2, 15.0); 
}

That's two tickers :)