Stepper Motor turns on power-up

So I got a fairly simple problem, and an idea for a fix. I will post the code at the bottom.

Currently, my arduino is reading a digital switch position, then turning a motor based on what it reads… so 200 steps one direction high and 200 in the opposite direction if it is low as an example. This part works. The issue I have is that if the arduino loses power, when it is turned back on, the stepper motor moves the 200 steps or not depending on the current switch position. Is this because in the setup I set a value for the switch position variable? If I left it as as just an int variable, instead of setting it to 0 would that stop my problem? if not, other ideas?

#include <Stepper.h>
#define STEPS 200 //number of steps in motor revolution
int dirpin = 2;
int steppin = 3;
int signal = 4;
int digitalValue = 0; //if this is left blank for value, does this prevent the reset in value aftr power cycle?
int prevdigitalValue = 0;

void setup()
{
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(signal, INPUT);
Serial.begin(9600);
}
void loop()
{

digitalValue = digitalRead(signal);

Serial.println(digitalValue, DEC);
if (digitalValue != prevdigitalValue)
{
prevdigitalValue=digitalValue;
if (digitalValue==0)
{ rotate(-3200, .5); } // “Rising Edge” so the easydriver knows to when to step.

else if (digitalValue==1)
{ rotate(3200, .5); }// “Rising Edge” so the easydriver knows to when to step.

}
}

void rotate(int steps, float speed){
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//speed is any number from .1 → .5 with .5 being fastest - Slower is stronger
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);

digitalWrite(dirpin,dir);

float usDelay = (1/speed) * 250;

for(int i=0; i < steps; i++){
digitalWrite(steppin, HIGH);
delayMicroseconds(usDelay);

digitalWrite(steppin, LOW);
delayMicroseconds(usDelay);
}
}

Hey im testing this code right now. Im not sure I can help with saving the number of steps on reset.
But have you thought about using a potentiometer to set the speed as a variable for this?

If you have a switch that toggles between on and off (i.e. that holds its position) you will need to think how you align your Arduino with the position of the motor and the switch at startup. For example if the switch and motor are "left" how does the Arduino know that?

A momentary contact push button may solve the problem because it is normally open and the Arduino can be programmed only to do something if the button is pressed. The assumption would be to move the motor "left" if it is already "right" etc. But you still have the problem that the Arduino doesn't know which way the motor is. That may not matter.

If the Arduino needs to detect the position of the motor one way may be to include some detector switches (such as microswitches) that are moved by the motor when it gets to the end of its travel. The Arduino could read them to determine where the motor is.

...R

Hi TP

Ive been trying a few things with this and the below may be of use to both of us if we can refine it

#include <Stepper.h>
#define STEPS 200 //number of steps in motor revolution
int dirpin = 2;
int steppin = 3;
int signal = 4;
int digitalValue = 0; //if this is left blank for value, does this prevent the reset in value aftr power cycle?
int prevdigitalValue = 0;
int sensorReading = A0;    // select the input pin for the potentiometer
int ledPin = 13; // choose the pin for the LED
int inPin = 5;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status



////////////////// USEage //////////////
/* Motor is stopped on power up
USE pot to set speed - map these values
USE swith for direction
Press push button to start routine based on Steps and Speed
*/


void setup() 
{
  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);
  pinMode(signal, INPUT);
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
  Serial.begin(9600);
}

void loop(){
checkBTN();
}



void checkBTN(){
  val = digitalRead(inPin);  // read input value
  Serial.println(val);
  if (val == LOW) {         // check if the input is LOW (button not pressed)
        digitalWrite(ledPin, HIGH);  // turn LED OFF
        //stopMotor(); // make a function that stops motor here, or just reverse LOW HIGH here?
        //////////////////// MOTOR is stopped he because digital value is not called

        } else {
          ///// BUTTON pressed gets the digitalValue and speed and starts motor
          digitalWrite(ledPin, LOW);  // turn LED OFF and run motor
          digitalValue = digitalRead(signal);
      
                  Serial.println(digitalValue, DEC);
                  if (digitalValue != prevdigitalValue)
                  {
                      prevdigitalValue=digitalValue;
                    if (digitalValue==0)
                    { rotate(-1600, .5); } // "Rising Edge" so the easydriver knows to when to step.
                    
                    else if (digitalValue==1)
                    {  rotate(1600, .5); }// "Rising Edge" so the easydriver knows to when to step.
                  }   
              }
}


void rotate(int steps, float speed){ 
  //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
  //speed is any number from .1 -> .5 with .5 being fastest - Slower is stronger
  int dir = (steps > 0)? HIGH:LOW;
  steps = abs(steps);

  digitalWrite(dirpin,dir); 
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int usDelay = map(sensorReading, 0, 1023, 0, 100);
  //float usDelay = (1/speed) * 250;
  Serial.println(usDelay);
  for(int i=0; i < steps; i++){ 
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(usDelay); 

    digitalWrite(steppin, LOW); 
    delayMicroseconds(usDelay); 
  } 
}