I'm a newbie to Arduino so please bear with me! I'm trying to write a program where a push button controls 5 LEDs advancing from one to the other with each button press. When I run the program however, the 5 LEDs just start blinking sequentially on their own and the button has no affect whatsoever. My code is below... any clues?
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin1 = 3; // the number of the LED pin
const int ledPin2 = 4; // the number of the LED pin
const int ledPin3 = 5; // the number of the LED pin
const int ledPin4 = 6; // the number of the LED pin
const int ledPin5 = 7; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonPressCount = 0;
int numberOfLED = 5;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
if (buttonPressCount % numberOfLED == 0) {
// turn LED1 on:
digitalWrite(ledPin1, HIGH);
} else {
digitalWrite(ledPin1, LOW);
}
if (buttonPressCount % numberOfLED == 1) {
// turn LED1 on:
digitalWrite(ledPin2, HIGH);
} else {
digitalWrite(ledPin2, LOW);
}
if (buttonPressCount % numberOfLED == 2) {
// turn LED1 on:
digitalWrite(ledPin3, HIGH);
} else {
digitalWrite(ledPin3, LOW);
}
if (buttonPressCount % numberOfLED == 3) {
// turn LED1 on:
digitalWrite(ledPin4, HIGH);
} else {
digitalWrite(ledPin4, LOW);
}
if (buttonPressCount % numberOfLED == 4) {
// turn LED1 on:
digitalWrite(ledPin5, HIGH);
} else {
digitalWrite(ledPin5, LOW);
}
buttonPressCount++;
delay(400);
}
}