# Stepper motor \[SOLVED\]

**URL:** <https://community.sparkfun.com/t/stepper-motor-solved/27181>\
**Category:** Arduino\
**Created:** [July 26, 2013, 12:25pm UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181 "2013-07-26T12:25:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ant001](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ant001](https://community.sparkfun.com/u/Ant001)\
**Post date:** [July 26, 2013, 12:25pm UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/1 "2013-07-26T12:25:03Z")

</div>

Hello,

I have been playing with a stepper motor with the following sketch

```auto
// This Arduino example demonstrates bidirectional operation of a 
// 28BYJ-48, using a ULN2003 interface board to drive the stepper.
// The 28BYJ-48 motor is a 4-phase, 8-beat motor, geared down by
// a factor of 68. One bipolar winding is on motor pins 1 & 3 and
// the other on motor pins 2 & 4. The step angle is 5.625/64 and the 
// operating Frequency is 100pps. Current draw is 92mA. 

////////////////////////////////////////////////

//declare variables for the motor pins
int motorPin1 = 8; // Blue - 28BYJ48 pin 1
int motorPin2 = 9; // Pink - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
                        // Red - 28BYJ48 pin 5 (VCC)

int motorSpeed = 15000; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 512; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

//////////////////////////////////////////////////////////////////////////////
void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){
  if(count < countsperrev )
    clockwise();
    
  else if (count == countsperrev * 2)
    count = 0;
    
  else
    anticlockwise();
  count++;
}
//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
  for(int i = 0; i < 8; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void clockwise()
{
  for(int i = 7; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
 
}

void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

```

What I want to do is have the motor start, rotate 360 degrees clockwise, 360 degrees anticlockwise, then delay for 4 minutes, then repeat.

I have the motor going slow deliberately.

Any help will be greatly appreciated.

Regards Ant.

---

<div class="post-metadata">

**Author:** ![fll-freak](https://avatars.discourse-cdn.com/v4/letter/f/49beb7/32.png) [@fll-freak](https://community.sparkfun.com/u/fll-freak)\
**Post date:** [July 26, 2013, 12:42pm UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/2 "2013-07-26T12:42:06Z")

</div>

So what does it do? Very hard to debug a program without know what it is supossed to do and what it actually does do.

---

<div class="post-metadata">

**Author:** ![Ant001](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ant001](https://community.sparkfun.com/u/Ant001)\
**Post date:** [July 26, 2013, 1:10pm UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/3 "2013-07-26T13:10:50Z")

</div>

Sorry,

At the moment the sketch, (which came from a supplier ) currently continually rotates the output shaft one full turn forwards then backwards.

I have slowed the motor down by altering

int motorSpeed = 15000; //variable to set stepper speed

from 1200 to 15000, as I wish to operate a security light/ search light and need it to sweep the area slowly.

What I want it to do is sweep each way for 360 degrees, delay for 4 minutes then sweep again.

Thanks, Ant

---

<div class="post-metadata">

**Author:** ![mlu](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@mlu](https://community.sparkfun.com/u/mlu)\
**Post date:** [July 26, 2013, 5:41pm UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/4 "2013-07-26T17:41:05Z")

</div>

Your main loop has three possible states:

```auto
count < countsperrev

```

Then you turn clockwise, one step

```auto
count == countsperrev * 2

```

Then you reset the count and start again

```auto
else 

```

The remaining possibility when countsperrev \<= count \< 2\*countsperrev.

In this case you turn one step anticlockwise.

What you want to do is insert a 4 minute delay when you reach 2\*countsperrev before you start again.

Is this helpful ? How much Arduino and C programming do you know, this is good to know to give best possible help.

---

<div class="post-metadata">

**Author:** ![Ant001](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ant001](https://community.sparkfun.com/u/Ant001)\
**Post date:** [July 27, 2013, 11:34am UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/5 "2013-07-27T11:34:57Z")

</div>

> How much Arduino and C programming do you know, this is good to know to give best possible help.

I know very little and am trying to learn.

I have bought the book Arduino for Dummies and have been working through it, but I am still having trouble comprehending the way the code is written.

I have tried to add a one second delay in various parts of the code but it seems to either not compile or will move two steps, delay for 1 sec then move two steps again.

I will try to work with what you have given me and experiment some more.

Thanks.

Regards, Ant.

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [July 27, 2013, 2:46pm UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/6 "2013-07-27T14:46:47Z")

</div>

The code for the _loop()_, as written, is a little hard to grasp. Here’s a re-write that should preserve the original functionality but be easier to understand what’s going on. It’s important to know that the code in the _loop()_ … loops. It runs from top to bottom, taking however long it takes to run but when it gets to the bottom … the last instruction in _loop()_, it goes back to the top and executes the 1’st instruction all over again. It does this until power is removed or you reset the Arduino.

```auto
void loop(){
  if(count < countsperrev)
  {
    clockwise();
  }
  else
  {
    anticlockwise();
  }
  count++;
  if (count == countsperrev * 2)
  {
    count = 0;
  }
}

```

In this case a variable, _count_, starts = 0 and counts how many time the _loop()_ is passed through. Some function, called _clockwise()_, runs. Every time the loop is run through, top to bottom, this _clockwise()_ function will run (so long as _count_ is less than some limit) and the motor will move some amount in a clockwise direction. The counter, _count_, is then incremented by 1 at the bottom of my new _loop()_. After enough passes of the _clockwise()_ function the motor will have turned a full 360 degrees and counter will have incremented to equal _countsperrev_. The original code did exactly the same thing.

Now a different function is run, _anticlockwise()_. Just as before the _loop()_ is run over and over until the motor has gone a full 360 to get back to where it started. At this point the counter should have incremented to 2 times the limit, _countsperrev_. When the counter is equal to this value, it’s reset (= 0) and the whole process starts all over again.

The above code should make that process easier to see. Now if you want a delay and you want it to happen ONLY AFTER the full 360 clockwise and the full 360 anticlockwise, well there’s 1 place in the code where you know that’s the case and that’s where you should add your delay.

[http://arduino.cc/en/Reference/Delay](http://arduino.cc/en/Reference/Delay)

---

<div class="post-metadata">

**Author:** ![mlu](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@mlu](https://community.sparkfun.com/u/mlu)\
**Post date:** [July 27, 2013, 7:28pm UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/7 "2013-07-27T19:28:16Z")

</div>

Putting the curly brackets in place to mark off sections of code really helps.

So the 4 minute delay should be placed just before restarting the count:

```auto
void loop(){
  if(count < countsperrev)
  {
    clockwise();
  }
  else
  {
    anticlockwise();
  }
  count++;
  if (count == countsperrev * 2)
  {
    delay(240000); /* 4 minutes is 4*60*1000 milliseconds */
    count = 0;
  }
}

```

Keep working to understand how the code works, not just copy. It takes a while but then you are in control and its a great feeling.

---

<div class="post-metadata">

**Author:** ![Ant001](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ant001](https://community.sparkfun.com/u/Ant001)\
**Post date:** [July 30, 2013, 10:30am UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/8 "2013-07-30T10:30:09Z")

</div>

Thanks for the replies.

I am still having trouble.

The way you (Mee\_n\_Mac) have explained the process helps make sense to me.

I did not cut and paste any of the codes you or (Mlu) put here for me, I have tried to alter the code myself.

It seems wherever I insert the delay it either wont compile or the delay is within the loop which

moves the motor through one step then delays for two seconds, then moves the next step.

(I have been using a two second delay to see what actually happens.)

should I be looking for a better code or am I just trying in the wrong spot?

Sorry to keep asking but I really want to get on top of this problem.

Regards Ant.

---

<div class="post-metadata">

**Author:** ![Mee\_n\_Mac](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Mee\_n\_Mac](https://community.sparkfun.com/u/Mee_n_Mac)\
**Post date:** [July 30, 2013, 2:17pm UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/9 "2013-07-30T14:17:26Z")

</div>

> Ant001:  
> It seems wherever I insert the delay it either wont compile or the delay is within the loop which
> 
> moves the motor through one step then delays for two seconds, then moves the next step.
> 
> Regards Ant.

In either code, the original or my reshuffle, there's one and only one place to place a delay that acts after both complete rotations. Go back and reread what I said re: that. If you put your delay there, it should work. If it doesn't, post you code here and we'll see what's up.

---

<div class="post-metadata">

**Author:** ![Ant001](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Ant001](https://community.sparkfun.com/u/Ant001)\
**Post date:** [August 2, 2013, 12:45pm UTC](https://community.sparkfun.com/t/stepper-motor-solved/27181/10 "2013-08-02T12:45:53Z")

</div>

Thanks for the support.

I could not get the code to do what I wanted so I have found another that I was able to modify to do most of what I need.

Again thanks for the help and guidance, even though I couldn’t get it to work with the original code you gave a me a clearer understanding of the process of modifying it.

```auto
//Small Stepper Motor and Driver

/*-----( Import needed libraries )-----*/
#include <Stepper.h>

/*-----( Declare Constants, Pin Numbers )-----*/
#define STEPS 100 //Number of steps per revolution

/*-----( Declare objects )-----*/
/* create an instance of the stepper class, specifying
 the number of steps of the motor and the pins it's
 attached to. The pin connections need to be 4 pins connected
 to Motor Driver In1, In2, In3, In4 and then the pins entered
 here in the sequence 1-3-2-4 for proper sequencing*/

Stepper small_stepper(STEPS, 8, 10, 9, 11);

int Steps2Take = 0;
int relayPin1 = 12;

void setup(){  
 pinMode(relayPin1, OUTPUT); 
}
void loop()   
{
  small_stepper.setSpeed(15);
  digitalWrite (relayPin1, LOW);
  Steps2Take = 2038; 
     // Rotate CW steps = 2038
  small_stepper.step(Steps2Take);
  digitalWrite (relayPin1, HIGH);
  delay(4000);   
                                     // Pause between CW and CCW
  small_stepper.setSpeed(15);
  digitalWrite (relayPin1, LOW);
  Steps2Take = -2038; // Rotate CCW steps =2038
  small_stepper.step(Steps2Take);
  digitalWrite (relayPin1, HIGH);
  
 
  
 
   delay(10000);
}
  

/* --(end main loop )-- */

```
