Rotating bipolar stepper motor, then stop, then other direction

Hello everyone, I write this message urgently because I have to realize a project that is in a week, I want to run a bipolar stepper motor with an Arduino Uno and Motor Shield in one direction for a given time, wait a certain number of seconds before turning it in the other direction for a given time, all once. The motor is connected to Motor Shield terminals A and B :slight_smile:

I found the following code to run the motor infinitely in one direction :

int delaylegnth = 30;

void setup() {
  
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B


  
  
}

void loop(){
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylegnth);

}

And in the other direction :

int delaylegnth = 30;

void setup() {
  
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B


  
  
}

void loop(){
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(delaylegnth);

}

In addition, I wish it all launches after pressing a button whose code is:

pinBouton=6;  
  pinMode(pinBouton,INPUT);
}



void loop(){
 

  boolean etatBouton = digitalRead(pinBouton);
  
  
  if (etatBouton==HIGH)

So, I am writing to you to know how to periodize the time of the rotations, and add a delay in the middle. Thank you very much for your precious help :slight_smile:

See BlinkWithoutDelay for working with the Arduino system timer.

You may be better off using the Stepper library, instead of code that uses delay().

Thank you for replying,

The arduino stepper library is full of codes that are made to rotate a stepper motor only when it's connected in the pins (8;9;10;11), but, my motor isn't connected there (it is connected on the blue bar, A+; A- and B+; B-)

thank you for your help

  1. There are plenty of different motor shields, so what is yours (exactly)?
  2. All these motor shields are connected via pins and headers with your Arduino and such they use Arduino pins which send direction and pulse signals to the motor shield.

You have to find out, what Arduino pins are used to send those signals to your shield and accordingly your sketch has to be adopted to those pins.
So make first sure what you are doing and learn a bit more about the basics around Arduinos before you start with the non-trivial field of stepper motors.

Hello, thanks for answering, I am using a Motor shield R3 but I don't know what stepper motor I'm using (it is from a CD reader) and all I know is in the code I sent above.
Thank you :slight_smile:

I think, the following link can help you:
http://forum.arduino.cc/index.php?topic=194077.0

Just play with the delay functions. Mightbe easiest for a newbie to start with and have some success. Later you should really study the "Blink without delay" example and try to substitute the delay function in your sketch(es) from the time on you have understood that blink example.

Be careful not to fry your little motor as I don't know if that R3 shield can limit the motor current and those little stepper motors don't need much current.

Thank you for answering, but I have found the answer of my problem.
Thanks again :slight_smile:

It would be nice and helpful for other members of the forum with similar problems to tell them what exactly did the trick.

The benefit of a forum is to share knowledge and experience - it is not meant to be a one way street where someone shows up, gets some answers and goes away with a solution.

This is called hotline service and normally you have to pay for that (either included in the goods which you bought or extra).

In order to solve my problem, the only thing I did is to copy paste the same code making the motor rotate 1 step in a direction a huge amount of time, then add delay, then, I did the same thing with the code to the other direction.
That's all

I agree with rpt007 so why not just post the code?

Maybe because the code is 4000 lines long??