Stepper Motor Control

Dear Arduino-ers,

It’s my first post here - I did use the search, but wasn’t lucky:
Right now I’m trying to control a Stepper Motor in a very basic way, it’s a QMOT Stepper hooked
up to a schmalzhaus easy driver, that I control using a Arduino Mega.

To do this, I wanted to to write an own method that I would call from the loop() method:

void setup() 
{
  Serial.begin(9600);
  pinMode(3, OUTPUT); // Direction of turning
  pinMode(2, OUTPUT); // Microsteps
}


void loop()
{

turnMotor(8000, 115, 1, 3, 2); // Calling my own method to turn 8000 steps with 115 speed, direction right, on pins 3 for direction and 2 for stepping
delay(1000);

// Microsteps-Maximum 8000 when I call it from another method. If I use any more steps than 8000, the motor won't turn at all.
// Delay time minimum 115 microseconds
// 1 = Right, 0 = Left

/*for (int i = 0; i<160000; i++)    // 1600 = one Turn. When I leave this loop right inside the void loop() method, the Motor will turn for 160 000 steps just fine!
  {
    digitalWrite(2, LOW);        
    digitalWrite(2, HIGH);      
    delayMicroseconds(115);      
  }           
  delay(1000);
 */
 
}


void turnMotor(int microsteps, int turnspeed, int leftright, int dirpin, int steppin){

 /* Serial.println("Turning for: " + microsteps); // This is another thing that won't work. The Serial won't print any of the variables ...
  Serial.println("Turnspeed: " + turnspeed);
  Serial.println("Direction: " + leftright); // and it does only print the strings (without any integer vars showing) until this portion here
  Serial.println("On Pins (direction): " + dirpin);
  Serial.println("and (step): " + steppin);
 */ 
  if(leftright==0){
     digitalWrite(dirpin, LOW);     
  }else{ 
     digitalWrite(dirpin, HIGH);    
  }
   
  for (int i = 0; i<microsteps; i++)    
  {
    digitalWrite(steppin, LOW);       
    digitalWrite(steppin, HIGH);       
    delayMicroseconds(turnspeed);      // Minimum Delay Time - 115 Microseconds für den QMOT Schrittmotor
  }                                    
  
}

My problem is, that if I call the for-loop out of the void loop() method, it can go up to almost unlimited microsteps
(that are defined by how often the for-loop loops and creates the PWM signal)

HOWEVER: When I use my void turnMotor method, if I enter any variable higher than 8000, the motor will just not
move at all.

It really drives me crazy: Why can I loop the for-loop as often as I want inside of the void loop() method,
but why won’t it loop that often if I tell my own method to do so, why will it just stop moving the motor?
Is there a limit in Arduino regarding the size/length of an Integer that you can use as a var. inside your own method?

I’d be happy for any help,
Regards,

  • Max

Is there a limit in Arduino regarding the size/length of an Integer that you can use as a var. inside your own method?

Integer variables are the same size whether they are defined/used in loop, as an argument, or inside another function. They are exactly 2 bytes long. As a result, there is a range of values that can be stored. An int can hold a value between -32767 and +32767.

When I leave this loop right inside the void loop() method, the Motor will turn for 160 000 steps just fine!

It shouldn't. The value 160000 is way outside the range that can be stored in an int. How are you counting that the motor turned that many steps?

If you need to step that many times, you should be using long as the variable type.

Wohoo! Thanks so much! Using a long instead of an int variable totally solved the problem! I was thinking that the motor turned longer inside of the void loop() method than inside of my own one, because it itself also loops (Which I knew since my very first lines of code in Arduino) - I simply forgot to add a delay between the loops, so the movement really seemed infinite using void loop() - silly me.

Greez, - Max