Go Down

Topic: Trouble running two NEMA 17s with a joystick  (Read 140 times) previous topic - next topic

IV_Explorations

So I'm trying to make a motorized dobsonian telescope using two NEMA 17 motors with Easy Driver boards. I want to control the x and y axis with a joystick. I found a code that ran one with a joystick and I tried to modify it by copying the loop and adding the y-axis. Well only the X-axis moves and it only turns CCW.

Can someone run over this and see if they see the issue that I can't? Thank you

Code: [Select]
/*This program is designed to control two nema 17 boards with easy driver board with a joystick on a Nano.
My specific use is to control the X and Y axis of a homemade telescope

considered open source, feel free to do whatever with it.
*/





#define step_pinX 3                                                             // Pin 3 connected to Steps pin on EasyDriver       X-AXIS
#define dir_pinX 2                                                              // Pin 2 connected to Direction pin                 X-AXIS
#define MS1X 5                                                                  // Pin 5 connected to MS1 pin                       X-AXIS
#define MS2X 4                                                                  // Pin 4 connected to MS2 pin                       X-AXIS
#define SLEEPX 6                                                                // Pin 6 connected to SLEEP pin                     X-AXIS
#define step_pinY 7                                                             // Pin 7 connected to Steps pin on EasyDriver         Y-AXIS
#define dir_pinY 8                                                              // Pin 8 connected to Direction pin                   Y-AXIS
#define MS1Y 9                                                                  // Pin 9 connected to MS1 pin                         Y-AXIS
#define MS2Y 10                                                                 // Pin 10 connected to MS2 pin                        Y-AXIS
#define SLEEPY 11                                                               // Pin 11 connected to SLEEP pin                      Y-AXIS
#define XPin A0                                                                // Pin A0 connected to joystick x axis              x-AXIS
#define YPin A1                                                                // Pin A1 connected to joystick y axis                Y-AXIS
#define JoySwitch 12                                                            // Pin 12 connected to joystick button 
#define Limit01 22                                                              // Pin 22 connected to limit switch
#define Limit02 23                                                              // Pin 23 connected to limit switch

int stepSpeed = 10;                                                            // speed of stepper motor


void setup() {
   pinMode(MS1X, OUTPUT);
   pinMode(MS2X, OUTPUT);
   pinMode(dir_pinX, OUTPUT);
   pinMode(step_pinX, OUTPUT);
   pinMode(SLEEPX, OUTPUT);
   pinMode(MS1Y, OUTPUT);
   pinMode(MS2Y, OUTPUT);
   pinMode(dir_pinY, OUTPUT);
   pinMode(step_pinY, OUTPUT);
   pinMode(SLEEPY, OUTPUT);

   pinMode(Limit01, INPUT);
   pinMode(Limit02, INPUT);

   pinMode(JoySwitch,INPUT_PULLUP);

   digitalWrite(SLEEPX, HIGH);                                                  // Wake up EasyDriver
   delay(5);                                                                    // Wait for EasyDriver wake up
   digitalWrite(SLEEPY, HIGH);                                                  // Wake up EasyDriver
   delay(5);                                                                    // Wait for EasyDriver wake up


/* Configure type of Steps on EasyDriver:
// MS1X MS2X MS1Y MS2Y
//
// LOW LOW = Full Step //
// HIGH LOW = Half Step //
// LOW HIGH = A quarter of Step //
// HIGH HIGH = An eighth of Step //
*/

   digitalWrite(MS1X, LOW);                                                      // Configures to Full Steps
   digitalWrite(MS2X, LOW);                                                      // Configures to Full Steps
   digitalWrite(MS1Y, LOW);                                                      // Configures to Full Steps
   digitalWrite(MS2Y, LOW);                                                      // Configures to Full Steps

}

