Smoothing Analog Pots

OK I am a newb trying to wrap up my project. It is a PPM Encoder using a Joystick with 6 analog Pots, read the post, and output them into PPM code to fly an RC plane.

I have all but one piece of the code working pretty good and now want to add th elast peice I am having trouble figuring out. I want to Smooth Analog Inputs A0 thru A5. Bu tI am stuck how to use the Analog Smoothing and piece it together into my code. Can anyone give me a clue. Here is my code:

#include <PulsePosition.h>

PulsePositionOutput PPMOut(FALLING); //PPM signal out

//Set and Name Analog Input Pins A0 - A5 for each channel
const int AI_Pin_AEL = A0; // Ana In - Aeleron potentiometer 
const int AI_Pin_ELE = A1; // Ana In - Elevator potentiometer 
const int AI_Pin_THR = A2; // Ana In - Throttle potentiometer
const int AI_Pin_RUD = A3; // Ana In - Rudder potentiometer
const int AI_Pin_GEAR = A4; // Ana In - Pot or Switch 
const int AI_Pin_AUX = A5; // Ana In - Pot or Switch  

//Set Analog Input Pin Values
int AI_Raw_AEL = 0; // Ana In raw var - 0->1023
int AI_Raw_ELE = 0; // Ana In raw var - 0->1023
int AI_Raw_THR = 0; // Ana In raw var - 0->1023
int AI_Raw_RUD = 0; // Ana In raw var - 0->1023
int AI_Raw_GEAR = 0; // Ana In raw var - 0->1023
int AI_Raw_AUX = 0; // Ana In raw var - 0->1023

//AI_Analog Output Values to each channel via Write command
int AI_AEL; // Ana In var - 0->1023 compensated
int AI_ELE; // Ana In var - 0->1023 compensated
int AI_THR; // Ana In var - 0->1023 compensated
int AI_RUD; // Ana In var - 0->1023 compensated
int AI_GEAR; // Ana In var - 0->1023 compensated
int AI_AUX; // Ana In var - 0->1023 compensated

void setup() {
  PPMOut.begin(10);  // Starts PPM Output on Pin 10 to send to Trainer Port
 PPMOut.begin(10, 8);// Starts Frame Pin
//Serial.begin(9600);

  }

void loop() {

  analogReadResolution(11);

  //Read Analog Input Voltages
 AI_Raw_AEL = analogRead(AI_Pin_AEL);
 AI_Raw_ELE = analogRead(AI_Pin_ELE);
 AI_Raw_THR = analogRead(AI_Pin_THR);
 AI_Raw_RUD = analogRead(AI_Pin_RUD);
 AI_Raw_GEAR = analogRead(AI_Pin_GEAR);
 AI_Raw_AUX = analogRead(AI_Pin_AUX);

 
// Convert Analog Input Voltage to microseconds for Write Command
  AI_AEL = map(AI_Raw_AEL, 0, 2047, 800, 2200); // Invert Aeleron pot and slight centre offset
  AI_ELE = map(AI_Raw_ELE, 0, 2047, 800, 2200); // Invert Elevator pot and slight centre offset
  AI_THR = map(AI_Raw_THR, 0, 2047, 800, 2200); // Throttle
  AI_RUD = map(AI_Raw_RUD, 0, 2047, 800, 2200); // Rudder
  AI_GEAR = map(AI_Raw_GEAR, 0, 2047, 800, 2200); // Thermal Intelligence pot (TI)
  AI_AUX = map(AI_Raw_AUX, 0, 2047, 800, 2200); // Thermal Intelligence pot (TI)

  PPMOut.write(1, AI_AEL);
  PPMOut.write(2, AI_ELE);
  PPMOut.write(3, AI_THR);
  PPMOut.write(4, AI_RUD);
  PPMOut.write(5, AI_GEAR);
  PPMOut.write(6, AI_AUX);

// Serial.print("AI_AEL = ");
//Serial.print(AI_AEL);
delay (2);
}

I want to smooth these inputs

const int AI_Pin_AEL = A0; // Ana In - Aeleron potentiometer
const int AI_Pin_ELE = A1; // Ana In - Elevator potentiometer
const int AI_Pin_THR = A2; // Ana In - Throttle potentiometer
const int AI_Pin_RUD = A3; // Ana In - Rudder potentiometer
const int AI_Pin_GEAR = A4; // Ana In - Pot or Switch
const int AI_Pin_AUX = A5; // Ana In - Pot or Switch

THX in advance.

If you can afford to call PPMOut.write() less frequently, you could e.g. get 5 values, calculate the average and then write that out.

Alternatively, you could build a moving average with an array.

Start by hardware (RC) smoothing them before sampling if you haven't already done so. Then if more smoothing is required there a loads of algorithms for digital filtering.

A simple moving average filter is commonly used. It's not necessarily the best in performance, but certainly easy to implement and understand.

The following is a simple IIR filter (that approximates moving average of the previous 16 readings) without requiring any array storage.

// initialize current_av with first analog reading
// then after each analog read just do the following
current_av = (current_av * 15 + current_sample) >> 4;

tuxduino:
Alternatively, you could build a moving average with an array.

Well I think that is what I am asking how to do. I looked at the Smoothing Example in Analog and it is an array. Problem is I have never written or worked with an Array. Tried looking at some videos and blogs and not getting how to set an Array up. I know I want 4 elements, but have no clue how to write it in the code.

For smoothing, I like to use this:

double smoothedValue;

void someFunc() {
  smoothedValue = .75 * smoothedValue + .25 * analogRead(A0);
}

An important point is to keep the sample rate regular. One way to do this is like so:

double smoothedValue;
byte mostRecentSample();

void someFunc() {
  byte prevSample = mostRecentSample;
  mostRecentSample = (int)(millis() >> 3);
  if(mostRecentSample != prevSample) {
    smoothedValue = .75 * smoothedValue + .25 * analogRead(A0);
  }
}

This will take a sample no more than once every 8 millis.