Trying to make my stepper motors turn in both directions

Im using a very simple code to run my stepper motors using an arduino uno and a cnc shield. The code runs and turns the motors but I need them to turn forward and backwards. Im sure im missing something simple please help. If you look on line 46 to 49 I reversed the HIGH, LOW function think this would reverse the polarity but apparently I am wrong.

const int StepX = 2;
const int DirX = 5;
const int StepY = 3;
const int DirY = 6;
//const int StepZ = 4;
//const int DirZ = 7;


void setup() {
  pinMode(StepX,OUTPUT);
  pinMode(DirX,OUTPUT);
  pinMode(StepY,OUTPUT);
  pinMode(DirY,OUTPUT);
//  pinMode(StepZ,OUTPUT);
//  pinMode( DirZ,OUTPUT);

}

void loop() {
 digitalWrite(DirX, HIGH); // set direction, HIGH for clockwise, LOW for anticlockwise
 digitalWrite(DirY, HIGH);
// digitalWrite(DirZ, HIGH);
 
 for(int x = 0; x<200; x++) { // loop for 200 steps      1  
  digitalWrite(StepX,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepX,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<20; x++) { // loop for 20 steps
  digitalWrite(StepY,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepY,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<20; x++) { // loop for 20 steps
  digitalWrite(StepY,LOW);
  delayMicroseconds(500);
  digitalWrite(StepY,HIGH); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<200; x++) { // loop for 200 steps        2
  digitalWrite(StepX,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepX,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<20; x++) { // loop for 20 steps
  digitalWrite(StepY,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepY,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<20; x++) { // loop for 20 steps
  digitalWrite(StepY,LOW);
  delayMicroseconds(500);
  digitalWrite(StepY,HIGH); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<200; x++) { // loop for 200 steps        3
  digitalWrite(StepX,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepX,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<20; x++) { // loop for 20 steps
  digitalWrite(StepY,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepY,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<20; x++) { // loop for 20 steps
  digitalWrite(StepY,LOW);
  delayMicroseconds(500);
  digitalWrite(StepY,HIGH); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<500; x++) { // loop for 500 steps        LONG CUT     HOLDER
  digitalWrite(StepX,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepX,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<20; x++) { // loop for 20 steps
  digitalWrite(StepY,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepY,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<250; x++) { // loop for 250 steps        LONG CUT     HOLDER
  digitalWrite(StepX,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepX,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<20; x++) { // loop for 20 steps  COMING OFF HOLDER CUT
  digitalWrite(StepY,LOW);
  delayMicroseconds(500);
  digitalWrite(StepY,HIGH); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second


for(int x = 0; x<166; x++) { // loop for 250 steps       GOING BACK FOR SECOND HOLDER CUT
  digitalWrite(StepX,LOW);
  delayMicroseconds(500);
  digitalWrite(StepX,HIGH); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<40; x++) { // loop for 20 steps   SETTING UP FOR LAST 2ND CUT FOR HOLDER
  digitalWrite(StepY,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepY,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second



for(int x = 0; x<80; x++) { // loop for 80 steps       LAST 2ND CUT FOR HOLDER    HOLDER
  digitalWrite(StepX,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepX,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

for(int x = 0; x<50; x++) { // loop for 50 steps  COMING OFF OF LAST CUT   FINISHING
  digitalWrite(StepY,LOW);
  delayMicroseconds(500);
  digitalWrite(StepY,HIGH); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

}

Then, perhaps, you should put that into your code, somewhere!

yes that is the question???

Yep. Look at the following lines of code and you can probably guess what you need to do:

 digitalWrite(DirX, HIGH); // set direction, HIGH for clockwise, LOW for anticlockwise
 digitalWrite(DirY, HIGH);

Oh my god yes I new it was something simple I was missing . thank you

 digitalWrite(DirX, HIGH); // set direction, HIGH for clockwise, LOW for anticlockwise
 digitalWrite(DirY, HIGH);
 digitalWrite(DirX, LOW); 
 digitalWrite(DirY, LOW);

why does this not work

sorry Im new to this Im just not seeing it

Did you STOP the motor before changing directions?

No All I did was add that to the code. that I posted before. Ho w do I do that

Just before each one of your for loops that do the stepping, set the direction you want the motor to go using the dirx, diry pins you're now aware of.

ok thanks ill try

do you mind showing me in the code

I don't know what your code's supposed to do, but let's suppose that these two loops are supposed to step the Y stepper in different directions:

 digitalWrite(DirY, HIGH);
for(int x = 0; x<20; x++) { // loop for 20 steps
  digitalWrite(StepY,HIGH);
  delayMicroseconds(500);
  digitalWrite(StepY,LOW); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

 digitalWrite(DirY,LOW);
for(int x = 0; x<20; x++) { // loop for 20 steps
  digitalWrite(StepY,LOW);
  delayMicroseconds(500);
  digitalWrite(StepY,HIGH); 
  delayMicroseconds(500);
 }
delay(1000); // delay for 1 second

I appreciate it. That is what I did it doesnt change the direction

Then you must have a wiring issue. The dir pin is for changing direction. Post your wiring diagram:

i figured out thank you

Yes, how? :thinking:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.