void loop() {
  if (!digitalRead(JoySwitch)) {                                                 // Check joystick switch position
  delay(500);                                                                    // delay for debouncing
  switch (stepSpeed) {                                                          // Check current value of step_speed and change it
    case 1:
    stepSpeed=10;                                                               // slow speed
    break;
    case 3:
    stepSpeed=1;                                                                // fast speed
    break;
    case 10:
    stepSpeed=3;                                                                // medium speed
    break;
  }
  }

  if (analogRead(XPin)> 712){                                                     //if joystick is moved left
    if (!digitalRead(Limit01))  {}                                               // check is limit switch is activated

    else {                                                                       // if limit switch is not activated, move motor clockwise
      digitalWrite(dir_pinX, LOW);                                               // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pinX, HIGH);
      delay(stepSpeed);
      digitalWrite(step_pinX, LOW);
      delay(stepSpeed);
    }
  }

if (analogRead(XPin) < 312){                                                     //if joystick is moved left
    if (!digitalRead(Limit02))  {}                                               // check is limit switch is activated

    else {                                                                       // if limit switch is not activated, move motor clockwise
      digitalWrite(dir_pinX, HIGH);                                               // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pinX, HIGH);
      delay(stepSpeed);
      digitalWrite(step_pinX, LOW);
      delay(stepSpeed);

    }
    }   
    if (analogRead(YPin)> 712){                                                     //if joystick is moved left
    if (!digitalRead(Limit01))  {}                                               // check is limit switch is activated

    else {                                                                       // if limit switch is not activated, move motor clockwise
      digitalWrite(dir_pinY, LOW);                                               // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pinY, HIGH);
      delay(stepSpeed);
      digitalWrite(step_pinY, LOW);
      delay(stepSpeed);
    }
  }

if (analogRead(YPin) < 312){                                                     //if joystick is moved left
    if (!digitalRead(Limit02))  {}                                               // check is limit switch is activated

    else {                                                                       // if limit switch is not activated, move motor clockwise
      digitalWrite(dir_pinY, HIGH);                                               // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pinY, HIGH);
      delay(stepSpeed);
      digitalWrite(step_pinY, LOW);
      delay(stepSpeed);

    }
    }         

PaulS

Quote
Trouble running two NEMA 17s with a joystick
The mounting hole specification is a useless bit of information.

Code: [Select]
   pinMode(Limit01, INPUT);
   pinMode(Limit02, INPUT);

Are these switches wired with external pulldown resistors?

You appear to have two limit switches, but can move in 4 directions (to the left, to the right, up, and down).

If the limit switches limit the left/right motion, checking them when you want to move up or down is pointless.

The art of getting good answers lies in asking good questions.

Robin2

Can someone run over this and see if they see the issue that I can't? Thank you
To be brutally honest the whole thing is a bit of a dog's breakfast. For example there is no need to repeat all the code for the two directions when the only thing that is different is the state of  dir_pinX
Code: [Select]
  if (analogRead(XPin)> 712){
    if (!digitalRead(Limit01))  {}

    else {
      digitalWrite(dir_pinX, LOW);
      digitalWrite(step_pinX, HIGH);
      delay(stepSpeed);
      digitalWrite(step_pinX, LOW);
      delay(stepSpeed);
    }
  }

  if (analogRead(XPin) < 312){
    if (!digitalRead(Limit02))  {}

    else {
      digitalWrite(dir_pinX, HIGH);
      digitalWrite(step_pinX, HIGH);
      delay(stepSpeed);
      digitalWrite(step_pinX, LOW);
      delay(stepSpeed);

    }
 }


It would be much better to read all the inputs at the start of loop() and save their values to variables. That way you can easily print the variables if you need to check that they have the values you think they have.

All those delay() are going to make for a very unresponsive program. The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.


Also have a look at the second (non blocking) example in this Simple Stepper Code


...R
Stepper Motor Basics
Planning and Implementing a Program

Two or three hours spent thinking and reading documentation solves most programming problems.

IV_Explorations

The mounting hole specification is a useless bit of information.

Code: [Select]
   pinMode(Limit01, INPUT);
   pinMode(Limit02, INPUT);

Are these switches wired with external pulldown resistors?

You appear to have two limit switches, but can move in 4 directions (to the left, to the right, up, and down).

If the limit switches limit the left/right motion, checking them when you want to move up or down is pointless.


Honestly I'm not using them, they were just in the code I began with and left them there in case I saw I needed to add some later.

PaulS

Quote
Honestly I'm not using them
What a load of crap. You ARE reading the pins!
The art of getting good answers lies in asking good questions.

IV_Explorations

What a load of crap. You ARE reading the pins!
It's an open circuit so what's the matter?

PaulS

It's an open circuit so what's the matter?
You care about the result of reading a pin with nothing attached to it. Why?

Google Arduino + floating pin to see why it is not a good idea to read pins with nothing connected to them and expect to get anything useful as a result.
The art of getting good answers lies in asking good questions.

Robin2

You care about the result of reading a pin with nothing attached to it. Why?
I'm not sure this is helpful to the OP.

Yes it would be more sensible to remove unused code from the program, but leaving it there and ignoring the results is unlikely to cause any problem.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

I'm not sure this is helpful to the OP.

Yes it would be more sensible to remove unused code from the program, but leaving it there and ignoring the results is unlikely to cause any problem.

...R
Code: [Select]
    if (analogRead(YPin)> 712){                                                     //if joystick is moved left
    if (!digitalRead(Limit01))  {}                                               // check is limit switch is activated

    else {                                                                       // if limit switch is not activated, move motor clockwise
      digitalWrite(dir_pinY, LOW);                                               // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pinY, HIGH);
      delay(stepSpeed);

The problem is that OP is not ignoring, in either direction, the non-existent limit switches.
The art of getting good answers lies in asking good questions.

Robin2

The problem is that OP is not ignoring, in either direction, the non-existent limit switches.
Sorry - I missed that. I'm blind to {} on the same line :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up