Problem with sketch or not i dont know really confused

Hi im using nema 17 stepper motor with drv8825 driver my project going good but one thing happened and it continuous till now.My stepper have to lift a plate above for 70mm i also run stepper in 1/32 steps . I initially set 30000 steps to run my motor and it lift around 45mm so i added 5000 more steps but now the motor keeps on going up and hitting the top can you help me with it .

Maybe your problems begin when you try to go more than 32767 steps, the largest number that firs in an int.

Post your code and we won’t have to guess.

Try using long integers in your calculations.

HTH

a7

1 Like

so how can i assign large int value

this is my sketch below

// Define pin connections & motor's steps per revolution
const int dirPin1 = 2;
const int stepPin1 = 3;
const int stepsPerRevolution1 = 6400;

const int dirPin2 = 8;
const int stepPin2 = 9;
const int stepsPerRevolution2 = 6400;

#define M_ZERO1 4
#define M_ONE1 5
#define M_TWO1 6
#define home_switch1 7 // Pin 7 connected to Home Switch (MicroSwitch)
int steps1;        // Used to set HOME position after Homing is completed

#define M_ZERO2 10
#define M_ONE2 11
#define M_TWO2 12
#define home_switch2 13 // Pin 13 connected to Home Switch (MicroSwitch)
int steps2;        // Used to set HOME position after Homing is completed


void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);
  pinMode(M_ZERO1, OUTPUT);
  pinMode(M_ONE1, OUTPUT);
  pinMode(M_TWO1, OUTPUT);
  pinMode(home_switch1, INPUT_PULLUP);

  // Declare pins as Outputs
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  pinMode(M_ZERO2, OUTPUT);
  pinMode(M_ONE2, OUTPUT);
  pinMode(M_TWO2, OUTPUT);
  pinMode(home_switch2, INPUT_PULLUP);


  // Here's the pin settings on the DRV8825 that determines the steps and how many steps is in one revolution

  //         M0   M1    M2          RESOLUTION
  //------------------------------------------------
  //        LOW   LOW   LOW        FULL  STEP   1 Revolution  200  steps
  //        HIGH  LOW   LOW        HALF  STEP   1 Revolution  400  steps
  //        LOW   HIGH  LOW        1/4   STEP   1 Rrevolution  800  steps
  //        HIGH  HIGH  LOW        1/8   STEP   1 Rrevolution  1600 steps
  //        LOW   LOW   HIGH       1/16  STEP   1 Revolution  3200 steps
  //        HIGH  LOW   HIGH       1/32  STEP   1 Revolution  6400 steps  1500

  digitalWrite(M_ZERO1, HIGH);                        // Lets just start with a base line Full Step
  digitalWrite(M_ONE1, HIGH);
  digitalWrite(M_TWO1, HIGH);

  digitalWrite(M_ZERO2, HIGH);                        // Lets just start with a base line Full Step
  digitalWrite(M_ONE2, HIGH);
  digitalWrite(M_TWO2, HIGH);



  // Start Homing procedure of Stepper Motor 2 at startup

  while (!digitalRead(home_switch2)) {  // Do this until the switch is not activated
    digitalWrite(dirPin2, LOW);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(1000);                  // Delay to slow down speed of Stepper
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(1000);
  }

  while (digitalRead(home_switch2)) { // Do this until the switch is  activated
    digitalWrite(dirPin2, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(3000);             // More delay to slow even more while moving away from switch
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(3000);
  }

  steps2 = 0; // Reset position variable to zero
  delay(5000);



  // Start Homing procedure of Stepper Motor 1 at startup

  while (!digitalRead(home_switch1)) {  // Do this until the switch is not activated
    digitalWrite(dirPin1, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(stepPin1, HIGH);
    delayMicroseconds(200);                  // Delay to slow down speed of Stepper
    digitalWrite(stepPin1, LOW);
    delayMicroseconds(200);
  }

  while (digitalRead(home_switch1)) { // Do this until the switch is  activated
    digitalWrite(dirPin1, LOW);
    digitalWrite(stepPin1, HIGH);
    delayMicroseconds(600);             // More delay to slow even more while moving away from switch
    digitalWrite(stepPin1, LOW);
    delayMicroseconds(600);
  }

  steps1 = 0; // Reset position variable to zero
  delay(3000);



}
void loop()
{

  // Set motor 1 direction clockwise
  digitalWrite(dirPin1, LOW);
  // Spin motor slowly
  for (int x = 0; x < 32000; x++)
  {
    digitalWrite(stepPin1, HIGH);
    delayMicroseconds(200);
    digitalWrite(stepPin1, LOW);
    delayMicroseconds(200);
  }
  delay(2000); // Wait a second


  

  // Set motor 2 direction clockwise
  digitalWrite(dirPin2, HIGH);
  // Spin motor slowly
  for (int x = 0; x < 5000; x++)
  {
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(1000);
  }
  delay(2000); // Wait a second

  

  // Set motor 1 direction counterclockwise
  digitalWrite(dirPin1, HIGH);
  // Spin motor quickly
  for (int x = 0; x < 32000; x++)
  {
    digitalWrite(stepPin1, HIGH);
    delayMicroseconds(200);
    digitalWrite(stepPin1, LOW);
    delayMicroseconds(200);
  }
  delay(2000); // Wait a second



  // Set motor 1 direction clockwise
  digitalWrite(dirPin1, LOW);
  // Spin motor slowly
  for (int x = 0; x < 32000; x++)
  {
    digitalWrite(stepPin1, HIGH);
    delayMicroseconds(200);
    digitalWrite(stepPin1, LOW);
    delayMicroseconds(200);
  }
  delay(2000); // Wait a second

  

  // Set motor 2 direction clockwise
  digitalWrite(dirPin2, LOW);
  // Spin motor slowly
  for (int x = 0; x < 5000; x++)
  {
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(1000);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(1000);
  }
  delay(2000); // Wait a second

  

  // Set motor 1 direction counterclockwise
  digitalWrite(dirPin1, HIGH);
  // Spin motor quickly
  for (int x = 0; x < 32000; x++)
  {
    digitalWrite(stepPin1, HIGH);
    delayMicroseconds(200);
    digitalWrite(stepPin1, LOW);
    delayMicroseconds(200);
  }
  delay(2000); // Wait a second
}

replace your int definitions with long int, e.g.

for (long int x = 0; x < 32000L; x++)

note the constant 32000L is declared as a long int

you could use unsigned int which has a range 0 to 65535

for (unsigned int x = 0; x < 32000U; x++)

have a look at arduino int

hi, thanks mate

is i must to add L and U in the int for long and unsigned ?

as you added in the eg you shown

it is wise to add the L U etc
the compiler may issue a warning or just carry on assuming you know what you are doing, e.g.
the following compiles for an Arduino Mega (no warning on my system)

void setup() {
  Serial.begin(115200);
  int x =  33000;
  Serial.println(x);
}

when run it gives

-32536

the value 33000 cannot be represented as a 16bit signed integer (the default int on an Arduino mega) and ends up as a negative value

the following program run on a PC using GNU C

int main(void) {
  int x =  33000;
  printf("%d\n", x);
}

gives

33000

the default int is 32 bits in this case

it is important to know the range of numeric data for the system you are using

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.