Stepper won't rotate counter clock wise

My program will turn a servo motor when a button pressed or a code is enter on a keypad.

The program actually runs just fine, except the servo motor only turn one way.

I have the pins of the servo connected 1, 3, 2, 4. I was told that this arrangement will fix the issue
and it did in the past. However, now it is not working. Only works turn counter clockwise.

Any ideas why???

Thanks for the help in advance!!! :slight_smile:

Here is the code:

#include <Keypad.h>
#include <Stepper.h>

const int stepsPerRev = 8;         // Number of steps in 1 revolution.

const byte ROWS = 4;               // set display to 4 rows.
const byte COLS = 3;               // set display to 3 columns.

char keys[ROWS][COLS] =            // defining the Keypad.
{ {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

byte rowPins[ROWS] = {2, 3, 4, 5};  // pins for Keypad.
byte colPins[COLS] = {6, 7, 8};

Keypad keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Stepper myStepper(stepsPerRev, A4, A3, A2, A1);

const int Lock = 13;            // pushbutton 1, to lock.
const int Unlock = 12;          // pushbutton 2, to unlock.
const int greenPin = 10;        // Green LED pin.
const int redPin = 11;          // Red LED pin.

char* pass = "1234";            // Passcode
int pos = 0;                    // Keypad position.    --> Position where the last key pressed is stored in the array.


void gFlash();        // Declaring green LED blinking function.
void rFlash();        // Declaring red LED blinking function.


void setup() {                        // Declaring the pins.
  pinMode(Lock, INPUT);
  pinMode(Unlock, INPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(redPin, OUTPUT);

}


int x = 0;        // Used to see if Lock button has already been pushed.
                  // Pos, used to clear the array for the passcode.
void loop() {

  char key = keypad.getKey();

  myStepper.setSpeed(750);

  if (key == '*' && x == 0) {           // To lock.
    x = 1;
    myStepper.step(130);
    rFlash(90);
  }

  if (digitalRead(Lock) == HIGH) {       // To Lock.
    pos = 0;
    x = 0;
    myStepper.step(130);
    rFlash(90);
  }

  if (digitalRead(Unlock) == HIGH) {      //  To Unlock.
    pos = 0;
    x = 0;
    myStepper.step(-130);
    gFlash(90);
  }

  if (key == pass[pos]) {               // If the key pressed is equal to the passcode,
    pos ++;                             // then the keywill be stored in the array.
  }

  if (pos == 4) {                       // Passcode is correct, and will unlock.
    pos = 0;
    x = 0;
    myStepper.step(-130);
    gFlash(90);
  }

}





// Functions

void gFlash(int gTime) {
  digitalWrite(redPin, LOW);
  for (int x = 0; x < 5; x++) {
    digitalWrite(greenPin, HIGH);
    delay(gTime);
    digitalWrite(greenPin, LOW);
    delay(gTime);
  }
}

void rFlash(int rTime) {
  digitalWrite(greenPin, LOW);
  for (int x = 0; x < 5; x++) {
    digitalWrite(redPin, HIGH);
    delay(rTime);
    digitalWrite(redPin, LOW);
    delay(rTime);
  }

}

My program will turn a servo motor when a button pressed

No it is a stepping motor not a servo the two are very different.

Try swapping one set of coils, either physically or in software like this:-

Stepper myStepper(stepsPerRev, A3, A4, A2, A1);

i tried switching the code to what you said but that did not work.

OK you have to be more specific here. Just saying "did not work" does not cut it in electronics. You have to say what happened.
Did it:-
Still rotate?
not move at all?

Just swap the order of those pins and you will hit on a combination that rotates the stepping motor in the direction you want.

myStepper.setSpeed(750);

Really? Try 100 and work your way up, sounds like you're going directly from 0 to high speed and the stepper can't accelerate that fast, it's losing sync.

WhiteT:
I need the program to rotate CW when one button is pressed and rotate CCW when another button is pressed.

So kind of you to put that in your initial request.

https://www.arduino.cc/en/Tutorial/StepperOneRevolution

Break the code down, do not try and do everything at once. Write and TEST little bits of it and only when you are sure it is all working try and combine them.
So:-
Do the keys read correctly? Write code and test.
Will the motor turn in both directions? Write code and test.
Now just turn the motor one way on a key press. Write code and test.
Now turn the other way on a key press? Write code and test.

Use serial print to see if parts of your code are being executed.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a picture of your project so we can see your component layout.

Do you know which wires from your stepper are coil pairs?

What are your steppers? link to spec/data...

Tom... :slight_smile:

So i was playing around with the pins on the stepper motor (28byj-48 Stepper Motor) and i disconnected pin1. Now the stepper rotates both CW and CCW when the certain button is pressed, like i wanted it to.

Any ideas why this fixed it??

No, but it suggests it was definitely in the wrong sequence.

WhiteT:
My program will turn a servo motor when a button pressed or a code is enter on a keypad.
The program actually runs just fine, except the servo motor only turn one way.

I agree. A servo motor is not a stepper motor.

Hi,
Please answer post #5.

Thanks.. Tom... :slight_smile: