Out of Storage Space I hit Overflow

Hello Everyone, I am new to this forum. I have been working on an invention and I am using a geared stepper. My problem is my normally 200 step motor is now 2750 steps for one revolution. My code worked for a 200 but not for the geared motor because I cant count enough steps, I run out of range. I can get half of what I need but not all. I need to get at least 40 revs but I can only get 20. Any ideas?

const int stepPin = 5;
const int dirPin = 3;
const int stepsPerRevolution = 1375;
const int buttonPin = 4;
int buttonState = 0;
int Distance = 0;
void setup()
{
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(buttonPin, INPUT);

}
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

// Set motor direction clockwise
digitalWrite(dirPin, LOW);

// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
Distance = Distance + 1;
}
} else {
// turn motor off:
digitalWrite(stepPin, LOW);
if ( Distance == 1375 * 20) {
// Set motor direction counterclockwise
digitalWrite(dirPin, HIGH);
const int stepsPerRevolution = 1375 * 20;
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
Distance = 0;
}
} else {
// turn motor off:
digitalWrite(stepPin, LOW);

}
}
}

Use long instead of int, but beware of int overflow, even in simple expressions.

Please remember to use code tags when posting code

1 Like

OK, Thank you, do you have an example? I tried "L" but it didnt do anything?

Sorry, I'm not sure how to dot he code tags but I will figure it out :slight_smile:

I can't see an overflow problem in your code, but the "Distance == 20 * 1375" is a bit suspect - the chances of hitting the button at exactly the right moment seem remote.

When you figure out code tags, repost the code, but first use the IDE's auto format tool.

20 * 1375 would be ok, 40 * 1375 will overflow an int.

Exactly. That’s my issue. Any suggestions?

Richard Potter

What is?
The low probability of seeing the switch at exactly the right moment, or the limited range of the int?

It’s the range. The button works right. I need help with how to use long instead of I int.

Richard Potter

Make the Distance variable type long.
If you have "1375* 40" as a literal, use "1375L * 40L" instead.

HI, @rpotter65
Hi

Okay, I'll set up your project here and test it.
which driver you are using. A4988?

RV mineirin

OK,
Here it worked correctly, but I had to change the 4
delayMicrosecons(500) to delayMicrosecons(800), so that
my engine didn't miss steps.

I think your engine is failing steps.
Increase delay times for testing.

RV mineirin

Yes That is the correct driver. I did the auto format, I hope this looks better?

const int stepPin = 5;
const int dirPin = 3;
const int stepsPerRevolution = 1375;
const int buttonPin = 4;
int buttonState = 0;
int Distance = 0;
void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(buttonPin, INPUT);

}
void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {

    // Set motor direction clockwise
    digitalWrite(dirPin, LOW);

    // Spin motor slowly
    for (int x = 0; x < stepsPerRevolution; x++)
    {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
      Distance = Distance + 1;
    }
  } else {
    // turn motor off:
    digitalWrite(stepPin, LOW);
    if ( Distance == 1375 * 20) {
      // Set motor direction counterclockwise
      digitalWrite(dirPin, HIGH);
      const int stepsPerRevolution = 1375 * 20;
      // Spin motor slowly
      for (int x = 0; x < stepsPerRevolution; x++)
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(500);
        Distance = 0;
      }
    } else {
      // turn motor off:
      digitalWrite(stepPin, LOW);

    }
  }
}

I will try that, Did you use my code?

Yes

I did the same and I'm getting the same result, The stepper does not reverse after 40 revs? Frustrating cuz it does work with a 200 step motor.

Instead of:
int Distance = 0;

Use long
long Distance = 0:

How to use
'Code tags' is shown in the first sticky posts on the top of every forum.

Getting closer, It will reverse at 25 now but now it wont stop? Sorry guys, still learning.

const int stepPin = 5;
const int dirPin = 3;
const long stepsPerRevolution = 1375;
const int buttonPin = 4;
int buttonState = 0;
long Distance = 0;
void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(buttonPin, INPUT);

}
void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {

    // Set motor direction clockwise
    digitalWrite(dirPin, LOW);

    // Spin motor slowly
    for (int x = 0; x < stepsPerRevolution; x++)
    {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
      Distance = Distance + 1;
    }
  } else {
    // turn motor off:
    digitalWrite(stepPin, LOW);
    if ( Distance == 34375L) {
      // Set motor direction counterclockwise
      digitalWrite(dirPin, HIGH);
      const long stepsPerRevolution = 34375L;
      // Spin motor slowly
      for (int x = 0; x < stepsPerRevolution; x++)
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(500);
        Distance = 0;
      }
    } else {
      // turn motor off:
      digitalWrite(stepPin, LOW);

    }
  }
}

Hello again everyone I have been able to get the code to advance the motor 40 revs now and then reverse. However, once in reverse, it doesn't stop after 40 revs? I know I am missing something stupid. Anyone have any ideas? Thank you all for your help so far. I am getting closer.

const int stepPin = 5;
const int dirPin = 3;
long stepsPerRevolution = 1375;
const int buttonPin = 4;
int buttonState = 0;
long Distance = 0;
void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(buttonPin, INPUT);

}
void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {

    // Set motor direction clockwise
    digitalWrite(dirPin, LOW);

    // Spin motor slowly
    for (int x = 0; x < stepsPerRevolution; x++)
    {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
      Distance = Distance + 1;
    }
  } else {
    // turn motor off:
    digitalWrite(stepPin, LOW);
    if (Distance == 1375L * 40L) {
      // Set motor direction counterclockwise
      digitalWrite(dirPin, HIGH);
      const long stepsPerRevolution = 1375L * 40L;
      // Spin motor slowly
      for (int x = 0; x < stepsPerRevolution; x++)
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(500);
        long Distance = 0L;
      }
    } else {
      // turn motor off:
      digitalWrite(stepPin, LOW);

    }
  }
}

I think you need to look into the scope of variables as you are not changing the variable you might think you are. The line:
const long stepsPerRevolution = 1375L * 40L;
create a new variable called stepsPerRevolution that is only used within the scope of the for loop and is then discarded it is not the same as the global variable of the same name you have used.
Similarly for long Distance = 0L; in this case the varaiable Distance is created and initialised but then discarded as you exit the For loop - it is not the Distance variable you use elsewhere.

It is normally bad practice to use variables of the same name in different scopes as it causes confusion for the programmer and can lead to unexpected results.

In your case just drop the long definition in the two lines highlighted as that is what causes a new variable with the same name to be used at the inner scope.