1

I'm trying to control the direction of three 28byj-48 stepper motors using the ULN2003 drivers. I am using 6 push buttons, 2 for each motor. what I want is for each motor to be stationary by default and rotate forward when I press on one button and reverse when I press the other.

You can find my code below. When I use the same approach for coding for one motor, it works fine with a suitable speed. But once I add all 3 motors, motor A and C perform fine but with low speed, motor B keeps rotating forward from the start, stops while I'm pushing its forward button and nothing happens when I push its reverse button. I have checked my circuit several times and there's nothing wrong with it.

#include <Stepper.h> //including stepper motor library

//defining pins section

// pins for motor A:
int stepAIN1Pin = 5;         
int stepAIN2Pin = 4;
int stepAIN3Pin = 3;
int stepAIN4Pin = 2;

// pins for motor B:
int stepBIN1Pin = 9;         
int stepBIN2Pin = 8;
int stepBIN3Pin = 7;
int stepBIN4Pin = 6;

// pins for motor C:
int stepCIN1Pin = 13;         
int stepCIN2Pin = 12;
int stepCIN3Pin = 11;
int stepCIN4Pin = 10;

int stepsPerRevolution = 2048; // amount of steps per revolution for the 28BYJ48 motor

int buttonA1Pin = A5;  // pushbutton 1 pin for clockwise rotation of motor A
int buttonA2Pin = A4;  // pushbutton 2 pin for counter clockwise rotation of motor A

int buttonB1Pin = A3;  // pushbutton 1 pin for clockwise rotation of motor B
int buttonB2Pin = A2;  // pushbutton 2 pin for counter clockwise rotation of motor B

int buttonC1Pin = A1;  // pushbutton 1 pin for clockwise rotation of motor C
int buttonC2Pin = A0;  // pushbutton 2 pin for counter clockwise rotation of motor C

Stepper myStepperA(stepsPerRevolution, stepAIN1Pin, stepAIN3Pin, stepAIN2Pin, stepAIN4Pin); // stepping sequence for motor A

Stepper myStepperB(stepsPerRevolution, stepBIN1Pin, stepBIN3Pin, stepBIN2Pin, stepBIN4Pin); // stepping sequence for motor B

Stepper myStepperC(stepsPerRevolution, stepCIN1Pin, stepCIN3Pin, stepCIN2Pin, stepCIN4Pin); // stepping sequence for motor C




void setup() 
{
  // Setting up the pushbutton pins to be an input:
  pinMode(buttonA1Pin, INPUT);
  pinMode(buttonA2Pin, INPUT);

  pinMode(buttonB1Pin, INPUT);
  pinMode(buttonB2Pin, INPUT);

  pinMode(buttonC1Pin, INPUT);
  pinMode(buttonC2Pin, INPUT);

myStepperA.setSpeed(15); // Setting the speed for motor A

myStepperB.setSpeed(15); // Setting the speed for motor B

myStepperC.setSpeed(15); // Setting the speed for motor C
}





void loop()
{

int buttonA1State, buttonA2State, buttonB1State, buttonB2State, buttonC1State, buttonC2State;

  buttonA1State = analogRead(buttonA1Pin);
  buttonA2State = analogRead(buttonA2Pin);

 if ((buttonA1State != 0) && !(buttonA2State != 0))  // if we're pushing button 1 and NOT button 2
  myStepperA.step(stepsPerRevolution/8);

 if (((buttonA2State != 0) && !(buttonA1State != 0)))  // if we're pushing button 1 and NOT button 2
  myStepperA.step(-stepsPerRevolution/8);


  buttonB1State = analogRead(buttonB1Pin);
  buttonB2State = analogRead(buttonB2Pin);

 if (((buttonB1State != 0) && !(buttonB2State != 0)))  // if we're pushing button 1 and NOT button 2
  myStepperB.step(stepsPerRevolution/8);

 if (((buttonB2State != 0) && !(buttonB1State != 0)))  // if we're pushing button 1 and NOT button 2
  myStepperB.step(-stepsPerRevolution/8);


  buttonC1State = analogRead(buttonC1Pin);
  buttonC2State = analogRead(buttonC2Pin);

 if (((buttonC1State != 0) && !(buttonC2State != 0)))  // if we're pushing button 1 and NOT button 2
  myStepperC.step(stepsPerRevolution/8);

 if (((buttonC2State != 0) && !(buttonC1State != 0)))  // if we're pushing button 1 and NOT button 2
  myStepperC.step(-stepsPerRevolution/8);


  }
  • 1
    Please don't put phrases like "Urgent" into the title. You won't get faster results with this. – chrisl Nov 13 '18 at 14:54
  • @chrisl sorry I'm new here! fixed – Saba Hilali Nov 13 '18 at 15:06
  • As I see in the source code of the stepper library, the step() method is blocking, until the given steps are done. With this it would be difficult to drive more than one stepper at a time. I think the AccelStepper library uses a non-blocking step function. – chrisl Nov 13 '18 at 15:15
  • Also: Why are you doing an analogRead(), but don't use it as an analog value. Depending on how you connected the pins, the returned values may be zero only a few times when they should be zero, due to electric noise. Why don't you use digitalRead()? – chrisl Nov 13 '18 at 15:18
  • @chrisl thanks a lot! it worked properly by changing to digitalRead() – Saba Hilali Nov 13 '18 at 17:11
3

Try using digitalRead() for button presses. Chances are your buttons, especially when on a breadboard, don't return exactly 0 for analogRead().

IMHO, simple On-Off switches should always connect your pin to either Vcc or GND, with either the internal pullup turned on (if connected to GND), or an external pulldown resistor in place.

To debug this, you might want to Serial.println() the values of your button states to your Arduino IDE and observe if their values are like you'd expect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.