Help with code

Hello I am new to arduino and programming and I am having trouble with some code. The goal is when I press and hold the button the two LED will blink at different times going back and forth and if the button is not press it will remain off. However when I run the code the LED's turn on by themselves without me pressing the button and runs as though I was. When I press the button the LED's stop blinking and stay on. I am not sure what I did wrong can anyone give me some advice.

Here is the code

const int LY=9;
const int LR=10;
const int BUTTON=11;
const int fade=10;
int ButtonState =0;
void setup() {
pinMode(LY, OUTPUT);
pinMode(LR, OUTPUT);
pinMode(BUTTON, INPUT);

}

void loop() {

ButtonState = digitalRead(BUTTON);
if(ButtonState == HIGH)
{

digitalWrite(LY, LOW);
delay(100);
digitalWrite(LY, HIGH);
delay(fade);
digitalWrite(LR, LOW);
delay(100);
digitalWrite(LR, HIGH);
delay(fade);
}

else if(ButtonState == LOW)
{
digitalWrite(LY, LOW);
digitalWrite(LR, LOW);

}

}

Unless you have an external pull-down resistor on your button pin it is probably floating and giving random values.

The easier solution if to use the internal pullup resistor and wire the switch to pull the pin to GND when pressed. The it will read LOW when pressed.

pinMode(buttonPin, INPUT_PULLUP);

...R

Thank you I will give that a try.

It worked thank you very much.