Controlling a stepper motor with a joystick

Hi Arduino community,

I wrote this sketch the move a stepper with a laser on it. I used 32 if statements to get this working.

Is there a way to do this on a less time consuming and a more elegant way the next time?

Kind regards,

MB

#include <Stepper.h>
#define STEPS 2038 // the number of steps in one revolution of your motor (28BYJ-48)
Stepper stepper(STEPS, 8, 10, 9, 11);  


#define JOY_X A0
#define JOY_PUSH 3

int X_VALUE;
bool PUSH=false;


void setup() {
  Serial.begin(9600);


pinMode(JOY_PUSH,INPUT_PULLUP);




}
void loop() {



//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Joystick PUSH BUTTON   

if(!digitalRead(JOY_PUSH) & !PUSH )
{
PUSH=true;
delay(200);
}


if(!digitalRead(JOY_PUSH) & PUSH )
{
PUSH=false;
delay(200);
}


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> JOYStick X axis clockwise 



 X_VALUE=analogRead(JOY_X);


//1
 if(X_VALUE >= 520 && X_VALUE < 551  & !PUSH )
 {
  stepper.setSpeed(1);
  stepper.step(1);
 }

//2
 if(X_VALUE >= 551 && X_VALUE < 582  & !PUSH)
 {
  stepper.setSpeed(2);
  stepper.step(1);
 }

//3
 if(X_VALUE >= 582 && X_VALUE < 613  & !PUSH)
 {
  stepper.setSpeed(3);
  stepper.step(1);
 }

//4
 if(X_VALUE >= 613 && X_VALUE < 644  & !PUSH)
 {
  stepper.setSpeed(4);
  stepper.step(1);
 }

//5
 if(X_VALUE >= 644 && X_VALUE < 675  & !PUSH)
 {
  stepper.setSpeed(5);
  stepper.step(1);
 }

//6
 if(X_VALUE >= 675 && X_VALUE < 706  & !PUSH)
 {
  stepper.setSpeed(6);
  stepper.step(1);
 }

//7
 if(X_VALUE >= 706 && X_VALUE < 737  & !PUSH)
 {
  stepper.setSpeed(7);
  stepper.step(1);
 }

//8
 if(X_VALUE >= 737 && X_VALUE < 768  & !PUSH)
 {
  stepper.setSpeed(8);
  stepper.step(1);
 }

//9
 if(X_VALUE >= 768 && X_VALUE < 799  & !PUSH)
 {
  stepper.setSpeed(9);
  stepper.step(1);
 }

//10
 if(X_VALUE >= 799 && X_VALUE < 830  & !PUSH)
 {
  stepper.setSpeed(10);
  stepper.step(1);
 }

//11
 if(X_VALUE >= 830 && X_VALUE < 861  & !PUSH)
 {
  stepper.setSpeed(11);
  stepper.step(1);
 }

//12
 if(X_VALUE >= 861 && X_VALUE < 892  & !PUSH)
 {
  stepper.setSpeed(12);
  stepper.step(1);
 }

//13
 if(X_VALUE >= 892 && X_VALUE < 923  & !PUSH)
 {
  stepper.setSpeed(13);
  stepper.step(1);
 }



//14
 if(X_VALUE >= 923 && X_VALUE < 954  & !PUSH)
 {
  stepper.setSpeed(14);
  stepper.step(1);
 }

//15
 if(X_VALUE >= 954 && X_VALUE < 985  & !PUSH)
 {
  stepper.setSpeed(15);
  stepper.step(1);
 }

//16
 if(X_VALUE >= 985 && X_VALUE < 1023 & !PUSH)
 {
  stepper.setSpeed(16);
  stepper.step(1);
 }

 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> JOYStick X axis counter-clockwise 

 //1
 if(X_VALUE <= 506 && X_VALUE > 475  & !PUSH)
 {
  stepper.setSpeed(1);
  stepper.step(-1);
 }

//2
 if(X_VALUE <= 475 && X_VALUE > 444 & !PUSH)
 {
  stepper.setSpeed(2);
  stepper.step(-1);
 }

//3
 if(X_VALUE <= 444 && X_VALUE > 413 & !PUSH)
 {
  stepper.setSpeed(3);
  stepper.step(-1);
 }

//4
 if(X_VALUE <= 413 && X_VALUE > 382 & !PUSH)
 {
  stepper.setSpeed(4);
  stepper.step(-1);
 }

//5
 if(X_VALUE <= 382 && X_VALUE > 351 & !PUSH)
 {
  stepper.setSpeed(5);
  stepper.step(-1);
 }

//6
 if(X_VALUE <= 351 && X_VALUE > 320 & !PUSH)
 {
  stepper.setSpeed(6);
  stepper.step(-1);
 }

//7
 if(X_VALUE <= 320 && X_VALUE > 289 & !PUSH)
 {
  stepper.setSpeed(7);
  stepper.step(-1);
 }

//8
 if(X_VALUE <= 289 && X_VALUE > 258 & !PUSH)
 {
  stepper.setSpeed(8);
  stepper.step(-1);
 }

//9
 if(X_VALUE <= 258 && X_VALUE > 227 & !PUSH)
 {
  stepper.setSpeed(9);
  stepper.step(-1);
 }

//10
 if(X_VALUE <= 227 && X_VALUE > 196 & !PUSH)
 {
  stepper.setSpeed(10);
  stepper.step(-1);
 }

//11
 if(X_VALUE <= 196 && X_VALUE > 165 & !PUSH)
 {
  stepper.setSpeed(11);
  stepper.step(-1);
 }

//12
 if(X_VALUE <= 165 && X_VALUE > 134 & !PUSH)
 {
  stepper.setSpeed(12);
  stepper.step(-1);
 }

//13
 if(X_VALUE <= 134 && X_VALUE > 103  & !PUSH)
 {
  stepper.setSpeed(13);
  stepper.step(-1);
 }



//14
 if(X_VALUE <= 103 && X_VALUE > 72  & !PUSH)
 {
  stepper.setSpeed(14);
  stepper.step(-1);
 }

//15
 if(X_VALUE <= 72 && X_VALUE > 41  & !PUSH)
 {
  stepper.setSpeed(15);
  stepper.step(-1);
 }

//16
 if(X_VALUE <= 41  & !PUSH  & !PUSH)
 {
  stepper.setSpeed(16);
  stepper.step(-1);
 }

 //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Joystick slowest movement when button is pushed

 if(X_VALUE >= 737 & PUSH )
 {
    stepper.setSpeed(1);
  stepper.step(1);
  delay(150);
   
 }

  if(X_VALUE <= 289 & PUSH )
 {
    stepper.setSpeed(1);
  stepper.step(-1);
  delay(150);
   
 }

 //Serial.println(X_VALUE);

}

a more elegant way would start by indenting the code.

and

getting rid of useless empty

lines..

this is unreadable.. (press ctrl-T in the IDE..)


To your question, you could either find a formula that maps the analogRead to the value you want or have the thresholds in an array and use a for loop to find where you are

To make it easy for people to help you please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

Also use the AutoFormat tool to indent your code for easier reading,

There is probably a simple mathematical formula that would calculate the stepper value from the joystick value. I think this works

speedVal = ((X_VALUE - 520) / 31) + 1;

If you have a series of related IF statements it makes for a lot less typing if you use ELSE IF - like this

   if (!PUSH) {
            //1
         if(X_VALUE >= 520 )
        {
            stepper.setSpeed(1);
            stepper.step(1);
         }

            //2
        else if(X_VALUE >= 551)
         {
          stepper.setSpeed(2);
          stepper.step(1);
         }
         
         // etc
    }

...R

Sorry for the messy post.

There is probably a simple mathematical formula that would calculate the stepper value from the joystick value. I think this works
Code: [Select]
speedVal = ((X_VALUE - 520) / 31) + 1;

This is what I was searching for. Thanks!

MB