//https://forum.arduino.cc/t/staying-in-a-subroutine/901037

# define SW 3 //define pin 3 as the select on rotary encoder

# define LED  4   // just an LED for showing

unsigned long lastButtonPress = 0; //records button press on rotary encoder

void setup() {
  pinMode(SW,INPUT_PULLUP); // setup rotary encoder SW as an input
  pinMode(LED, OUTPUT); // setup selector switch 
}

void menuSelect(){

  int btnState = digitalRead(SW); //read button state

  //if btnState LOW, then button is pressed
  if (btnState == LOW) {
    if (millis() - lastButtonPress > 500) {

      digitalWrite(LED, !digitalRead(LED)); // toggle the LED

    }
    lastButtonPress = millis(); // remember last button press
  }
}

void loop() {
    menuSelect();
}