 # Am I missing a conversion?

Below you will see a small segment of code, the part in question is 'intervalX'. If I replace it in the second line with a number, it works flawlessly. 'speedX' is a previous calculation, that is a proportional equation based on 2 long() values. I've been staring at this for way too long, and figure I am missing something simple..... but my eyes just can't see it. I'd appreciate the help!

``````int intervalX = (int)speedX;

if(currentMillisX - previousMillisX > intervalX){ previousMillisX = currentMillisX;
if(stepPinstateX == LOW){ digitalWrite(stepPinX, HIGH); stepPinstateX = HIGH; positionX++;}
else{ digitalWrite(stepPinX, LOW); stepPinstateX = LOW;}}
if(positionX >= (X)){ statusX = 2;}} else{ runX = LOW;}}
``````

What number are you using to make it work? What is the value of speedX if you print it out? What is the problem - I assume it's not executing the code inside the time check?

Your indentation is making my eyes bleed, and I can’t line up your braces.

The number is at the lowest 22, it is a MicroSecond delay and that is the fastest the Stepper will allow. There is no cap, it is the small number that is most important.

As for the line breaks, I apologize; I recently did that to make it easier to browse through all the code, as there is tons. Here is a rewrite:

``````int intervalX = (int)speedX;

if(currentMillisX - previousMillisX > intervalX)
{
previousMillisX = currentMillisX;
if(stepPinstateX == LOW)
{
digitalWrite(stepPinX, HIGH);
stepPinstateX = HIGH;
positionX++;
}
else
{
digitalWrite(stepPinX, LOW);
stepPinstateX = LOW;
}
}
if(positionX >= (X))
{
statusX = 2;
}
}
else
{
runX = LOW;
}
}
``````

Sorry, I left a response out. If I do a Serial.println() for it outside the statement it sees it just fine, just inside the if it will not work. That is why I was wondering if I need to convert it to a different type of value.

If I do a Serial.println() for it outside the statement it sees it just fine, just inside the if it will not work.

What the heck is "it"?

Post all of your code. We can't see what speedX is (or contains). We can't see that currentMillisX or previosMillisX are defined correctly.

It still isn't clear what your problem is.

Below you will see a small segment of code, the part in question is 'intervalX'.

That is "it".

The rest of the code is irrelevant, and hundreds of lines of code; this is just one small piece. I divide 2 longs to create proportional speed value: speedX, and then I try to turn speedX into that intervalX in which I run my example code. The code works flawlessly if I replace intervalX with an arbitrary number.

For example, the top speed of the system is 22, this segment incorporates the speed. If I do a Serial.print of intervalX, I see (example) 50, however, the motor spins as if it were 22 (the top speed). Now, if I type in 50 instead of intervalX in that same spot, the motor works at the speed of 50. It can print the correct value, but it fails inside that if statement. So again, my question is: do I need to convert my value into a different format so that it works inside that if?

The rest of the code is irrelevant

In your opinion.

I divide 2 longs to create proportional speed value: speedX, and then I try to turn speedX into that intervalX in which I run my example code.

And the values of those two longs is? The resulting value in speedX is? The value in intervalX is? The values and types of currentMillisX and previosMillisX are?

do I need to convert my value into a different format so that it works inside that if?

unsigned long would be a better choice to store the value in. The cast is not required.