Stepper motor [SOLVED]

Hello,

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

// 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.

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.

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

Your main loop has three possible states:

count < countsperrev

Then you turn clockwise, one step

count == countsperrev * 2

Then you reset the count and start again

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.

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.

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.

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

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:

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.

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.

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.

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.

//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 )-- */