Pages: [1]   Go Down
Author Topic: I'm having trouble getting my stepper motor to turn once every hour  (Read 178 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  ////////////////////////////////             
  //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?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 290
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 1
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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?

Code:
 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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Like this?

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

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

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 1
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 92
Posts: 4703
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

UK
Offline Offline
Faraday Member
**
Karma: 101
Posts: 6169
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

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

...R
Logged

Pages: [1]   Go Up
Jump to: