Fading joystick values

I am attempting to control a couple of motors using an analog joystick, arduino UNO, and a sabertooth speed controller. I am using the PWM output of the arduino to direct the speed controller, and while that part is functioning, the motion of the motors is very sensitive to sudden movements, causing the device to have jerky controls if you aren't very careful about easing the joystick into position. What i figure might work is using the fade function to fade the value the joystick gives (anywhere from 0 to 1023) so that it gradually ramps up or down to where the joystick is at. Any ideas on how i can work it so that no matter what position the joystick is coming from/going to it can slowly fade to that value?

Somethign like:

const int OutputUpdateInterval = 50; //ms between output updates
const byte OutputStepSize = 2; //Ramp value, 1000/OutputUpdateInterval * OutputStepSize = steps per second

unsigned int lastOutputUpdate; //when did the last output update happened?
int lastOutputValue;



unsigned int nowMillis= millis();
if(nowMillis - lastOutputUpdate >= OutputUpdateInterval){
  lastOutputUpdate = nowMillis; //save it for the next time
  
  int joystickValue = analogRead(JoystickPin) / 4; //255 max for pwm
  
  //do we need to go bigger?
  if(joystickValue > lastOutputValue){
    //Do we overshoot if we add the stepsize?
    if(lastOutputValue + OutputStepSize > joystickValue){
      lastOutputValue = joystickValue;
    }
    //if not, use stepsize
    else{
      lastOutputValue += OutputStepSize;
    }
  }
  else if(joystickValue < lastOutputValue){
    //Do we undershoot if we add the stepsize?
    if(lastOutputValue - OutputStepSize < joystickValue){
      lastOutputValue = joystickValue;
    }
    //if not, use stepsize
    else{
      lastOutputValue -= OutputStepSize;
    }
  }
  
  //update output
  analogWrite(lastOutputValue);
}

thanks. that looks like it would work but to be honest its way over my head, i am very new to arduino. I tried fitting it into my code but i cant get anything to work. any ideas what i will need to get this portion of code to work?

I was just trying to understand what your code does and how it works so i made this, but it doesnt seem to do anything.

int JoystickPin =(A0);
int serialone =(9);
int joystickValue =0;

void setup() {
 
 Serial.begin(9600);          //  setup serial
 pinMode(serialone, OUTPUT);  //  set serialone as output 

}

void loop() {

const int OutputUpdateInterval = 50; //ms between output updates
const byte OutputStepSize = 2; //Ramp value, 1000/OutputUpdateInterval * OutputStepSize = steps per second

unsigned int lastOutputUpdate; //when did the last output update happened?
int lastOutputValue;



unsigned int nowMillis= millis();
if(nowMillis - lastOutputUpdate >= OutputUpdateInterval){
 lastOutputUpdate = nowMillis; //save it for the next time
 
 int joystickValue = analogRead(JoystickPin) / 4; //255 max for pwm
 
 //do we need to go bigger?
 if(joystickValue > lastOutputValue){
   //Do we overshoot if we add the stepsize?
   if(lastOutputValue + OutputStepSize > joystickValue){
     lastOutputValue = joystickValue;
   }
   //if not, use stepsize
   else{
     lastOutputValue += OutputStepSize;
   }
 }
 else if(joystickValue < lastOutputValue){
   //Do we undershoot if we add the stepsize?
   if(lastOutputValue - OutputStepSize < joystickValue){
     lastOutputValue = joystickValue;
   }
   //if not, use stepsize
   else{
     lastOutputValue -= OutputStepSize;
   }
 }
 
 //update output
 analogWrite(serialone, lastOutputValue);
}

}

If you add code tags I'll have a look :wink:

sorry, i dont know what that is or how to do it, but id like to.

Have a look at the How to use this forum sticky on top of this section :wink:

I figured it out. sorry about that.

int JoystickPin =(A0);
int serialone =(9);
int joystickValue =0;

