Esp8266

Here is my median filter, if anyone cares to use it. Note that you don’t want thousands of measurements, but if you get the occasional glitch, this will cure your problem.

For fast changing measurements, you don’t want to use this.

CAYENNE_OUT(V0)
{
  float m = MedianRead(A0);

  //convert to voltage
  m = 3.3 * m / 1024.0;
  
  Cayenne.virtualWrite(V0, m);
}


//define how many measurements to make
#define MAX_BUF 11

int MedianRead(int chan) 
{
   int buffer[MAX_BUF]; 
   int i, j; 
   int temp; 

   //get some data
   for (i=0; i<MAX_BUF; i++) 
   {
     buffer[i] = analogRead(chan);
     delay(1); 
   }
    
   //sort em
   for (j=0; j<MAX_BUF-1; j++) 
   {
     for (i=0; i<MAX_BUF-1- j;i++)
     {
       if (buffer[i]>buffer[i+1])
       {
         temp = buffer[i]; 
         buffer[i]=buffer[i+1]; 
         buffer[i+1] = temp; 
       } 
     } 
   } 

   //return the one in the middle
   return buffer[(MAX_BUF-1)/2];
}

Cheers,

Craig