Math bug

Hi all, I was bug testing a small program I've been working on and have found a bug that I was hoping someone could help with. The program is in very early development but it's basically a stop and start camera slider. The problem I'm having is that when I adjust the stepper motor range (the total amount of steps for one full slide) the steps per movement (number of times the stepper motor has to move between pauses) I get negative values. I initially started creating the program with a random stepper range of 6000 as I don't have the stepper motor yet nor do I have a track set up so thought 6000 would do temporarily and all is working fine. The problem occurs when I tested the range at 60000 which although a very high number should not make a huge change to the math, I also tried simply doubling the range to 12000 which works fine. I haven't tested any other values but I shouldn't need to as it should work the same no matter what value I use according to the formula I came up with.

You are using a 16-bit integer which can only represent numbers in the range -32768 to +32767. If you exceed 32767 strange things happen as you've discovered.
Use a bigger integer. For example;

unsigned long range;

Pete

Hi all I'm making a simple move shoot move camera slider, I started the basic coding or at least what I can do right now without having the stepper motor and slider and all is working well. However I thought I would play with the stepper range (the amount of steps needed to move to do one full slide of the slider) value just to test for any bugs and unfortunately found one. Wheh I started coding the project I used a placeholder value for the stepper range as I have not bought the motor yet, the value I used was 6000. I tried some differnent values such as 12000 which works fine also however when I put in the value of 60000 the value calculated for the number of steps to be made between pauses reads negotive. This shouldn't be happeneing as I'm pretty sure the maths I came up with to calculate this is sound and it works fine in the calculator just for some reason the arduino doesn't like the number.

Any help would be greatly apprecieted unfortunately I can't just dump the whole code in as it exceeds the word cound but here is the line that is most likely to be the source of the problem If you need any further clarification please feel free to ask as it is quite difficult to explain the problem.

I don't believe it to be a problem with my code rather something arduino doesn't like and before anyone asks yes I changed the (%) value whilst debugging.

int Step_range = 6000;//this is a global variable in my code 

NoStepsMoto = (Step_range/NoSteps)%6000;

moderategamer:
Any help would be greatly apprecieted unfortunately I can't just dump the whole code in as it exceeds the word cound but here is the line that is most likely to be the source of the problem If you need any further clarification please feel free to ask as it is quite difficult to explain the problem.

what is the largest 16bit signed integer (i.e. an int on arduino)?

BulldogLowell:
what is the largest 16bit signed integer (i.e. an int on arduino)?

Oh never thought about that but wouldn't that be 65535 or at least that's what the internet tells me so should still work as that's the highest binary number that can be made with 16 bits and I fall below that no? please correct me if i'm wrong.

Time for some reading:
See "Defining Data Types"
https://learn.sparkfun.com/tutorials/data-types-in-arduino

.

65535 is an unsigned int.
signed ints go from -32768 to 32767

int yourNumber; // results in a signed int

CrossRoads:
65535 is an unsigned int.
signed ints go from -32768 to 32767

int yourNumber; // results in a signed int

Thanks for the reply very helpful.

el_supremo:
You are using a 16-bit integer which can only represent numbers in the range -32768 to +32767. If you exceed 32767 strange things happen as you've discovered.
Use a bigger integer. For example;

unsigned long range;

Pete

Thanks for the reply very helpful I never thought about that makes sense now. Also you may notice that I accidentally posted this Topic twice please ignore that it was a mistake I didn't realise that this had posted as I accidentally hit a button and thought I had lost everything I typed turns out it posted.

Do not double post.

Do not double post.

Threads merged.

jremington:
Do not double post.

jremington:
Do not double post.

There must be some irony here no? after the merge looks like you double posted. Anyway I apologised it was an honest mistake my finger slipped and must have accidentally posted the thread I thought I had just lost it but turns out I was wrong.

Thanks for that apologies once again.