A pin which i don't use works, and i dont know why

i am an Arduino Leonardo user and beginner.

i am using a button and connect it to pin A2
when i click the button, LED works

problem is that
even though i connect the button to other analog pin (for example a3 a4 or a5...) and click the button,
LED works. it must not work
in my code, i don't declare other analog pins. i am using only pin A2

really thank you for reading my question

const int buttonPin = A2; // the number of the pushbutton pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
long lastDebounceTime = 0; // the last time the output pin was toggled

const int ledPin = 5; // the number of the LED pin
int ledState = LOW; // the current state of the output pin
long debounceDelay = 30; // the debounce time; increase if the output flickers

void setup() {
pinMode(A2, INPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
}

int inc = 1;
int bright = 255;
int slideSensor;

void Show_Black() {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(9, LOW);
}

void Show_White() {

bright -= 5 * inc;
Serial.println(bright);
analogWrite(5, bright);
analogWrite(6, bright);
analogWrite(9, bright);
if (bright > 254 || bright < 6) {
ledState=LOW;
bright = 255;
}
if (bright == 5) {
delay(30);
} else {
delay(30);
}
}

void loop() {

int reading = digitalRead(buttonPin);

if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}

if(ledState == HIGH){
Show_White();
}
else {
Show_Black();
}
lastButtonState = reading;
}

Hi, and welcome to the forum.

ssd0023:
i am using a button and connect it to pin A2

To pin A2 and ground?
If so, did you use a pull up resistor.

You could also use the internal pull up resistor of the pin, like this:

pinMode(A2, INPUT_PULLUP);

Leo..

my button is analog touch sensor button.
and i heard INPUT_PULLUP is for digital input, so i dont know if it works or not
anyway, i m gonaa try that thx

You've declared ledState as an int.
What do you expect

ledState = !ledState;

to change it to? A bool would be much more appropriate.

if (bright == 5) {
delay(30);
} else {
delay(30);
}

seems rather pointless

INTP /
thank you for giving me a lot of things which i have to revise!!

ssd0023:
my button is analog touch sensor button.
and i heard INPUT_PULLUP is for digital input, so i dont know if it works or not
anyway, i m gonaa try that thx

Your code:

int reading = digitalRead(buttonPin);

So you use the pin as a digital input.

And yes, PULLUP also works on analogue pins.

What do you mean with "analogue touch sensor button".
Leo..

Maybe he means capacitive button module