Selector without delay or millis

Hello everybody.

I’m looking to make a selector 3 positions with 2 buttons, one Plus and one Minus.

The problem is that when i stay pushed on Plus, the selection continue to increase to the Max alone and when i stay pushed on Minus, the selection continue to decrease alone too.

In fact I would like to supress the delay(200); function in my program and make Plus Minus buttons work only one time when pushed.

int PLUS = 0;
int MINUS = 0;

int Pin=1;

void setup() {

 Serial.begin(9600);

 pinMode(3,INPUT);
 pinMode(2,INPUT);
 
}

void loop() {

  int StateP = digitalRead(2);
  int StateM = digitalRead(3);
  
  if (StateP+StateM == 0) { 
    PLUS=0;
    MINUS=0;
    }
    
  else {
      if ((StateP+StateM<2)) {
          if (StateP == 1) {
            PLUS=1;
            MINUS=0;
            delay(200);
          }
          if (StateM == 1) {
            PLUS=0;
            MINUS=1;
            delay(200);
          }
      }
    }



 


    if (PLUS == 1 && Pin == 2) {
      Pin = 2;
    }
    else if (MINUS == 1 && Pin == 0) {
      Pin = 0;
    }
    else {
        if (PLUS == 1) {
        Pin++;
        }

        else if (MINUS == 1) {
        Pin--;
        }
     }
 
  
  Serial.println(Pin);
 
}

You need to build into your program some code to check that the button is released before you act on it again. The usual way to do that is something like this

static byte previousStateP = 0;
byte StateP = digitalRead(2);
if (StateP != previousStateP and StateP == HIGH) {
   // do stuff
}
previousStateP = StateP;

...R

There is a state change detection example in the IDE.