Spikes in ADC values - how to eleminate

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Spikes in ADC values - how to eleminate

Post by michap »

Using the ADC 0..3 in Z-Uno for long period I have got some spikes from time to time.

It can be a problem if the value is going out of the needed range - that's why I have changed the procedure for measure the values.

If you see such "spikes" and want to eleminate it - you can use the statistical "median" method.
Get (as sample) 5 values, sort it and take the middle of them. Spikes will be outsorted.

Here is a sample code for 5 readings - maybe it is useful for somebody ;)
It's easy to modify the code for other needs.

Code: Select all

static const byte adc[] = {A0,A1,A2,A3};
...
vA0 = vADC(0);
vA1 = vADC(1);
vA2 = vADC(2);
vA3 = vADC(3);
...

word vADC(word adcNum){
// use odd number of readings 5,7,9,....
  word vAD[5];
  for(int n=0; n<5; n++){
    vAD[n]=analogRead(adc[adcNum]);
  }
  return Median(vAD,5);
}

word Median(word *AS1, byte len) {
  byte m;
  byte n=len;
  word temp=0;
  do {
    m=1;
    for(int p=1;p<len;p++){
      if(AS1[p-1]>AS1[p]){
        temp=AS1[p];           
        AS1[p]=AS1[p-1];
        AS1[p-1]=temp;
        m=p;
      } 
    } 
    n=m;
  } while(n>1);
  n=(int)(len/2);
 return AS1[n];
}
Michael
Post Reply