Stepper moving in idle state

Hi

Below is the schematic of a pretty simple and straightforward project.

a) A 6-pole selector switch is connected to analog pin A0. Depending upon the values of the active resistors the analog value is selected at pin A0. This value is interpreted as number of STEPS to move the stepper motor.

b) D2 is connected to the output of an inductive proximity sensor thru an opto coupler. When the sensor gets on, an ISR function is called to rotate the stepper. This function is a simple for() loop which is repeated for the number of STEPS interpreted by the selector switch. (The stepper is 3-phase, 6A, 12Nm).

b) Two push buttons are attached to D2 and D4 using internal pullup resistors. FWD button moves 1 step in forward (anti clockwise) direction. REV button moves 1 step in reverse (clockwise) direction.

c) D6 & D8 issue direction & pulse signals.

The sketch works fine, as programmed. However, in idle state; sometimes the motor rotates at very slow speed. Sometimes (again in idle state) the value at A0 changes drastically such that it enters the previous or next size range.

After thorough checking, I found that if I disconnect REV switch connected to D3, the movement of stepper in idle state is stopped.

How should I resolve these two issues?

void setup() 
{
  Serial.begin(9600);
  pinMode(stripMoveSignalPin, INPUT_PULLUP);
  pinMode(pulsePin,          OUTPUT);
  pinMode(dirPin,            OUTPUT);
  pinMode(FWDpin,             INPUT);
  pinMode(REVpin,             INPUT);

  attachInterrupt(digitalPinToInterrupt(stripMoveSignalPin), moveStrip, FALLING);
}

void loop() 
{
  //####### SIZE SELECTION ##############
   selectorSw = analogRead(selectorPin);
   if (selectorSw < 50)  ///// Size 1-1/16" selected
  {
    stripLength = 116;
  }
  if (selectorSw > 160 && selectorSw < 240)  ///// Size 1-1/4" selected
  {
    stripLength = 130;
  }
  if (selectorSw > 350 && selectorSw < 480)  ///// Size 1-1/2" selected
  {
    stripLength = 150;
  }      
   if (selectorSw > 550 && selectorSw < 640)  ///// Size 1-3/4" selected
  {
    stripLength = 170;
  }    
   if (selectorSw > 660 && selectorSw < 750)  ///// Size 2" selected
  {
    stripLength = 188;
  }      
  if (selectorSw > 780 && selectorSw < 880)  ///// Size 2-1/4" selected
  {
    stripLength = 211;
  }
  Steps = ( 7.0 * stepsPerRev * stripLength ) / (22.0 * rollerDia);
  steps = round(Steps);
  Serial.print(stripLength);
  Serial.println(steps);

  //###### STRIP SETTING ##########################
  reading = digitalRead(FWDpin);
  debounce();
  if (reading == HIGH)  ///// FWD button pressed
  {
    digitalWrite(dirPin, !Clockwise);  // Change back to forward direction
    jogMoveStrip();
  }

  reading = digitalRead(REVpin);
  debounce();
  if (reading == HIGH)  ///// REV button pressed
  {
    digitalWrite(dirPin, Clockwise);   // Change direction to reverse
    jogMoveStrip();                    // Jog move the Strip
  }
}

========================================
void jogMoveStrip()
{
  digitalWrite(pulsePin, HIGH);
  delay(10);
  digitalWrite(pulsePin, LOW);
}/////////////////////////////////////////////////////////

void moveStrip()
{
  digitalWrite(dirPin, !Clockwise); //////// FORWARD DIRECTION
  for(int x = 0; x < steps; x++) 
  {
    digitalWrite(pulsePin, HIGH); 
    delayMicroseconds(pulseDelay); 
    digitalWrite(pulsePin, LOW); 
    delayMicroseconds(pulseDelay); 
  }
}//////////////////////////////////////////


//##################### Debounce ##############
void debounce()
{
  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  if (reading != lastButtonState) // if selectorSw status changed pressed or noise
  {
    lastDebounceTime = millis(); // reset the debouncing timer
  }

  // if Longer than the debounce delay, selectorSw really pressed
  if ((millis() - lastDebounceTime) > debounceDelay) 
  {
    if (reading != buttonState) 
    {
      buttonState = reading;
    }
  }
  lastButtonState = reading;
} ////////////////////////////////////////////////////////// 

I would put a 0.1uF cap from A0 to ground to bypass noise to ground.

Are the wires from the FWD and REV switches long (several inches)? Maybe stronger (lower value) pullup resistors and caps across those switches, too.

Thank you for your input.

a) The cap should be polarized?
b) Yes, the wires are several feet long. Would a 1K pull-up resistor be strong enough?

The 0.1uF (100nF) caps are ceramic (sorry for not specifying that) and so are not polarized.
0.1uF caps are marked 104.

I would try 4.7K first and then 2K. 1K if required but you should not need that low.