Pages: [1]   Go Down
Author Topic: 2nd stepper motor not reversing direction  (Read 819 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm setting up 2 stepper motors to trigger on switches, going one direction on one button press, and the opposite direction on the other button's press.  Right now everything in my code works as I want it: locking out of being able to go the opposite direction until the other button is pressed.  However, the 2nd motor as of now is not working correctly, on either button press the 2nd motor will continue in the same direction, instead of turning the opposite direction, like the 1st motor properly functions.  I don't understand how my code could be wrong, it's basically a mirror of the first motor.

#include <Stepper.h>

#define motorSteps 48     // change this depending on the number of steps
                           // per revolution of your motor
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define motorPin5 4
#define motorPin6 5
#define motorPin7 6
#define motorPin8 7



// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2,motorPin3,motorPin4);
Stepper myStepper2(motorSteps, motorPin8, motorPin5, motorPin6, motorPin7);
const int buttonPin2 = 2;
const int buttonPin3 = 3;
int buttonState1 = 0;
int buttonState2 = 0;
int Start = 0;
int End = 0;

void setup()
  {
  // set the motor speed at 60 RPMS:
  myStepper.setSpeed(60);
  myStepper2.setSpeed(60);
 
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
 
  // Initialize the Serial port:
  Serial.begin(9600);
  }

void loop()

{
  buttonState1 = digitalRead(buttonPin2);
  buttonState2 = digitalRead(buttonPin3);

 
 
  if (buttonState1 == HIGH)
  while (Start == 0)
  { 
   // Step forward 100 steps:
  Serial.println("Forward");
  myStepper.step(100);
  myStepper2.step(100);
  Start++; 
  //int End;
  End = 0;
  }
   
  if (buttonState2 == HIGH)
  while (End == 0)
 {
  // Step backward 100 steps:
  Serial.println("Backward");
  myStepper.step(-100);
  myStepper2.step(-100);
  End++;
  //int Start;
  Start = 0;
 }

 


}

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8856
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you using the pin order " motorPin8, motorPin5, motorPin6, motorPin7" instead of "motorPin5, motorPin6, motorPin7, motorPin8"?

How are the motors connected?
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just typo'd when I was putting them in, didn't think that order had any affect on the outcome.  The motors are connected through a 2803? darlington chip, one of them on pins 4,5,6,7, and the other on pins 8,9,10,11.  I don't have the circuit in front of me right now, it's sitting at our school with the rest of our project at the moment.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Changing the order of the numbers did matter apparently, when I did it, it completely fixed my problem.  Thank you very much for the help!
Logged

Pages: [1]   Go Up
Jump to: