# Stepper Motor forward and backwards [Solved]

Hello everyone,

First I would like to say posting was my last resort, very last after reading through previous threads and doing some problem analysis on my own. Seems as though I wasnt as lucky as I had hoped so here I am

Starting this project I knew hardly anything of arduino, just some basic first year student Electrical Engineering Fundamentals, so when I was able to run my stepper x amount of steps within a few trys I was very pleased, the sketch I used is a mish mash of a lot of things that make minimal sense to me, as I am very fresh in arduino.

I am using a hybrid stepper with a texas instruments L293D Motor driver.
This is what I have for my attempt at a forwards and then reverse direction:

``````boolean trigger_forward = false ;
boolean trigger_backward = false ;
unsigned long time = 0L ;
int Distance = 0;

#include <Stepper.h>

const int stepsPerRevolution = 200;Â
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;Â  Â  Â  Â

void setup() {

Â  Serial.begin(9600);
}

void loop ()
{Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â
Â   if ( millis () - time >= 20000 && trigger_forward)Â  Â  Â  Â  Â  Â  Â  Â
Â  {
Â  Â  time = millis() ;
Â  Â  trigger_forward = false ;
Â  Â  trigger_backward = true ;
Â  Â  myStepper.step (-15000) ;

Â  }
Â
Â  delay(1000);
Â
Â  ifÂ  (millis ()- time >= 32000L && trigger_backward)Â
Â  {
Â  Â  time = millis();
Â  Â  trigger_backward = false ;
Â  Â  trigger_forward = true;
Â  Â  myStepper.step (1450) ;
Â  Â  Â  Â
Â  }
}
``````

^to those who understand what they are reading here, this is more than defiantly a large eye sore, and for that my apologizes. This compiles for me, I just get no results from the motor.

If anyone could lead me down the right path I would be more than thankful, Ive made it this far on my own and would like to continue to use this as a learning opportunity for myself, i just need a push at this point.

Essentially i need help analyzing the problem, and there more than likely are multiple in this sketch

This compiles for me, I just get no results from the motor.

Start simple. Try this

``````#include <Stepper.h>

const int stepsPerRevolution = 200;Â
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;Â  Â  Â  Â

void setup() {

Â  Serial.begin(9600);
Â  myStepper.setSpeed(15); // RPM
Â  Serial.println("starting");
Â  myStepper.step(400);
Â  Serial.println("finished");
}

void loop ()
{
}
``````

If that does not work then post a link to the datasheet for your stepper motor and post a diagram showing how you have everything connected. See this Simple Image Guide

â€¦R

@Robin2

Much more simple than what I had for a single direction program, and what you have is much easier for a newbie to understand.

I made these changes and was able to add the opposite direction rotation right after

``````#include <Stepper.h>

const int stepsPerRevolution = 200;Â
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;Â  Â  Â  Â

void setup() {

Â  Serial.begin(9600);
Â  myStepper.setSpeed(15); // RPM
Â  Serial.println("starting");
Â  myStepper.step(400);
Â  Serial.println("finished");
Â
Â   Serial.begin(9600);
Â  myStepper.setSpeed(15); // RPM
Â  Serial.println("starting");
Â  myStepper.step(-400);
Â  Serial.println("finished");
}

void loop ()
{
}
``````

A co-worker of mine always says less is better, this proves itâ€¦

I think I had many unneeded lines and commands that weighed it down and confused it?

Thanks!

The Arduino never gets confused. It always does exactly what you told it to do. If you're confused and you tell it to do something stupid, then it follows your instruction perfectly.

Your mix of delay() and millis() seems confused. If you are using millis() then you never need anything bigger than delay(1) and even that big of a delay is pretty rare.

That makes sense. Im gonna learn the difference of milli() and delay() and look into the mess I had there a bit more even though I had reached my goal with what Robin shared.

Thanks everyone for the feedback.