void setup() {
  
  Serial.begin(9600);          //  setup serial
  pinMode(serialone, OUTPUT);  //  set serialone as output 

}

void loop() {

const int OutputUpdateInterval = 50; //ms between output updates
const byte OutputStepSize = 2; //Ramp value, 1000/OutputUpdateInterval * OutputStepSize = steps per second

unsigned int lastOutputUpdate; //when did the last output update happened?
int lastOutputValue;



unsigned int nowMillis= millis();
if(nowMillis - lastOutputUpdate >= OutputUpdateInterval){
  lastOutputUpdate = nowMillis; //save it for the next time
  
  int joystickValue = analogRead(JoystickPin) / 4; //255 max for pwm
  
  //do we need to go bigger?
  if(joystickValue > lastOutputValue){
    //Do we overshoot if we add the stepsize?
    if(lastOutputValue + OutputStepSize > joystickValue){
      lastOutputValue = joystickValue;
    }
    //if not, use stepsize
    else{
      lastOutputValue += OutputStepSize;
    }
  }
  else if(joystickValue < lastOutputValue){
    //Do we undershoot if we add the stepsize?
    if(lastOutputValue - OutputStepSize < joystickValue){
      lastOutputValue = joystickValue;
    }
    //if not, use stepsize
    else{
      lastOutputValue -= OutputStepSize;
    }
  }
  
  //update output
  analogWrite(serialone, lastOutputValue);
}

}

This is for an important project but i'm a mechanical engineer, not a programmer. This kind of thing is interesting to me but i dont know how to go about it, so i'm sorry for the trouble. Currently using just one axis on the joystick as i try and figure out how to do this but eventually i will have one joystick, controlling two motors tank style. the speed controller can handle the motors, i just need to figure out how to give values to it with ramping, so it doesn't speed up or slow down to fast. I appreciate you looking at this and helping me out.

Sorry to keep you waiting. By the time you posted I was off (to bed or something). Next day I totally forget it!

Biggest thing I notice, the upper part of my code must be global, otherwise it will be re-initialized at the start of each iteration of the loop aka not remembering anything. (You could also make them static, nice if you make a separate function).

joystickValue on the other hand doesn't need to be global.

serialone is a very bad name... Like, why is it called serial? Isn't it a motor? So MotorPin would be better.

So an edit (note: it's untested! There might be a bug in it's about the global idea)

const byte JoystickPin = A0; //const byte, not changing and byte is large enough
const byte MotorPin = 9; //what where () doing here? And serial???
int joystickValue = 0;

const int OutputUpdateInterval = 50; //ms between output updates
const byte OutputStepSize = 2; //Ramp value, 1000/OutputUpdateInterval * OutputStepSize = steps per second

unsigned int lastOutputUpdate; //when did the last output update happened?
int lastOutputValue;

void setup() {
  
  Serial.begin(9600);          //  setup serial
  pinMode(MotorPin, OUTPUT);  //  set as output

}

void loop() {
  unsigned int nowMillis= millis();
  if(nowMillis - lastOutputUpdate >= OutputUpdateInterval){
    lastOutputUpdate = nowMillis; //save it for the next time
    
    int joystickValue = analogRead(JoystickPin) / 4; //255 max for pwm
    
    //do we need to go bigger?
    if(joystickValue > lastOutputValue){
      //Do we overshoot if we add the stepsize?
      if(lastOutputValue + OutputStepSize > joystickValue){
        lastOutputValue = joystickValue;
      }
      //if not, use stepsize
      else{
        lastOutputValue += OutputStepSize;
      }
    }
    else if(joystickValue < lastOutputValue){
      //Do we undershoot if we add the stepsize?
      if(lastOutputValue - OutputStepSize < joystickValue){
        lastOutputValue = joystickValue;
      }
      //if not, use stepsize
      else{
        lastOutputValue -= OutputStepSize;
      }
    }
    
    //update output
    analogWrite(MotorPin, lastOutputValue);
  }
}