I'm trying to run a stepper motor a fixed number of steps before stopping. I'm having trouble implementing the bool has_run variable. I've carried out debugging with the serial monitor which confirms that the counter is working and the has_run variable gets set to true once the counter == steps. However the move stepper function does not seem to work! Only when I remove the has_run = true assignment will the stepper motor move and then of course this does so in a loop and does not stop once it has moved the specified number of steps. Does anybody know what I am doing wrong here? Apologies if it's obvious but I've wasted a fair few hours trying to work this out and it's still beyond me at this point. I'm using the Arduino IDE.

// defines pins numbers
#include<math.h> 

const int stepX = 2;
const int dirX  = 5;
const int stepY = 3;
const int dirY  = 6;
const int stepZ = 4;
const int dirZ  = 7;
const int enPin = 8;

signed int steps = 0;
signed int step_counter = 0;
bool has_run = false;

void setup() 
{
  Serial.begin(9600);
  
  // Sets the two pins as Outputs

  pinMode(stepX,OUTPUT);
  pinMode(dirX,OUTPUT);

  pinMode(stepY,OUTPUT);
  pinMode(dirY,OUTPUT);

  pinMode(stepZ,OUTPUT);
  pinMode(dirZ,OUTPUT);

  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);

//  digitalWrite(dirX,HIGH);
//  digitalWrite(dirY,LOW);
//  digitalWrite(dirZ,HIGH);

}

void loop() {

if (has_run == false)

{
steps = 20;
move_motor_x (steps);

if (step_counter == steps)
{
 has_run = true;
}

Serial.print("step_counter: ");
Serial.println(step_counter);
Serial.print("has_run: ");
Serial.println(has_run);
}

}
  
  void move_motor_x(signed int steps)
{
    if (steps > 0)
    {
      digitalWrite(dirX,HIGH);
      for(signed int x = 0; x < steps; x++) 
      {
        digitalWrite(stepX,HIGH);
        delayMicroseconds(50);
        digitalWrite(stepX,LOW);
        delayMicroseconds(50);
        step_counter = step_counter + 1;
      }
    }
    else if (steps < 0)
    {
      digitalWrite(dirX,LOW);
      for(signed int x = 0; x > steps; x--) 
      {
        digitalWrite(stepX,HIGH);
        delayMicroseconds(50);
        digitalWrite(stepX,LOW);
        delayMicroseconds(50);
        step_counter = step_counter - 1;
      }
    }
}

void move_motor_y(int steps)
{
    if (steps > 0)
    {
      digitalWrite(dirY,HIGH);
      for(int x = 0; x < steps; x++) 
      {
        digitalWrite(stepY,HIGH);
        delayMicroseconds(50);
        digitalWrite(stepY,LOW);
        delayMicroseconds(50);
        step_counter = step_counter + 1;
      }
    }
    else if (steps < 0)
    {
      digitalWrite(dirY,LOW);
      for(int x = 0; x > steps; x--) 
      {
        digitalWrite(stepY,HIGH);
        delayMicroseconds(50);
        digitalWrite(stepY,LOW);
        delayMicroseconds(50);
        step_counter = step_counter - 1;
      }
    }
}

void move_motor_z(int steps)
{
    if (steps > 0)
    {
      digitalWrite(dirZ,HIGH);
      for(int x = 0; x < steps; x++) 
      {
        digitalWrite(stepZ,HIGH);
        delayMicroseconds(50);
        digitalWrite(stepZ,LOW);
        delayMicroseconds(50);
        step_counter = step_counter + 1;
      }
    }
    else if (steps < 0)
    {
      digitalWrite(dirZ,LOW);
      for(int x = 0; x > steps; x--) 
      {
        digitalWrite(stepZ,HIGH);
        delayMicroseconds(50);
        digitalWrite(stepZ,LOW);
        delayMicroseconds(50);
        step_counter = step_counter - 1;
      }
    }
}




0

There are 0 best solutions below