Code to controlling 2 stepper motors with v2.4 joystick shield

Hi, i am a newbie in arduino and i am working on a project for my research camp. I want to control 2 stepper motors with a joystick shield. The thumbstick will make the motors move slowly and the buttons will make them move fast but the program doesnt work. I would be grateful for any help, thanks.


#include <AFMotor.h>
AF_Stepper motor(48, 1);
AF_Stepper motor1(48, 2);

const byte PIN_ANALOG_X = 0;
const byte PIN_ANALOG_Y = 1;

const int X_THRESHOLD_LOW = 300;
const int X_THRESHOLD_HIGH = 600;

const int Y_THRESHOLD_LOW = 300;
const int Y_THRESHOLD_HIGH = 600;

int x_position;
int y_position;

int x_direction;
int y_direction;

int val1;
int val2;
int val3;
int val4;
int val5;

const byte PIN_BUTTON_A = 2;    //A
const byte PIN_BUTTON_B = 3;    //B 
const byte PIN_BUTTON_C = 4;    //C
const byte PIN_BUTTON_D = 5;    //D
const byte PIN_BUTTON_E = 6;    //E

//const byte PIN_ANALOG_X = 0;
//const byte PIN_ANALOG_Y = 1;

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

pinMode(PIN_BUTTON_B, INPUT);
 //digitalWrite(PIN_BUTTON_B, HIGH);

 pinMode(PIN_BUTTON_E, INPUT);
 //digitalWrite(PIN_BUTTON_E, HIGH);

 pinMode(PIN_BUTTON_C, INPUT);
 //digitalWrite(PIN_BUTTON_C, HIGH);

 pinMode(PIN_BUTTON_D, INPUT);
 //digitalWrite(PIN_BUTTON_D, HIGH);

 pinMode(PIN_BUTTON_A, INPUT);
 //digitalWrite(PIN_BUTTON_A, HIGH);
}

void loop() {
 x_direction = 0;
 y_direction = 0;

 x_position = analogRead(PIN_ANALOG_X);
 y_position = analogRead(PIN_ANALOG_Y);

 val1 = digitalRead(PIN_BUTTON_A);
 val2 = digitalRead(PIN_BUTTON_B);
 val3 = digitalRead(PIN_BUTTON_C);
 val4 = digitalRead(PIN_BUTTON_D);
 val5 = digitalRead(PIN_BUTTON_E);
 
 while (val1 == HIGH) {          // A Button is pressed

   motor.setSpeed(25);
   motor.step(1, FORWARD, MICROSTEP);
} 
 while (val2 == HIGH) {          // C Button is pressed

   motor1.setSpeed(25);
   motor1.step(1, FORWARD, MICROSTEP);
}

 while (val3 == HIGH) {          // B Button is pressed

   motor.setSpeed(25);
   motor.step(1, BACKWARD, MICROSTEP);
} 

 while (val4 == HIGH) {          // D Button is pressed

   motor1.setSpeed(25);
   motor1.step(1, BACKWARD, MICROSTEP);
}

    if (x_position < X_THRESHOLD_LOW){
     Serial.print("x: ");
     Serial.print(x_position,DEC);
     Serial.print(",");
     motor1.setSpeed(10);
     motor1.step(1, FORWARD, MICROSTEP);
     
   } 

   else if (x_position > X_THRESHOLD_HIGH) {
     Serial.print("x: ");
     Serial.print(x_position,DEC);
     Serial.println(",");
     motor1.setSpeed(10);
     motor1.step(1,BACKWARD, MICROSTEP);
    }

    if (y_position < Y_THRESHOLD_LOW){
     Serial.print("y: ");
     Serial.print(y_position,DEC);
     Serial.print(",");
     motor.setSpeed(10);
     motor.step(1, FORWARD, MICROSTEP);
     
   } 

   else if (y_position > Y_THRESHOLD_HIGH) {
     Serial.print("y: ");
     Serial.print(x_position,DEC);
     Serial.println(",");
     motor.setSpeed(10);
     motor.step(1,BACKWARD, MICROSTEP);
   }
  }

Your while condition will never break if you don't update the reading status. You can write a function which will read and return the status of the button.

Or you can use if statement instead of while.

That's because when you press one button, it's read as HIGH then the while loop will never break, because you are not updating the reading. it will just work as while(true) { //Infinite loop }.

You can solve it by this I think

bool buttonStatus(byte pin)
{
     return digitalRead(pin);
}

....

while(buttonStatus(PIN_BUTTON_A) == true){
   // do this

}

or simply calling the digitalRead function,

while(digitalRead(PIN_BUTTON_A) == HIGH){

// do this

}

Hope that helps. :slight_smile:

I think you need to replace all the WHILEs

while (val3 == HIGH) {

with IF

if (val3 == HIGH) {

Please edit your Post and use the code button </> to display your code properly so it is easy to select and copy to a text editor.

Help us to help you.

…R

Thanks for your help but the motors still dont work. I couldnt understand what the problem is because arduino itself doesnt show an error.

bzrobotics:
Thanks for your help but the motors still dont work.

I presume that means you have changed your code. We can't see it unless you post the new version in your next Reply.

...R