Stepper Motors won't go in reverse

My code is almost complete.
I have one issue in that the motors will only go in one direction.
I've set them to -350 instead of 350 yet they go the same direction regardless.
Any help would be much appreciated!!

#include <LiquidCrystal.h>
#include <Stepper.h>

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
Stepper StepperElev(200, 22, 24, 26, 28);
Stepper StepperBase(200, 23, 25, 27, 29);
const int nextbutton =  41;
const int backbutton =  40;

int buttonstate = 0;
int buttonstate2 = 0;
int lastbuttonstate = 0;
int lastbuttonstate2 = 0;
int satpos = 0;
int satdirection = 0;
int hasbeendone = 1;

void setup()
{
  lcd.begin(16, 2);
  lcd.clear();
  pinMode   (nextbutton, INPUT);
  pinMode   (backbutton, INPUT);
  StepperElev.setSpeed(60);
  StepperBase.setSpeed(60);
}

void loop()
{

  buttonstate = digitalRead(nextbutton);

  if (buttonstate != lastbuttonstate) {
    if (buttonstate == HIGH) {
      satpos++;
      satdirection = 0;
      hasbeendone = 0;
    }
    delay(50);
  }

  lastbuttonstate = buttonstate;

  buttonstate2 = digitalRead(backbutton);
  
  if (buttonstate2 != lastbuttonstate2) {
    if (buttonstate2 == HIGH) {
      satpos--;
      satdirection = 1;
      hasbeendone = 0;
    }
    delay(50);
  }

  lastbuttonstate2 = buttonstate2;

  

  if (satpos == 0) {
  lcd.clear();
  lcd.print ("SAT1: Astra 28:E");
  lcd.setCursor(0, 1);
  lcd.print ("Back        Next");
  delay(100);
  if (hasbeendone == 0) {
    if (satdirection == 0){
      StepperBase.step(350);
      hasbeendone = 1;
    }
    if (satdirection == 1) {
      StepperBase.step(-350);
      hasbeendone = 1;
    }
    } 
  }

  if (satpos == 1) {
  lcd.clear();
  lcd.print ("SAT2: Astra 28:F");
  lcd.setCursor(0, 1);
  lcd.print ("Back        Next");
  if (hasbeendone == 0) {
    if (satdirection == 0){
      StepperElev.step(350);
      hasbeendone = 1;
    }
    if (satdirection == 1) {
      StepperElev.step(-350);
      hasbeendone = 1;
    }
    } 
  }
  
  if (satpos == 2) {
  lcd.clear();
  lcd.print ("SAT3: Astra 28:G");
  lcd.setCursor(0, 1);
  lcd.print ("Back        Next");
  delay(100);
  if (hasbeendone == 0) {
    if (satdirection == 0){
      StepperBase.step(-350);
      hasbeendone = 1;
    }
    if (satdirection == 1) {
      StepperBase.step(350);
      hasbeendone = 1;
    }
    } 
  }
  
  if (satpos == 3) {
  lcd.clear();
  lcd.print ("SAT4: Astra  1:N");
  lcd.setCursor(0, 1);
  lcd.print ("Back        Next");
  delay(100);
  if (hasbeendone == 0) {
    if (satdirection == 0){
      StepperElev.step(-350);
      hasbeendone = 1;
    }
    if (satdirection == 1) {
      StepperElev.step(350);
      hasbeendone = 1;
    }
    } 
  }
  
  if (satpos == -1){
    satpos = 3;
  }
  
  if (satpos == 4){
    satpos = 0;
  }

  
    
}

I suspect a hardware problem.

What are you using as stepper drivers? What kind of steppers are you using?

28BYJ-48 Steppers and ULN2003 for the driver boards.

This is infuriating, on the reference code it just says to put a - in front of the steps and it should go backwards, instead it goes forward like there's no - there.

Figured it out, I had to switch Int2 and Int3 around on the driver board.

4 Likes

28BYJ-48s cannot run at 60 RPM shaft speed, try steps per rev = 32 and rpm = 640, that should give about 10 RPM shaft speed, remember, the 28BYJ-48 has a 32 step motor and 64 to 1 gear reduction.

1 Like

Another frustrating issues that can take days to resolve.
Here are a few issues I have been facing and the solutions I cam up with. I hope this helps others in long frustrations.

Stepper motor 28BYJ-48 12V with external power
ULN2003A drive

Challenge 1
ULN led lights blink, motor does not move.
Solution: reduce speed from 1 to 130 max.
if(speed > 130) speed = 130; //for safety and test proper max speed with your motor

Challenge 2
Motor acts funny and does not move to right position regardless of the number of steps
Solution: loop around single step. It is slower but guarantee to work.
for(i = 0; i <= numOfSteps; i++) StepMotor1.step(1);

Challenge 3
Motor only goes clockwise no matter what.
Solution discovered in another forum post, interchange pin 2 with pin 3
Stepper StepMotor1(steps, Pin1, Pin3, Pin2, Pin4); //yet on board corresponding wires are sequential.

On your state "challenge 3". Im having the same issue but I'm using the CNC Shield, because of this I don't know what be options are for switching pins and inputs for pins 2 and 3.