Go Down

Topic: stepper Motor freezes after about 10 seconds (Read 472 times) previous topic - next topic

Ziayakens

I am using a sparkfun stepper motor with the big easy driver shield powered by a 12V power source with 2 amps. I have no load on the motor and I have the potentiometer at slightly below half (as there is no load and I need to motor to spin for about 20 min without heating up) but for some reason when I run the code below it locks up and doesnt spin after about 10 seconds

any ideas?

Code: [Select]
//Declare pin functions on Arduino
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define MS3 6
#define EN  7

//Declare variables for functions

long previousTime;

void setup() {
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(MS3, OUTPUT);
  pinMode(EN, OUTPUT);
  resetBEDPins(); //Set step, direction, microstep and enable pins to default states
  digitalWrite(EN, LOW);
 
  digitalWrite(MS1, HIGH);
  digitalWrite(MS2, HIGH);
  digitalWrite(MS3, LOW);
  digitalWrite(dir, LOW);

  previousTime = millis();
}

//Main loop
void loop() {
  if (timePassed()) {
    oneStep();
  }
}

bool timePassed(){
  int now = millis();
  if (now > previousTime + 5) {
    previousTime = now;
    return true;
  } else {
    return false;
  }
}

void oneStep() {
  digitalWrite(stp,HIGH); //Trigger one step forward
  digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
}

void resetBEDPins() {
  digitalWrite(stp, LOW);
  digitalWrite(dir, LOW);
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  digitalWrite(MS3, LOW);
  digitalWrite(EN, HIGH);
}
 

Delta_G

#1
Jun 11, 2018, 03:45 am Last Edit: Jun 11, 2018, 03:46 am by Delta_G
Show your wiring.  You're not pulling the power for the stepper directly through the arduino are you?  You've got transistors on all those lines?  Or a stepper driver? 

Code: [Select]
if (now > previousTime + 5)

That line is probably not an issue now but it's got a bug in it if the code ever runs to the millis rollover. 

Write this way to be safe:

Code: [Select]
if (now - previousTime >= 5)
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

jremington

#2
Jun 11, 2018, 05:26 am Last Edit: Jun 11, 2018, 05:29 am by jremington
Post a link to the stepper motor or the data sheet.  To what value did you set the current limit?

The Big Easy Driver is overrated, and unless you have set the current limit to less than about 1.4 A, it may be overheating and shutting down.

Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

This code is faulty.
Code: [Select]

bool timePassed(){
  int now = millis();
  if (now > previousTime + 5) {
    previousTime = now;
    return true;
  } else {
    return false;
  }
}

Firstly millis() returns unsigned long, not int, so you will see wraparound after a few tens of seconds.

Secondly as pointed out by Delta_G your test is broken for wraparound.

Here is how to do things properly:

Code: [Select]

bool timePassed(){
  if (millis() - previousTime >= 5) {
    previousTime += 5 ;
    return true;
  } else {
    return false;
  }
}

You declared previousTime as long, which is going to work, but unsigned long is more correct.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Ziayakens

These links may help
Stepper Motor Basics
Simple Stepper Code

Was there something obvious that indicated a lack of understanding? I apologize if I poorly articulated my circumstances.

Firstly millis() returns unsigned long, not int, so you will see wraparound after a few tens of seconds.

Secondly as pointed out by Delta_G your test is broken for wraparound.
You declared previousTime as long, which is going to work, but unsigned long is more correct.
Thank you Mark and Delta_G, its turns out the if statement logic was faulty, after fixing it with your suggestion it run as long as I needed!

jremington

#6
Jun 14, 2018, 04:09 am Last Edit: Jun 14, 2018, 04:11 am by jremington
Quote
Was there something obvious that indicated a lack of understanding?
Yes, the following quote demonstrates that you don't understand 1) how to properly set the current limit, and 2) that stepping motors consume the same amount of power regardless of load, or whether they are even stepping.

Quote
I have the potentiometer at slightly below half (as there is no load and I need to motor to spin for about 20 min without heating up)

Robin2

Was there something obvious that indicated a lack of understanding? I apologize if I poorly articulated my circumstances.
You will note that I started Reply #3 with "These links may help"

Also, when you have read 500 Forum Threads you will realize that many people do start questions with very little background knowledge. It is simpler to provide it than to start a long question and answer session to find out the OPs level of knowledge. Many are also very reluctant to answer simple questions.


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Delta_G

Quote
Many are also very reluctant to answer simple questions.
And many will take it all personally and act like total jerks about it.  You'd be amazed how many people get their knickers in a knot over asking them for some basic information that they didn't include and don't think is important.  "How dare you ask me if I connected the grounds don't you know who I am".
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up