Edit: Your code can be greatly simplified with the correct use of arrays, but try this. All I did was modify the example code.
/*
Debounce
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Debounce
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin[] = {2,3,4,5};
const int ledPins[] = {8,9,10,11}; // the number of the LED pin
int MAX_PINS = 4;
int i = 0;
int counter=0;
int reading[4];
//byte ledState = HIGH;
int buttonState[4]; // the current reading from the input pin
int lastButtonState[] = {LOW,LOW,LOW,LOW}; // the previous reading from the input pin
long lastDebounceTime[] = {0,0,0,0}; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
for(i = 0; i <= MAX_PINS; i++){
pinMode(buttonPin[i], INPUT);
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// read the state of the switch into a local variable:
for(counter = 0; counter <= MAX_PINS; i++)
{
reading[counter] = digitalRead(buttonPin[counter]);
if (reading[counter] != lastButtonState[counter]) {
lastDebounceTime[counter] = millis();
}
if ((millis() - lastDebounceTime[counter]) > debounceDelay) {
buttonState[counter] = reading[counter];
}
digitalWrite(ledPins[counter], buttonState[counter]);
lastButtonState[counter] = reading[counter];
}
}