A button press counter with Analog keypad - How to?

The code below works well for digital signals (when modified for HIGH/LOW) but how to make switch presses with an analogRead value between 212-215?

Trying to debounce an analog switch I guess!

sensorVal = analogRead(sensorPin);

 if (sensorVal >= 212 && sensorVal <= 215)
  {
    MSstate = sensorVal;
    if(MSstate != MSlastState)
    {
      if (MSstate >= 212 && MSstate <= 215)
      {
        MSbuttonCounter++;
      }
      MSlastState = MSstate;
    }
    if (MSbuttonCounter == 0)
    {
      digitalWrite(MS1Pin, LOW);
      digitalWrite(MS2Pin, LOW);
      MSmenuFunction("FULL STEP");
    }
    if (MSbuttonCounter == 1)
    {
      digitalWrite(MS1Pin, LOW);
      digitalWrite(MS2Pin, HIGH);
      MSmenuFunction("1/2 STEP ");
    }
    if (MSbuttonCounter == 2)
    {
      digitalWrite(MS1Pin, HIGH);
      digitalWrite(MS2Pin, LOW);
      MSmenuFunction("1/4 STEP ");
    }
    if (MSbuttonCounter == 3)
    {
      digitalWrite(MS1Pin, HIGH);
      digitalWrite(MS2Pin, HIGH);
      MSmenuFunction("1/8 STEP ");
    }
    if(MSbuttonCounter >= 4)
    {
      MSbuttonCounter = 0;
      delay(20);
    }
  }
sensorVal = analogRead(sensorPin);

 if (sensorVal >= 212 && sensorVal <= 215)
  {
    MSstate = sensorVal;
    if(MSstate != MSlastState)
    {
      if (MSstate >= 212 && MSstate <= 215)

Why bother checking the range of “MSstate” when you already checked it when it was called “sensorVal” ?
Or have I missed something subtle?

Think I need it for the variable names chosen?

    if (MSbuttonCounter == 0)
    {
    }
    if (MSbuttonCounter == 1)
    {
    }
    if (MSbuttonCounter == 2)
    {
    }
    if (MSbuttonCounter == 3)
    {
    }
    if(MSbuttonCounter >= 4)
    {
    }

Is there some value of MSbuttonCounter that could cause multiple if blocks to be executed? If not, why are you not using if/else if/else?

PaulS:

    if (MSbuttonCounter == 0)

{
    }
    if (MSbuttonCounter == 1)
    {
    }
    if (MSbuttonCounter == 2)
    {
    }
    if (MSbuttonCounter == 3)
    {
    }
    if(MSbuttonCounter >= 4)
    {
    }



Is there some value of MSbuttonCounter that could cause multiple if blocks to be executed? If not, why are you not using if/else if/else?

Nope but I am doing so now. Code modified however my issue seems to be I am tying sensorVal to the exact analogRead value when I need a range.

  if (sensorVal >= 212 && sensorVal <= 215)
  {
    MSstate = sensorVal;
    if(sensorVal != MSlastState)
    {
      MSbuttonCounter++;
      MSlastState = sensorVal;
    }
    if (MSbuttonCounter == 0)
    {
      digitalWrite(MS1Pin, LOW);
      digitalWrite(MS2Pin, LOW);
      MSmenuFunction("FULL STEP");
    }
    else if (MSbuttonCounter == 1)
    {
      digitalWrite(MS1Pin, LOW);
      digitalWrite(MS2Pin, HIGH);
      MSmenuFunction("1/2 STEP ");
    }
    else if (MSbuttonCounter == 2)
    {
      digitalWrite(MS1Pin, HIGH);
      digitalWrite(MS2Pin, LOW);
      MSmenuFunction("1/4 STEP ");
    }
    else if (MSbuttonCounter == 3)
    {
      digitalWrite(MS1Pin, HIGH);
      digitalWrite(MS2Pin, HIGH);
      MSmenuFunction("1/8 STEP ");
    }
    else if(MSbuttonCounter >= 4)
    {
      MSbuttonCounter = 0;
      delay(20);
    }
  }

So, you want to see if the current value is in some range, and if the previous value was in some range, not if the current value is in some range and is equal to the previous value.

Create a flag. Set it to true when the current value is in range, or false when it isn't. Copy that setting to the variable used to record the previous state, instead of the actual value.

If the current value is in range, and the previous one was, too, do something.

Excellent logical description PaulS, I'll work on it and post the code back when sorted. Thanks.

Ok, given it a few tries and my code just jumps to if MSbuttonCounter==3 and loops back to it.

Any pointers? Thanks.

  if (sensorVal >= 212 && sensorVal <= 215)
  {
    MSbuttonCounter++;
    if (MSlastState == DOWNstate)
    {
      if (MSbuttonCounter == 0)
      {
        digitalWrite(MS1Pin, LOW);
        digitalWrite(MS2Pin, LOW);
        MSmenuFunction("FULL STEP");
        DOWNstate = 1;
      }
      else if (MSbuttonCounter == 1)
      {
        digitalWrite(MS1Pin, LOW);
        digitalWrite(MS2Pin, HIGH);
        MSmenuFunction("1/2 STEP ");
        DOWNstate = 1;
      }
      else if (MSbuttonCounter == 2)
      {
        digitalWrite(MS1Pin, HIGH);
        digitalWrite(MS2Pin, LOW);
        MSmenuFunction("1/4 STEP ");
        DOWNstate = 1;
      }
      else if (MSbuttonCounter == 3)
      {
        digitalWrite(MS1Pin, HIGH);
        digitalWrite(MS2Pin, HIGH);
        MSmenuFunction("1/8 STEP ");
        DOWNstate = 1;
      }
    }
  }
  else 
  {
    if (MSbuttonCounter >= 4)
    {
      MSbuttonCounter = 0;
      delay(20);
      DOWNstate = 0;
    }
  }

Any pointers?

Use Serial.print() to see what is going on. What value is read from the sensor? What was/is MSbuttonCounter? What is MSlastState? Where is is (re)set?