I'm having trouble getting my stepper motor to turn once every hour

  ////////////////////////////////              
  //TWO WIRE STEP/DIR DRIVER BOARD CODE      
  int Motor1StepPin = 7;
  int Motor1DirPin = 8; 
  
  //The number of steps required for your stepper to make one revolution. (Don't forget to take into 
  //account the settings on your driver board. i.e. Microstepping, half stepping etc.)
  float steps = 4800;  
    //Set the travel speed for your stepper motors here. (In Rev/Min)
    //Note: There is a limit to how fast or slow this code is able to spin the stepper motors.
    //You can try experimenting with the "delayMicroseconds" code if you need different speeds.
  int altSpeed=1;
  
  //////////////////////////////// 

  float Motor1Delay, doSteps;

void setup()
{ 
    Serial.begin(9600);
  ////////////////////////////////              
  //TWO WIRE STEP/DIR DRIVER BOARD CODE  
  pinMode(Motor1StepPin, OUTPUT);
  pinMode(Motor1DirPin, OUTPUT);
  Motor1Delay = ( 1000000 * ( 60 / (steps * altSpeed) ) ) / 2;
  ////////////////////////////////  

        pinMode(6, OUTPUT);
//      pinMode(7, OUTPUT);      
//      pinMode(13, OUTPUT);
    

         
}

