I'm running a NEMA 17 stepper motor with an EasyDriver, which is also hooked up to a potentiometer controlling 5V. The voltage is then read in pin A0. When the voltage is below 2.4, my stepper motor moves counterclockwise and if it's over 2.6V, it moves clockwise. The problem that I am having is how to program the stepper motor to move as little as possible in each direction per step. The spec says that it has a step angle of 1.8 degrees. How can I program it to move 1.8 degrees per step? I'm sure it's something very simple, but I just can't seem to get it. Here is the code:
if (voltage > 2.60)
{
myStepper.step(stepsPerRevolution);
delay(500);
}
else if (voltage < 2.40)
{
myStepper.step(-stepsPerRevolution);
delay(500);
}
Is there some reason not to use the raw output of analogRead() in the if statements? What advantage is obtained by multiplying and dividing?
Code readability? IMHO it's much easier to remember the actual target voltages than the ADC counts. That said, there is still some room for optimization here. I'd probably change tho 2.60 and 2.40 to const floats (or #defines) to make threshold changing easier. If using const float I'd probably do the floating point math conversion when declaring the constant so the expensive floating point math is only done once instead of every iteration.
(Are literal floats used as implicit values for opcodes, or are they converted to variables at compile time? i.e. would #define for floats actually provide a SRAM saving?)