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 :wink:

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

ADDN:
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! :slight_smile:

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.