void loop()
{
  
  delay(1000); 
  digitalWrite(6, LOW);
  delay(100); 
  
  moveToPosition(1, 0, -steps * 1);  
  moveToPosition(1, 0, steps * 1);   
  moveToPosition(1, -steps * 1, 0); 
  moveToPosition(1, steps * 1, 0); 

  digitalWrite(6, HIGH);  

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This code moves the stepper motors
void moveToPosition(int limitSearch,  long altsteps, long azsteps){
    
//    Serial.println("altsteps");
//    Serial.println(altsteps);
//    Serial.println("azsteps");
//    Serial.println(azsteps);                 
        
  ////////////////////////////////              
  //TWO WIRE STEP/DIR DRIVER BOARD CODE    
  if (abs(altsteps)==altsteps){digitalWrite(Motor1DirPin, HIGH);}else{digitalWrite(Motor1DirPin, LOW);}

     
  for (doSteps=1; doSteps <= abs(altsteps); doSteps++){
     digitalWrite(Motor1StepPin, HIGH);
     delayMicroseconds(int(Motor1Delay));
     digitalWrite(Motor1StepPin, LOW); 
     delayMicroseconds(int(Motor1Delay));
  }
    
}

Above is the only code I’ve been able to use with my driver/motor set up. The driver I have is set up in 2 pin (step/dir) mode.

I want to modify the above code to turn the motor around once every HOUR instead of once every minute like it is currently setup to do.

I have experimented with changing delayMicroseconds to Delay and adjusting the values to what I’d think wold be appropriate to turn the motor at 1 revolution per hour but all it does is make strange unpleasant noises.

How can I get my motor to turn one revolution in an hour?

duncan916: How can I get my motor to turn one revolution in an hour?

There seems to be a lot of complexity in that code for something that ought to be very simple. Ultimately, all you need to do is set the direction pin to the right state and then toggle the step pin at the required frequency. If you want to take 4800 steps in an hour then you need to send a pulse to the step pin every 750ms which means toggling the state every 375ms. There are various ways to code that, but a fairly simple approach which also leaves your sketch free to control other things as well as the stepper motor would look like this:

void doStepping()
{
    static const unsigned long HALF_STEP_INTERVAL = 375; // ms
    static unsigned long lastStepTime = 0;

    if(millis() - lastStepTime >= HALF_STEP_INTERVAL)
    {
        digitalWrite(Motor1StepPin, !digitalRead(Motor1StepPin));
        lastStepTime += HALF_STEP_INTERVAL ;
    }
}

If you call that function repeatedly, for example each time loop() is called, it will step the motor at the appropriate time.

http://arduino.cc/en/reference/stepper

If you need an accurate time, you'd need to install an RTC / clock. Time on the arduino will drift.

PeterH:

duncan916: How can I get my motor to turn one revolution in an hour?

There seems to be a lot of complexity in that code for something that ought to be very simple. Ultimately, all you need to do is set the direction pin to the right state and then toggle the step pin at the required frequency. If you want to take 4800 steps in an hour then you need to send a pulse to the step pin every 750ms which means toggling the state every 375ms. There are various ways to code that, but a fairly simple approach which also leaves your sketch free to control other things as well as the stepper motor would look like this:

void doStepping()
{
    static const unsigned long HALF_STEP_INTERVAL = 375; // ms
    static unsigned long lastStepTime = 0;
if(millis() - lastStepTime >= HALF_STEP_INTERVAL)
{
    digitalWrite(Motor1StepPin, !digitalRead(Motor1StepPin));
    lastStepTime += HALF_STEP_INTERVAL ;
}

}




If you call that function repeatedly, for example each time loop() is called, it will step the motor at the appropriate time.

Like this?

  int Motor1StepPin = 7;
  int Motor1DirPin = 8; 

  float steps = 4800;  

  int altSpeed=1;
  
  float Motor1Delay;

void doStepping()
{
    static const unsigned long HALF_STEP_INTERVAL = 375; // ms
    static unsigned long lastStepTime = 0;

    if(millis() - lastStepTime >= HALF_STEP_INTERVAL)
    {
        digitalWrite(Motor1StepPin, !digitalRead(Motor1StepPin));
        lastStepTime += HALF_STEP_INTERVAL ;
    }
}

What am I missing? I'm very new to arduino

duncan916: Like this?

What am I missing? I'm very new to arduino

You're missing loop() and setup() functions.

PeterH:

duncan916: Like this?

What am I missing? I'm very new to arduino

You're missing loop() and setup() functions.

Thanks... I'm having another problem...

I should have mentioned I have the motor set up in 1/4 stepping mode, which is probably why it took 30 minutes to turn 90º. Unfortunately the only instructions that came with my driver board were how to set it up in 1/4 step mode.

Also, I'm getting an unpleasant speaking noise coming from my stepper between steps. http://youtu.be/EzqkM3plGu0 Hopefully you can hear it on that video.

There's a low purring sound then a high pitched squeak, each about a second long and it's not my gears it's coming from inside the motor. Happens between each step.

Here's my code:

  int Motor1StepPin = 7;
  int Motor1DirPin = 8; 

  float steps = 800;  

  int altSpeed=1;
  
  float Motor1Delay, doSteps;
  
void setup()
{ 
    Serial.begin(9600);
  ////////////////////////////////              
  //TWO WIRE STEP/DIR DRIVER BOARD CODE  
  pinMode(Motor1StepPin, OUTPUT);
  pinMode(Motor1DirPin, OUTPUT);
  Motor1Delay = ( 1000000 * ( 60 / (steps * altSpeed) ) ) / 2;
  ////////////////////////////////  

        pinMode(6, OUTPUT);
//      pinMode(7, OUTPUT);      
//      pinMode(13, OUTPUT);
    

         
}

void loop()
{
  
  delay(1000); 
  digitalWrite(6, LOW);
  delay(100); 
  
  doStepping(); 

  digitalWrite(6, HIGH);  

}



  

void doStepping()
{
    static const unsigned long HALF_STEP_INTERVAL = 375; // ms
    static unsigned long lastStepTime = 0;

    if(millis() - lastStepTime >= HALF_STEP_INTERVAL)
    {
        digitalWrite(Motor1StepPin, !digitalRead(Motor1StepPin));
        lastStepTime += HALF_STEP_INTERVAL ;
    }
}

The motor also gets a lot hotter than usual. What do I have to change in my code to set things up correctly to control the motor in 1/4 stepping mode? Hopefully that will get rid of the squeaking and cool the motor down. If there's anything else that looks wrong in my code please let me know.

I suggest you look at the documentation for your stepper motor driver to find how to select the stepping mode. Also find out the voltage/current requirements of your motor and see whether it is designed to be powered continuously with the voltage you’re using.

Maybe the motor step pin should only be ON (or OFF depending on wiring) for a short time?

The sounds on the video... 2 of them sound like airflow in and from a hose.

I don't think you have said what stepper driver board you are using - it would be useful to know.

If it just requires step and direction inputs then a step can probably be generated like this - this certainly works with an A4988 stepper driver.

void motorStep() {
    digitalWrite(stepPin, HIGH);
    digiitalWrite(stepPin, LOW);
}

and then all your timing needs to do is produce steps at the appropriate intervals.

...R

Robin2: I don't think you have said what stepper driver board you are using - it would be useful to know.

If it just requires step and direction inputs then a step can probably be generated like this - this certainly works with an A4988 stepper driver.

void motorStep() {
    digitalWrite(stepPin, HIGH);
    digiitalWrite(stepPin, LOW);
}

and then all your timing needs to do is produce steps at the appropriate intervals.

...R

Where do I put that into my code? What do I replace?

duncan916: The motor also gets a lot hotter than usual. What do I have to change in my code to set things up correctly to control the motor in 1/4 stepping mode? Hopefully that will get rid of the squeaking and cool the motor down. If there's anything else that looks wrong in my code please let me know.

Sounds like your stepper driver is getting hot and shutting down, then restarting. This is consistent with the motor getting hot - it is drawing a lot of current. You either need to put some cooling on the driver, reduce the current limit or use a bigger driver.

The squeaking is probably normal for any microstepping driver, they use PWM to control the current. Often some of this ends up in an audible band. It may be possible to tune out the noise, depends on the stepper driver.

duncan916: Where do I put that into my code? What do I replace?

You just need a mechanism to call that function whenever a step is required. For example

unsigned long stepInterval = 1000;  // millisecs
unsigned long prevStepMillis;

void loop() {
    curMillis = millis();
    if (curMillis - prevStepMillis >= stepInterval) {
         motorStep();
        prevStepMillis += stepInterval;
    }
}

If your think your motor is getting too hot perhaps you have not properly adjusted the current limit on your stepper driver board.

...R

quarter stepping mode is a bad idea if you want to hold the position for a long time.

A stepper motor will stop better at a full step position, holding at a quarter step position requires more continuous current.