# Does this bit of code take a considerably long time to be calculated?

So I am building a camera slider that uses two stepper motors. Those steppers have some gearing and I need to calculate some values only once. So I moved all those calculations into the call below. If I put these calculations into the loop, the steppers move awefully slow. I suspect because the loop time got a whole lot longer.

So the question is: Do the calculations in the code below require a lot of processing power from the Arduino Mega? Is there a way to estimate how much the loop would get longer if it has to do these calculations?

I solved the problem now by only calling the Standard Calculations () once, but it would be interesting to know why they take so long.

``````void StandardCalculations() //Call this once when the process starts.
{
xDistanceInSteps = (int)(xDistance / XmmPerStep); //calculates the number of steps required to move from one x position to the next given the xDistance set by the user.
NumberOfAngles = (90 / (SmallAngle * 0.1)); //checked it, it is correct. /10 because the input is 14° instead of 1.4°
ArcLengthPerAngle = (((SmallAngle * 0.1) / 360) * 2 * 3.141 * SliderLength * 10); // This comes from the arc length formula in degrees. The times 10 comes from cm to mm for the slider length.
Serial.print("Arc Length Per Angle = ");
Serial.println(ArcLengthPerAngle);
SliderEndPosition = ((SliderLength * 10) / XmmPerStep);
Serial.print("SliderEndPosition = ");
Serial.println(SliderEndPosition);
Serial.print("Number of Angles = ");
Serial.println(NumberOfAngles);
}
``````

Floating point math is expensive on an Uno and I expect that the same is true of a Mega. So if you were calculating floating point math on every step of the stepper, it might cause some lag although since you say that the calc is constant I would have hoped that the compiler would have optimized it out.

Another consideration is your serial prints - what baud rate are you using.

define "too long"... what you could do to find out how long it is:

``````void StandardCalculations() //Call this once when the process starts.
{
// -----------------------
// take note of time when entering the function
unsigned long t0, t1;
t0 = micros();
// -----------------------

xDistanceInSteps = (int)(xDistance / XmmPerStep); //calculates the number of steps required to move from one x position to the next given the xDistance set by the user.
NumberOfAngles = (90 / (SmallAngle * 0.1)); //checked it, it is correct. /10 because the input is 14° instead of 1.4°
ArcLengthPerAngle = (((SmallAngle * 0.1) / 360) * 2 * 3.141 * SliderLength * 10); // This comes from the arc length formula in degrees. The times 10 comes from cm to mm for the slider length.

// -----------------------
// take note of time at the end of the maths and print the duration
t1 = micros();
Serial.print(F("Calc Duration = ")); Serial.print(t1-t0); Serial.println(F(" micro seconds"));
// -----------------------

Serial.print("Arc Length Per Angle = ");
Serial.println(ArcLengthPerAngle);
SliderEndPosition = ((SliderLength * 10) / XmmPerStep);
Serial.print("SliderEndPosition = ");
Serial.println(SliderEndPosition);
Serial.print("Number of Angles = ");
Serial.println(NumberOfAngles);
}
``````

For ballpark calculation, a float add takes about 10us, multiply 8us and divide 80us. With your code, if assume all your calcs are float,

xDistanceInSteps = (int)(xDistance / XmmPerStep); = 80us

NumberOfAngles = (90 / (SmallAngle * 0.1)); will LIKELY get pre-procesed to 900/ DmallAngle = 80us

ArcLengthPerAngle = (((SmallAngle * 0.1) / 360) * 2 * 3.141 * SliderLength * 10); As above LIKELY to become SmallAngle * SliderLength * 0.01745 = 16us

SliderEndPosition = ((SliderLength * 10) / XmmPerStep); = 88us

Total processor time doing math = about 264us.

Serial.print("Arc Length Per Angle = ");
Serial.println(ArcLengthPerAngle);

Serial.print("SliderEndPosition = ");

Serial.println(SliderEndPosition);
Serial.print("Number of Angles = ");
Serial.println(NumberOfAngles);

Total time printing 1 character at 9600 baud = about 1ms.

Total time printing 1 character at 9600 baud = about 1ms.

unless the buffer is full, the print commands returns immediately and printing happens asynchronously through interrupts - so I would not count that as part as such

but going to 115200 or more can't hurt :)

Thank you all!