# Motor running forward and reverse at different speeds

Hi All

Please bear with me, as in spite of my advanced years, I am trying to learn coding my Arduino Uno.
Whilst I am learning I am trying to construct a sketch to opearate a stepper motor with two buttons. One button to run the motor in one direction at a certain speed and to keep running whilst the button is depressed.
When the other button is pressed the motor needs to run in the reverse direction but at a different set speed, also to run whilst the button is pressed.

I have been studying the sketches of Brian Schmalz for the EasyDriver, and have found them very useful and instructive, so I have tried to modify one of those sketches to do what I want.

I list the sketch below and would appreciate some guidance and advice. I do realise that the more experienced of you could well get exasperated by such questions, but much of this stuff related to steppers isn't to be found in any of the books I have acquired so far.

Thanks in anticipation:

#define DISTANCE 3200

int StepCounter = 0;
int Stepping = false;

void setup() {
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);

pinMode(2, INPUT);
pinMode(3, INPUT);
}

void loop() {
if (digitalRead(3) == LOW && Stepping == false)
{
digitalWrite(8, LOW);
Stepping = true;
}

if (Stepping == true)
{
digitalWrite(9, HIGH);
delay(1);
digitalWrite(9, LOW);
delay(1);

StepCounter = StepCounter + 1;

if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
if (digitalRead(2) == LOW && Stepping == false)
{
digitalWrite(8, HIGH);
Stepping = true;

if (Stepping == true)
{
digitalWrite(9, HIGH);
delay(10);
digitalWrite(9, LOW);
delay(10);

StepCounter = StepCounter + 1;

if (StepCounter == DISTANCE)
{
StepCounter = 0;
Stepping = false;
}
}
}

Pins without names suck.

``````    if (StepCounter == DISTANCE)
``````

It does not appear logical to compare the number of steps taken to a distance. One of these names needs to change.

What kind of stepper driver are you using? The AccelStepper library could handle the stepping at differing speeds for you.

PaulS

I am using Easydriver with Nema17 motor.

The basis of the sketch as I said was as published by Brai Scmalz, the designer of the Easydriver module.

I will look into AccelStepper library, but would still appreciate any guidance.

but would still appreciate any guidance.

The sketch you posted does something. You did not explain what it actually does.

You want the sketch to do something. It is not clear how what the sketch actually does differs from what you want, so any guidance is not going to lead you where you want to go.

Give all your I/O pins names so we can understand what writing to different pins is intended to do.

There is a missing } in your code and I don't know where it should go.

The structure of the code that deals with

``````if (digitalRead(3) == LOW && Stepping == false)
``````

is completely different from the structure of the code that deals with

``````    if (digitalRead(2) == LOW && Stepping == false)
``````

If you use the AutoFormat tool to lay out your code consistently the missing } and the different structures would be more obvious.

When posting code please use the code button </>

``````so your code
looks like this
``````

and is easy to copy to a text editor See How to use the Forum

...R

My advice is to first get the buttons working and then add motor movement. You should have variables for both buttons and read them each time through loop(). If the "move forward" or "move in reverse" button is pressed write a message to the serial monitor. When this is working you can move on to motor control.

In code it would look something like this (with an added variable set to true when either button is pressed):

``````const byte FORWARD_BTN = 2;  // forwared button
const byte REV_BTN = 3;      // reverse button

boolean moveMotor = false;  // true => motor should move

void setup()
{
Serial.begin(9600);

// buttons
pinMode(FORWARD_BTN, INPUT);
pinMode(REV_BTN, INPUT);

}

void loop()
{
// foward button
{
// set flag
moveMotor = true;

Serial.println("Moving forward ...");

}

// set flag
moveMotor = true;

Serial.print("Moving in reverse ...");

} // else

// reset flag
moveMotor = false;

}
``````

Now consider what the difference is, in terms of the Easy Driver, between moving in one direction or the other. The direction pin is either HIGH or LOW. Each time through loop() if a button is pressed set the direction pin and move the motor. I think your speed requirement could be met by changing the number of steps moved in each direction.

I don't have an Easy Driver board but based on you posting and the guide at SparkFun I took a stab at filling in the motor control to the above, try it out if you like.

``````const byte FORWARD_BTN = 2;  // forwared button
const byte REV_BTN = 3;      // reverse button

// steps for one move
const int FWD_STEPS = 500;
const int REV_STEPS = 250;
int stepCount;

// easy driver pins
const byte MOTOR_DIR_PIN = 8;
const byte MOTOR_STEP_PIN = 9;
const byte MOTOR_ENABLE_PIN = 6;
byte motorDir;  // direction pin value

boolean moveMotor = false;  // true => motor should move

void setup()
{
Serial.begin(9600);

// easy driver
pinMode(MOTOR_ENABLE_PIN, OUTPUT);
pinMode(MOTOR_DIR_PIN, OUTPUT);
pinMode(MOTOR_STEP_PIN, OUTPUT);
digitalWrite(MOTOR_ENABLE_PIN, HIGH);

// buttons
pinMode(FORWARD_BTN, INPUT);
pinMode(REV_BTN, INPUT);

}

void loop()
{
// foward button
{
// set flag
moveMotor = true;

// set direction
motorDir = LOW;

// # of steps
stepCount = FWD_STEPS;

Serial.println("Moving forward ...");

}

// set flag
moveMotor = true;

// set direction
motorDir = HIGH;

// # of steps
stepCount = 250;

Serial.print("Moving in reverse ...");

} // else

// if we should move the motor
if( moveMotor )
{
digitalWrite(MOTOR_ENABLE_PIN, LOW);

// move desired # of steps
for(int steps = 0; steps < stepCount; steps++)
{
digitalWrite(MOTOR_STEP_PIN, HIGH);
delay(1);
digitalWrite(MOTOR_STEP_PIN, LOW);

} // for

digitalWrite(MOTOR_ENABLE_PIN, HIGH);

} // if

// reset flag
moveMotor = false;

}
``````