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
*/
}
}