const int BTN_PIN = 12;
const int POT_PIN = A0;

int oldBtnState = HIGH;
int oldValue = 0;
int potVal = 0;

int checkButton()  {
  int btnState = digitalRead(BTN_PIN);
  if (btnState != oldBtnState)  {
    oldBtnState = btnState;
    if (btnState == LOW)  {
      potVal = analogRead(POT_PIN);
      //Serial.println(potVal);
    }
    delay(20);
  }
  return potVal;
}

void setup() {
  Serial.begin(115200);
  pinMode(BTN_PIN, INPUT_PULLUP);
  Serial.println("Press button to set level\n");
}

void loop() {
  int value = checkButton();
  if (value != oldValue)  {
    oldValue = value;
    Serial.println(value);
  }
}