Button toggle

I'm trying to perform a simple light display that toggles on and off at the press of a button, but I can only get it to bang instead of toggle for some reason. I'd really appreciate if someone could help!

Here's the code:

const int ledPin1 = 6; // the number of the LED pin
const int ledPin2 = 7; // the number of the LED pin
const int ledPin3 = 8; // the number of the LED pin
const int ledPin4 = 9; // the number of the LED pin
const int ledPin5 = 10; // the number of the LED pin
const int ledPin6 = 11; // the number of the LED pin
const int buttonPin1 = 2; // the number of the button pin
const int buttonPin2 = 3; // the number of the button pin
const int onTime = 150; // amount of time the LED will be on
const int offTime = 150; // amount of time the LED will be off

const byte LED_OFFSET = 6;

int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
boolean play = false;

void setup() {
pinMode(ledPin1, OUTPUT); // initialize the LED pin as an output
pinMode(ledPin2, OUTPUT); // initialize the LED pin as an output
pinMode(ledPin3, OUTPUT); // initialize the LED pin as an output
pinMode(ledPin4, OUTPUT); // initialize the LED pin as an output
pinMode(ledPin5, OUTPUT); // initialize the LED pin as an output
pinMode(ledPin6, OUTPUT); // initialize the LED pin as an output

pinMode(buttonPin1, INPUT); // initialize the LED pin as an output
pinMode(buttonPin2, INPUT); // initialize the LED pin as an output

}

void loop(){
buttonState1 = digitalRead(buttonPin1); // read the state of the button bin
buttonState2 = digitalRead(buttonPin2); // read the state of the button bin

if (buttonState1 == LOW && play == false){
play = true;
}
else if (buttonState1 == LOW && play == true){
play = false;
}

//else if (buttonState2 == LOW){
//play = false;
//}

if (play == true){
for (byte i = 0; i < 6; i++){
digitalWrite(i + LED_OFFSET, HIGH); // turn on the LED
delay(onTime); // wait
digitalWrite(i + LED_OFFSET, LOW); // turn off the LED
delay(offTime); // wait
}
/*
digitalWrite(ledPin2, HIGH); // turn on the LED
delay(onTime); // wait
digitalWrite(ledPin2, LOW); // turn off the LED
delay(offTime); // wait
digitalWrite(ledPin3, HIGH); // turn on the LED
delay(onTime); // wait
digitalWrite(ledPin3, LOW); // turn off the LED
delay(offTime); // wait
digitalWrite(ledPin4, HIGH); // turn on the LED
delay(onTime); // wait
digitalWrite(ledPin4, LOW); // turn off the LED
delay(offTime); // wait
digitalWrite(ledPin5, HIGH); // turn on the LED
delay(onTime); // wait
digitalWrite(ledPin5, LOW); // turn off the LED
delay(offTime); // wait
digitalWrite(ledPin6, HIGH); // turn on the LED
delay(onTime); // wait
digitalWrite(ledPin6, LOW); // turn off the LED
delay(offTime); // wait
*/
}
}

but I can only get it to bang

What does this mean?

How much time do you suppose elapses between calling digitalRead, seeing that play is true, and setting it to false and the next call to digitalRead?

Can you remove your finger from the button in that length of time?

I mean I can only cause the lights sequence to start by pressing the button, but not toggle the sequence on and off using the button. Should there be a delay after each button press maybe?

Should there be a delay after each button press maybe?

Yes. That was my point. You are doing something the first time the button is pressed. That causes there to be a delay before the button is read again.

You are doing virtually nothing the next time the button is pressed, so it is read again very quickly, which causes the play variable for go from true to false and back to true in a few microseconds.

You could add a state check. The button would then need to be pressed AND released before being counted as pressed.

The button library manages all this for you, though. To paraphrase Nike: "Just use it".