How can a wire with no voltage provide input?

So to begin I am working on this project where I use two stepper motors that home themselves at the beginning of the program. It seemed to be working great but then I had a wire plugged in, with no other connection on the other side, but for some reason, it activated the input. I did more testing and found out that just by plugging in the wire it activated the input, for both of the pins I was using. I honestly have no idea why this is happening, I’ve tried several boards and nothing seems to work. I have an Atmega 2560 and here is the code thanks.

int SM1_1 = 22;
int SM1_2 = 24;
int SM1_3 = 26;
int SM1_4 = 28;

int SM2_1 = 23;
int SM2_2 = 25;
int SM2_3 = 27;
int SM2_4 = 29;

int LS = 51;

int button = 52;

int limitState;
int buttonState;
int stepCount;

int stepVar = 1000;

void setup() {
pinMode(SM1_1, OUTPUT);
pinMode(SM1_2, OUTPUT);
pinMode(SM1_3, OUTPUT);
pinMode(SM1_4, OUTPUT);

pinMode(SM2_1, OUTPUT);
pinMode(SM2_2, OUTPUT);
pinMode(SM2_3, OUTPUT);
pinMode(SM2_4, OUTPUT);

pinMode(LS, INPUT);

pinMode(button, INPUT);

}

void loop() {
limitState = digitalRead(LS);
buttonState = digitalRead(button);

while(limitState == 0) {
digitalWrite(SM1_1, LOW);
digitalWrite(SM1_2, LOW);
digitalWrite(SM1_3, LOW);
digitalWrite(SM1_4, HIGH);

digitalWrite(SM2_1, LOW);
digitalWrite(SM2_2, LOW);
digitalWrite(SM2_3, LOW);
digitalWrite(SM2_4, HIGH);

delay(5);

digitalWrite(SM1_1, LOW);
digitalWrite(SM1_2, LOW);
digitalWrite(SM1_3, HIGH);
digitalWrite(SM1_4, LOW);

digitalWrite(SM2_1, LOW);
digitalWrite(SM2_2, LOW);
digitalWrite(SM2_3, HIGH);
digitalWrite(SM2_4, LOW);

delay(5);

digitalWrite(SM1_1, LOW);
digitalWrite(SM1_2, HIGH);
digitalWrite(SM1_3, LOW);
digitalWrite(SM1_4, LOW);

digitalWrite(SM2_1, LOW);
digitalWrite(SM2_2, HIGH);
digitalWrite(SM2_3, LOW);
digitalWrite(SM2_4, LOW);

delay(5);

digitalWrite(SM1_1, HIGH);
digitalWrite(SM1_2, LOW);
digitalWrite(SM1_3, LOW);
digitalWrite(SM1_4, LOW);

digitalWrite(SM2_1, HIGH);
digitalWrite(SM2_2, LOW);
digitalWrite(SM2_3, LOW);
digitalWrite(SM2_4, LOW);

delay(5);

limitState = digitalRead(LS);
}

delay(2000);
while (buttonState == 0) {
delay(5);
buttonState = digitalRead(button);
}
if (buttonState == 1) {
for(int i; i < stepVar; i++) {
digitalWrite(SM1_1, HIGH);
digitalWrite(SM1_2, LOW);
digitalWrite(SM1_3, LOW);
digitalWrite(SM1_4, LOW);

digitalWrite(SM2_1, HIGH);
digitalWrite(SM2_2, LOW);
digitalWrite(SM2_3, LOW);
digitalWrite(SM2_4, LOW);

delay(5);

digitalWrite(SM1_1, LOW);
digitalWrite(SM1_2, HIGH);
digitalWrite(SM1_3, LOW);
digitalWrite(SM1_4, LOW);

digitalWrite(SM2_1, LOW);
digitalWrite(SM2_2, HIGH);
digitalWrite(SM2_3, LOW);
digitalWrite(SM2_4, LOW);

delay(5);

digitalWrite(SM1_1, LOW);
digitalWrite(SM1_2, LOW);
digitalWrite(SM1_3, HIGH);
digitalWrite(SM1_4, LOW);

digitalWrite(SM2_1, LOW);
digitalWrite(SM2_2, LOW);
digitalWrite(SM2_3, HIGH);
digitalWrite(SM2_4, LOW);

delay(5);

digitalWrite(SM1_1, LOW);
digitalWrite(SM1_2, LOW);
digitalWrite(SM1_3, LOW);
digitalWrite(SM1_4, HIGH);

digitalWrite(SM2_1, LOW);
digitalWrite(SM2_2, LOW);
digitalWrite(SM2_3, LOW);
digitalWrite(SM2_4, HIGH);

delay(5);
}
}
}

Your input pin is floating. This means it can easily switch to HIGH or LOW. The wire acts as an antenna, picking up electrical noise from the environment. This is why we use pull-up or pull-down resistors on input pins, so that they will be held in a known state when the circuit to the pin is otherwise open. The ATmega2560 has built-in pull-up resistors. You can enable them by setting the pin mode to INPUT_PULLUP:

pinMode(button, INPUT_PULLUP);

this will cause the state of the button pin to be HIGH unless it is connected to ground, in which case it will be LOW.

More information:

I have just tried this however it seemed to work but about half a second after it stopped, it started to spin again with the button still being a HIGH state.

Isn't that what your code is written to do?

A concept that some people have trouble with at first when using pull-up resistors and buttons is that you wire the button between the pin and ground. So when the button is open, the pin is HIGH and when the button is pressed, the pin is LOW. Beginners tend to assume that a pressed button should result in a HIGH state, but really HIGH and LOW are just arbitrary pin states. You can write your code so that HIGH or LOW mean anything you like.

By the way, I definitely recommend that you use the nice HIGH and LOW pin state names in your code instead of 1 and 0. This makes your code easier to understand. Better is to do something like this:

const byte buttonPressedState = LOW;

Then:

if (buttonState == buttonPressedState) {

Do you see how that makes your code easy to understand, without even adding any comments? And if you ever changed the wiring of your button so that the pressed state was HIGH, you could just change a single line of code to adjust to the new configuration.