Stepper Motor control with Joystick

Hey!

I'm coding stepper motor and joystick so that I can play this game I created with the arduino but I'm having trouble with something. Below is my code, and everything works fine (when the joystick is moved the stepper motor moves in the same direction at the speed), but I want it so that if I move the joystick a little bit, the stepper motor goes a little slower than the speed, and the more I move it (to the right for example) the faster it gets until its completely to the right and is at the set speed. Can someone help me with this please. Thanks

#define step_pin 25  // Pin 25 connected to Steps pin on EasyDriver
#define dir_pin 23   // Pin 23 connected to Direction pin
#define MS1 28       // Pin 10 connected to MS1 pin
#define MS2 30       // Pin 11 connected to MS2 pin
#define SLEEP 32     // Pin 28 connected to SLEEP pin

#define X_pin A0    // Pin A0 connected to joystick x axis
#define Joy_switch 52  // Pin 4 connected to joystick switch


int step_speed = 10;  // Speed of Stepper motor (higher = slower)

void setup() {
   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(SLEEP, OUTPUT);
   
   pinMode(Joy_switch, INPUT_PULLUP);
   
   digitalWrite(SLEEP, HIGH);  // Wake up EasyDriver
   delay(5);  // Wait for EasyDriver wake up
   

/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/

   digitalWrite(MS1, LOW);      // Configures to Full Steps
   digitalWrite(MS2, LOW);    // Configures to Full Steps
}

void loop() {
  if (!digitalRead(Joy_switch)) {  //  If Joystick switch is clicked
    delay(500);  // delay for deboucing
    switch (step_speed) {  // check current value of step_speed and change it
      case 1:
        step_speed=10;  // slow speed
        break;
      case 3:
        step_speed=1;  // fast speed
        break;
      case 10:
        step_speed=3;  // medium speed
        break;
    }
  }    
    
  if (analogRead(X_pin) > 712) {  //  If joystick is moved Left
      
        digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }      
  
  
    if (analogRead(X_pin) < 312) {  // If joystick is moved right
    
      
        digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }      
  }

void loop()
{
  int X = analogRead(X_pin);

  if (X < 500) // Left?
  {
    step_speed = map(X, 0, 499, 1, 30);  // Fast to slow
    digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(step_pin, HIGH);
    delay(step_speed);
    digitalWrite(step_pin, LOW);
    delay(step_speed);
  }
  else if (X > 525) // Right?
  {
    step_speed = map(X, 526, 1023, 30, 1);  // Slow to fast
    digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(step_pin, HIGH);
    delay(step_speed);
    digitalWrite(step_pin, LOW);
    delay(step_speed);
  }
}

Hi thanks for the reply! This still doesn't work. There aren't the different speeds I can go through and also moving it to the left doesn't work. It's very choppy and moving it to the max right makes it go way too fast. Please help

That sounds strange. Please post the full sketch.

This is the full sketch

Hey!

I'm coding stepper motor and joystick so that I can play this game I created with the arduino but I'm having trouble with something. Below is my code, and everything works fine (when the joystick is moved the stepper motor moves in the same direction at the speed), but I want it so that if I move the joystick a little bit, the stepper motor goes a little slower than the speed, and the more I move it (to the right for example) the faster it gets until its completely to the right and is at the set speed. Can someone help me with this please. Thanks

#define step_pin 25  // Pin 25 connected to Steps pin on EasyDriver
#define dir_pin 23   // Pin 23 connected to Direction pin
#define MS1 28       // Pin 10 connected to MS1 pin
#define MS2 30       // Pin 11 connected to MS2 pin
#define SLEEP 32     // Pin 28 connected to SLEEP pin

#define X_pin A0    // Pin A0 connected to joystick x axis
#define Joy_switch 52  // Pin 4 connected to joystick switch


int step_speed = 10;  // Speed of Stepper motor (higher = slower)

void setup() {
   pinMode(MS1, OUTPUT);
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(SLEEP, OUTPUT);
   
   pinMode(Joy_switch, INPUT_PULLUP);
   
   digitalWrite(SLEEP, HIGH);  // Wake up EasyDriver
   delay(5);  // Wait for EasyDriver wake up
   

/* Configure type of Steps on EasyDriver:
// MS1 MS2
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/

   digitalWrite(MS1, LOW);      // Configures to Full Steps
   digitalWrite(MS2, LOW);    // Configures to Full Steps
}

void loop() {
  if (!digitalRead(Joy_switch)) {  //  If Joystick switch is clicked
    delay(500);  // delay for deboucing
    switch (step_speed) {  // check current value of step_speed and change it
      case 1:
        step_speed=10;  // slow speed
        break;
      case 3:
        step_speed=1;  // fast speed
        break;
      case 10:
        step_speed=3;  // medium speed
        break;
    }
  }    
    
  if (analogRead(X_pin) > 712) {  //  If joystick is moved Left
      
        digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }      
  
  
    if (analogRead(X_pin) < 312) {  // If joystick is moved right
    
      
        digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delay(step_speed);
        digitalWrite(step_pin, LOW);
        delay(step_speed);
    }      
  }

You can use the map() function to convert the range of values returned by analogRead() to the range required for step_speed thus making the speed proportional to the stick movement which is what you want I think

Hi,
Can you show me what you mean by that? I don't really understand

@codingcody0594

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

There isn't one solution, because you don't have one thing you want to do. You want to read different values from the joystick, right? The value means something for you, not only it being lesser than 312 or bigger than 712. Read the joystick and place its value in a variable. Can you do that?
The other thing you want is to run the stepper motor at different speeds. How do you do that? Write code, where you do
int speed = 50;
Then you set the speed of the stepper motor according to the variable speed. Change the value in your code to check if it affects the stepper motor. Can you do that?
If either task is beyond your skills, figure out how to do them. Don't think they are one task.

What is the full sketch? There was nothing further in or attached to your post. Without seeing your entire sketch I have no guess as to why it might be acting the way you say it is acting.

Are you running on an Arduino UNO, Nano, or MEGA? Some other models have analogRead() return values up to 4096.