Coding Question - #definte StepsPerRevolution

Greetings All …

I’m attempting to adapt a sketch for educational purposes. And I’m running into a limit I can’t understand.

The code starts like this:

#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 6400
void setup() {
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
// Set the spinning direction clockwise:
digitalWrite(dirPin, HIGH);
// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < 5 * stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(16000);
digitalWrite(stepPin, LOW);
delayMicroseconds(16000);
}
delay(1000);

This works fine. The stepper motor runs very slowly for five full revolutions, then reverses direction.

What I can’t understand is why the program won’t do anything if I increase the factor to 6 instead of 5.

If I replace 5 * stepsPerRevolution with 6 * stepsPerRevolution, the motor doesn’t budge.

If I set it back to 5 and reload, all is well.

Sadly, 6 would get me to the end of the run.

Thoughts?

Sincerely …

Stephen McDowell

That code does not change direction. Post the proper, and the entire code.
For loop variable i is an integer. How large do You think it can get?

Hi …

The code works right up until I change a 5 to a 6.

Integers should be able to exceed 5. And at the moment, all I need is 6.

Sincerely …

Stephen McDowell

Try again. The for loop says 5 *...... What do You think thats ends up with?

Hi ...

I would think 5 * is five times the value of stepsPerRevolution, which works beautifully.

Change the 5 * to a 6 * and the motor doesn't move.

Sincerely ...

Stephen McDowell

No, it will not work. What do You think happends changing that 5 or 6 into, lets say, 32767, or even more?

zzsgm202:
Hi ...

I would think 5 * is five times the value of stepsPerRevolution, which works beautifully.

Change the 5 * to a 6 * and the motor doesn't move.

Sincerely ...

Stephen McDowell

if i is an integer............5stepsPerRevolution..... ==32000 integer max is exceeded @ 6stepsPerRevolution == 38400

Hi ...

Yeah, I just worked that out. I figured the number 32767 wasn't random. And I found this:

On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).

So, microstepping the way I am, my slider length is exactly too long to handle the maximum integer range.

Nice.

Sincerely ...

Stephen McDowell

zzsgm202:
So, microstepping the way I am, my slider length is exactly too long to handle the maximum integer range.

Nice.

Sincerely …

Stephen McDowell

HINT: Int is not the only data type available.

So, microstepping the way I am, my slider length is exactly too long to handle the maximum integer range.

Yes, but not to long for an 'unsigned long'

In order to help the compiler use the math needed I suggest this:
#define stepsPerRevolution 6400L The "L" tells that this is a "long" numerical.
Else, in some situations, the compiler has used integer math, loosing data, and then deposited the faulty calculation into a long-variable.

This was a construction I used to awoid compiler math error:

  goxyz(motorX, long(82L * X_steps_per_mm), xtransport_delay); // position for hole 2, X=75,Y=0

Sad to say I used #define X_steps_per_mm 400
Note the "long" in the code. long(82L * X_steps_per_mm)

I am missing a hint: Where is the cheapest gas station?
That is how I see this discussion.

I see it as where is a functioning gas station.

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