brainy bits electric miter box - steps negativ direction with high input values

Hi,

I am testing an implementation of a project based on Brainy Bits "Electric Miter Box" found here:
https://brainy-bits.com/blogs/tutorials/diy-stepper-miter-box?comment=2747517379#comments

I am using identical hardware (Arduino Mega 2560, Easy Driver 3967, Nokia 5110 LCD, Thin Keypad).
I am using a screw jack instead of drive belts, so I changed the function: int calculatedmove=((z1600)/80); to int calculatedmove=((z1600)/8); ... as my screw jack moves 8 mm per revolution.

The problem is: when entering high values (varies based on above formulas), the stepper will move in a negative (reverse) direction when it should move forward. (Ex: If at 0 position, and entering 160, the stepper moves forward 160 mm. However, if at 0 position, and entering 170, the stepper moves backwards to -170 instead of forward as it should).

I can not find a problem with the code which would calculate a negative value based on too high a number, so I was wondering if it might be something built into one of the libraries.

Any help or suggestions leading me to a solution would be greatly appreciated.

Thanks,

Tony

What is the highest value of z? You are probably overflowing the capacity of an int type. In that code, any value of z over 20 will overflow an int because it will exceed 32767.

Hi aarg,
I didn't test the range between 161-169, but if I enter 160 for z, all is well. If I enter 170 for z instead, it will function in a negative direction.
So if z=160 and (z*1600/8 == 160 * 1600/8 == 256,000 / 8 = 32,000).
I will try changing int to long and retest and post.
Thx.

All is not well when a variable overflows. It is guaranteed for any value of z over 20. But that is not because of the type of z. It is because of the way it is multiplied. You could use:

int calculatedmove=((z*1600L)/80);

or

int calculatedmove=((z*1600L)/8);

Where z is an int type.

Hi aarg,

Please forgive my lack of programming prowess, but I don't understand the significance of placing the L next to the value 1600.
Can you educate me on this?
Tony

gufinov:
Hi aarg,

Please forgive my lack of programming prowess, but I don't understand the significance of placing the L next to the value 1600.
Can you educate me on this?
Tony

It specifies the constant 1600 as a long type, which forces the expression to be evaluated as a long type.

Well, I tried:

long calculatedmove=(z1600/8);
int calculatedmove=((z
1600L)/8);
int calculatedmove=((z*1600)/8);

This doesn't appear to be where the problem lies unless it correlates with some other part of the code. I still get the same problem.

I did run some more tests and found the threshold to be 0-163 (all is well), 164 > things get weird.

  1. Benching from 0, if I run a high number like 165, the stepper runs in reverse to a negative value (position) as stated (in this case -165mm), but if I follow up with an input of say 190, it moves forward the difference +25).
  2. Benching from 0, if I run a normal value of 163, it moves 163 mm forward. Followed by 164, it reverses and travels backward to position -164mm, a total of 327mm difference. This should have moved forward 1 mm.

scratching head.

Well, I tried:

long calculatedmove=(z1600/8);
int calculatedmove=((z
1600L)/8);
int calculatedmove=((z*1600)/8);

You missed

long calculatedmove=(z*1600L/8);

The runToNewPosition takes a long as a parameter, by the way.

Your right on all accounts. I did miss it :o and your deductions are spot on!

long calculatedmove=((z*1600L)/8); ======>

problem solved.

Thank you so very much.
Thank you both!