Stepper motor and variables not working together

Hello everybody! I am trying to make a force feedback wheel and I am almost done! I am using a stepper motor, and I know that it's not the right choice for a ffb wheel, and a better dc motor is coming soon, but for now I want to take some measurements and test it. When I give the stepper motor a specific speed it does work, but when I replace it with a variable it just doesn't rotate and not the serial monitor nor the game detects and potentiometer movement. It doesn't take any more data from the potentiometer. I've been trying to troubleshoot this myself for the past 10 days and I just cannot get it to work, and when I tell you I tried everything I knew I mean it. Nothing worked.
Here is the code:

#include <Stepper.h>

const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);

  long value1 = 0;
  int value = 0;
  #include "Joystick.h"

  //X-axis & Y-axis REQUIRED
  Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
                    JOYSTICK_TYPE_GAMEPAD,  //JOYSTICK, GAMEPAD, MULTI_AXIS
                    4, 0,                   //Button Count, Hat Switch Count
                    true, true, true,       //X,Y,Z
                    true, false, false,    //Rx,Ry,Rz
                    false, false, true,    //Rudder,Throttle,Accelerator
                    false, true);          //Brake,Steering

Gains mygains[3];
EffectParams myeffectparams[3];
int32_t forces[3] = { 0 };


void setup() {
  pinMode(A5, INPUT);
  pinMode(13, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(4, OUTPUT);

  pinMode(A5, INPUT);

  //Joystick.setXAxisRange(0, 1023);
  //Steering wheel
  Joystick.setXAxisRange(0, 1023);
  Joystick.setZAxisRange(0, 1023);
  //set X Axis gains
  mygains[0].totalGain = 90;   //0-100
  mygains[0].springGain = 90; 

  //enable gains REQUIRED
  Joystick.setGains(mygains);
  Joystick.begin();
  Serial.begin(9600);






  digitalWrite(13, HIGH);

  Serial.begin(9600);
}


void loop() {
 myeffectparams[0].springMaxPosition = 1023;
  myeffectparams[0].springPosition = analogRead(A5);  //-512-512
  Joystick.setEffectParams(myeffectparams);
  Joystick.getForce(forces);
  Joystick.setXAxis(analogRead(A5));
value = abs(forces[0]);
value1 = map(value,0,255,0,160);
if(forces[0] > 0) {

  myeffectparams[0].springMaxPosition = 1023;
  myeffectparams[0].springPosition = analogRead(A5);  //-512-512
  Joystick.setEffectParams(myeffectparams);
  Joystick.getForce(forces);
  Joystick.setXAxis(analogRead(A5));
value = abs(forces[0]);
value1 = map(value,0,255,0,160);
  myStepper.setSpeed(value1);
  myStepper.step(1);
}else if(forces[0] < 0){

  myeffectparams[0].springMaxPosition = 1023;
  myeffectparams[0].springPosition = analogRead(A5);  //-512-512
  Joystick.setEffectParams(myeffectparams);
  Joystick.getForce(forces);
  Joystick.setXAxis(analogRead(A5));
value = abs(forces[0]);
value1 = map(value,0,255,0,160);

  myStepper.setSpeed(value1);
  myStepper.step(-1);
}else{

   myeffectparams[0].springMaxPosition = 1023;
  myeffectparams[0].springPosition = analogRead(A5);  //-512-512
  Joystick.setEffectParams(myeffectparams);
  Joystick.getForce(forces);
  Joystick.setXAxis(analogRead(A5));
value = abs(forces[0]);
long value1 = map(value,0,255,0,160);

 myStepper.setSpeed(value1);
   myStepper.step(1);
     Serial.println(analogRead(A5));
     
}


  delay(1);
}

I know that this code is an absolute mess and I know that there is a lot of unnecessary stuff like the beginning of the loop ,but I've tried everything. I tried to cover it for every case scenario.

At this point, snippits of both codes that you used will help. What worked and what did not work? Can we guess you put the specific speed value into the variable before you called the motor to move?

When I put a specific speed for the motor it would work. For Example
myStepper.setSpeed(100);,
but when I put a variable that constantly changes it doesn't work. For Example
myStepper.setSpeed(value1);

Did you Serial.print value and value1 to make sure they were what you expected?

That is not a good test. Use the same value in the variable that you used in the fixed value test!!!!

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