Arduino Linear Actuator Joystick Control - Please Help

I am a programmer working on a project involving an Arduino Mega 2560, a Microsoft Sidewinder Force Feedback 2 Joystick, and a Firgelli Automations OS Series linear actuator.

My goal is to create a program that will move the actuator a particular distance, based on the position of the joystick.

I have successfully written functions to extend and retract the actuator a specified distance. But, I am having issues with joystick integration.

The readings from the potentiometers on the joystick are not steady, causing the actuator to be jumpy. I have attempted to write a code so that the actuators only move if the joystick has moved past a certain threshold from its previous position. The issue that I am running into is that the joystick value is constantly being updated by the loop, so there is never a big enough separation between the previous and current values to cause the actuator to move.

Does anybody have any ideas as to how to write a threshold code that will work like this? Preferably without using PID control.

Any help would be much appreciated!

Save the last value you actually set the position to, and use that to detect change.

Something like:

int lastpos=-999; // erroneous value more than thresh away from any realistic value
int thresh=20;

loop()
{
    int currpos=joystick.get();
    if abs(currpos-lastpos) > thresh {
        lastpos=currpos;
    }
    actuator.set(lastpos);
}

Another way to stabilize the joystick without losing minor changes held for a long time is to keep a running, decaying average:

int avgpos=-999; // erroneous value for avgpos which succeeds if statement below
int weight=0.1;

loop()
{
    int currpos=joystick.get();
    if (avgpos < -1) { // first pass only
        avgpos = curpos;
    } else {
        avgpos = avgpos * (1-weight) + currpos * weight;
    }
    actuator.set(avgpos);
}

This last bit of code averages out the “recent” joystick positions. After it stabilizes, the most recent is weighed 0.1, the one before it 0.09, the ones before that 0.081, 0.0729, 0.06561, and so forth, each preceding point weighed 90% of the previous one. A larger weight will result in faster tracking; a smaller weight in less jitter. A weight of one is no jitter control at all; a weight of zero will always use the first value.

OBTW, both examples assume that the scales of joystick.get() and actuator.set() are the same; otherwise some rescaling is required.

Since he’s on an Arduino, it has a built-in map() function.

I had almost forgot about this because I never used this function; it didn’t do the integer math right when I started using Arduinos about six years ago. I see now that it was fixed in 2014.

It’s annoying because there’s no map() function for floats, so you either have to overload it, or just copy/paste their map equation to a new function and just change ints to floats.

It’s pretty useful, especially when converting analog inputs to some sort of output

Another way to stabilize the joystick without losing minor changes held for a long time is to keep a running, decaying average

Thanks for your help, I will test it out and see how it works. I was also thinking about wiring a capacitor into the circuit to stabilize the readings electrically. Thoughts?