Potentiometer Fluctuate and Smoothing

Hi all, first of all i am trying to build a 3 axis throttle unit for flight simulation, using 3x 10k potentiometers and Arduino pro micro. İ’m following a build guide that has a code for this project. I soldered all connections to arduino and pot pins. Uploaded the code and it works really well. However there is a fluctuate on pots values that causes levers to move up and down by 2 or 3 value in game. I did some research on the net and found “smoothing” values by average values. I tried doing this method but couldnt do that unfortunately. How can i write those “average values” code to my code. Can you help me? :slight_smile:

The code is this:

#include "Joystick.h"

// Potentiometer pins (Make sure these matches your configuration);

  int throttle_pin = A2;
  int prop_pin = A1;
  int mixt_pin = A0;

// Calibration (These are the only ones you're going to change)

  float throttle_lever_full_up = 0;
  float throttle_lever_down_notch = 253;
  float throttle_lever_full_down = 330;

  float prop_lever_full_up = 0;
  float prop_lever_full_down = 284;

  float cutoff_lever_full_up = 1023;
  float cutoff_lever_full_down = 693;

// Throttle Quadrant Arrays

  int throttle_array[3];
  int throttle_array_old[3];
  
// Throttle Lever and Reverse Button

  float thr_max = 1023 - throttle_lever_full_up;
  float thr_min = 1023 - throttle_lever_down_notch;
  float deadzone = 7; // Analog units for deadzone.
  float rev_min = thr_min - deadzone;
  float rev_max = 1023 - throttle_lever_full_down;
  float reverso = 0; //Toggle Reverse Button

// Propeller
  int prop_max = 1023 - prop_lever_full_up;
  int prop_min = 1023 - prop_lever_full_down;

// Mixture
  int mixt_max = cutoff_lever_full_up;
  int mixt_min = cutoff_lever_full_down;
  

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  1, 0,                 // Button Count, Hat Switch Count
  false, false, false,   // No Axis
  true, true, false,   // Rx, Ry and no Rz
  false, true,          // No Rudder and throttle
  false, false, false);  // No accelerator, brake or steering

void setup() {

  //Initialize Joystick Library
  Joystick.begin();

  // Initializes serial port.
  //Serial.begin(9600);
  // Waits for serial port to connect (needed for Leonardo only).
  //while (!Serial);

  // Joystick Axes Setup

   // Throttle
   Joystick.setThrottleRange(thr_min, thr_max);
   // Prop
   Joystick.setRxAxisRange(prop_min, prop_max);
   // Mixture/Cutoff
   Joystick.setRyAxisRange(mixt_min, mixt_max);
  
}

void loop() {
  potentiometers();
}

void potentiometers() {

  throttle_array[0] = 1023 - analogRead(throttle_pin);
  throttle_array[1] = 1023 - analogRead(prop_pin);
  throttle_array[2] = analogRead(mixt_pin);

  //Serial.print(analogRead(throttle_pin));
  //Serial.print(" ");
  //Serial.print(analogRead(prop_pin));
  //Serial.print(" ");
  //Serial.println(analogRead(mixt_pin));
  
    if (throttle_array[0] != throttle_array_old[0]) {
      if (throttle_array[0] >= thr_min) {
        // Toggles the reverse button when it exits the threshold (1st time)
        if (reverso == 1) {
          Joystick.setButton(0, 1);
          delay(50);
          Joystick.setButton(0, 0);
        }
        Joystick.setThrottle(throttle_array[0]);
        reverso = 0;
      }
      if (throttle_array[0] <= rev_min) {
        // Toggles the reverse button when it enters the threshold (1st time)
        if (reverso == 0) {
          Joystick.setButton(0, 1);
          delay(50);
          Joystick.setButton(0, 0);
        }

        // Interpolates the Reverse part to the Throttle forward part.
        float rev = thr_min+((rev_min - throttle_array[0])/(rev_min - rev_max)*(thr_max - thr_min));
        Joystick.setThrottle(rev);
        reverso = 1;
      }
      throttle_array_old[0] = throttle_array[0];
    }

    if (throttle_array[1] != throttle_array_old[1]) {
      Joystick.setRxAxis(throttle_array[1]);
      throttle_array_old[1] = throttle_array[1];
    }

    if (throttle_array[2] != throttle_array_old[2]) {
      Joystick.setRyAxis(throttle_array[2]);
      throttle_array_old[2] = throttle_array[2];
    }
    
}

I suggest you take a look at this page.

pesimistanblue,

Did you work out the solution from the information Pylon gave you. He gave you some really great information about RunningAverage.h and I'm interested in knowing if it worked with your pots.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.