Help Needed With Stepper Motor Code

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:

int sensorPin = 0;

int ledPin = 13;

#include <Stepper.h>

const int stepsPerRevolution = 50;

int dirPin = 8;

int stepPin = 9;

Stepper myStepper(stepsPerRevolution, dirPin, stepPin);

void setup()
{

pinMode(ledPin, OUTPUT);

myStepper.setSpeed(60);

pinMode(dirPin, OUTPUT);

pinMode(stepPin, OUTPUT);

digitalWrite(dirPin, LOW);

digitalWrite(stepPin, LOW);

Serial.begin(9600);
}

void loop()
{

int sensorValue = analogRead(sensorPin);

float voltage = sensorValue * (5.0 / 1023.0);

Serial.println(voltage);

if (voltage > 2.60)
{
myStepper.step(stepsPerRevolution);

delay(500);
}
else if (voltage < 2.40)
{
myStepper.step(-stepsPerRevolution);

delay(500);
}

digitalWrite(ledPin, HIGH);

delay(sensorValue);

digitalWrite(ledPin, LOW);

delay(sensorValue);

}

Do I just change the number of steps per revolution? Thank you for your time and assistance.

If your stepper steps in 1.8 degree increments, stepsPerRevolution should be 200

But when you tell the stepper to step, if you want 1.8 degrees, tell it to step 1, not 50.

 int sensorValue = analogRead(sensorPin);
 
  float voltage = sensorValue * (5.0 / 1023.0);
 
  Serial.println(voltage);
 
  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?

PaulS:

 int sensorValue = analogRead(sensorPin);

float voltage = sensorValue * (5.0 / 1023.0);

Serial.println(voltage);

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?)

d probably change tho 2.60 and 2.40 to const floats

Better still to do the floating point calculations offline at compile time, and keep the constants integers.