# Help with a 'long' translation please

I have a CNC type machine, where I take a Serial bytes and transfer them into the necessary stepper motor steps. The code is pretty huge, so to spare you all the details, the way the code is setup, is to individually read each point like:

``````if(Serial.available() == 13) //13 points of data
//etc
``````

And then basically I calculate: x = (A*10000)+(B*1000)+(C*100)+(D*10)+E

The problem I am seeing is that anything over 33,000 my data becomes incorrect. I have tried setting up my 'x' as shown above as:

unsigned long x = (A*10000)+(B*1000)+(C*100)+(D*10)+E;

as well as:

long x = long(A*10000)+(B*1000)+(C*100)+(D*10)+E;

But it still is having the same issue. I know this is a simple issue and I feel silly bring this here, but I can't seem to figure out what I have missed. I can find the issue, I just need some slight direction. Thanks in advance.

Not 33000, but anything over 32767 Try putting UL at the end of your constants

So like: unsigned long x UL =.... ?

I've never done this coding before. Thanks for your help, btw.

You're also doing it the "hard" way. If you have a string of integers you can use a library function to convert them to an integer:

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#gaea44aa48bda8261f794dcb2d1e7ab2b2

Like "10000UL", "1000UL" etc

So like: unsigned long x UL =.... ?

No, more like:

``````unsigned long x = (A*10000UL)+(B*1000U)+(C*100U)+(D*10U)+E;
``````

Only the '10000' really needs to be of long type.

-- The Ruggeduino: compatible with Arduino UNO, 24V operation, all I/O's fused and protected

OK, so next question. I did the unsigned long steps1long = (A*1000UL) etc…

This below is the code that drives the motor, and this no longer works. ‘progress1’ is listed as a long before the setup function, is that incorrect? Again, I greatly appreciate your help!

``````if(run1 == HIGH)
{
digitalWrite(sleepPin1, HIGH);
digitalWrite(sleepPin3, LOW);
if(progress1 < steps1long)
{
unsigned long currentMillis1 = micros();

if(currentMillis1 - previousMillis1 > (interval1 + acceleration))
{
previousMillis1 = currentMillis1;
progress1++;
if(acceleration > 0)
{
acceleration--;
}
if(stepPinstate1 == LOW)
{
digitalWrite(stepPin1, HIGH);
stepPinstate1 = HIGH;
}
else
{
digitalWrite(stepPin1, LOW);
stepPinstate1 = LOW;
}
}
if((progress1 > (steps1long - 2000)) && (once1 == 1))
{
status1 = 2;
once1 = 2;
}
}
else
{
if(status1 != 2)
{
status1 = 2;
}
run1 = LOW;
}
}
else
{
progress1 = 0;
once1 = 0;
digitalWrite(sleepPin1, LOW);
}
``````

If you set a variable to zero as an accumulator then read your digits as they come in, each time; accum = accum * 10UL + digit You can have the value with a lot less code to a variable number of digits.

And yes, I've already posted working code example of that twice now.

From a quick glance at the code it looks as if there's scope to make this application a lot shorter - and less code usually means less bugs. I didn't look at the whole thing, but this line snagged my attention:

``````      unsigned long currentMillis1 = micros();
``````

It's a good idea to give variable a meangingful name that indicates the type and purpose of the data they hold. In this case I'm not sure whether the name is misleading, or you have a bug. In any case, when I see numbered variables currentMillis1, currentMillis2 etc it makes me suspect the author hasn't bothered to think about what they're for and give them a sensible name.