controlling several leds using a single pushbutton

I am new to ardiuno. I know C++. I tried controlling 3 leds using single pushbutton but it didnt work PLEASE HELP me to get this done.

const int buttonPin = 2; // the number of the pushbutton pin
const int led1 = 3; // the number of the LED pin
const int led2 = 4;
const int led3 = 5;

// Variables will change:
int led1State = LOW;
int led2State = LOW;
int led3State = LOW;// the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int i=0;
// the following variables are long’s because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(buttonPin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);

// set initial LED state
digitalWrite(led1, led1State);
digitalWrite(led2, led2State);
digitalWrite(led3, led3State);
}

void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);

// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you’ve waited
// long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it’s been there for longer
// than the debounce delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) i++;i<4; {
if(i==1){
led1State = !led1State;
led2State = led2State;
led3State = led3State;
}
if(i==2){
led1State = led1State;
led2State = !led2State;
led3State = led3State;
}
if(i==3){
led1State = led1State;
led2State = led2State;
led3State = !led3State;
}

}
}
}

// set the LED:
digitalWrite(led1, led1State);
digitalWrite(led2, led2State);
digitalWrite(led3, led3State);

// save the reading. Next time through the loop,
// it’ll be the lastButtonState:
lastButtonState = reading;
}
PLEASE HELP
rk out

Time related variables should be unsigned long, that is what millis() and micros() return:

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

This line looks really suspect:
if (buttonState == HIGH) i++;i<4; {

I think what you intended is:
if (buttonState == HIGH) {
i=i+1;
if (i==4){ i=1;} // cycle thru the states, reset after the 3rd one
// continue on
if(i==1){
led1State = !led1State;
etc.

How curious!

I just answered that query yesterday!

Mind you, the code I posted had a funny effect because of the way that fellow started it, but you could insert your code snippets such as

 led1State = !led1State;
led2State =  led2State;
led3State =  led3State;

into it and get it to do just what you (and in fact, he) actually intended.

Oh yes, go back to your first post, choose “modify”, highlight the actual code part and click the “code” icon - a little scroll - to show it as proper code.

Then question is what you are tryimg to acheive. Do you want your code to work the way you intended or do you want to solve a problem involving 3 leds lit cosecutively by the same button? Do you need separate state machines for each of the leds influencing each other (your solution). Or do you want one state machine with 3 (or maybe 4 or 6 states) or do you want a state machine with 2 states and a counter deciding which led to lit? The latter can even be done with only edge detection and a counter.

Can anybody suggest a program using debounce function .Or tell me what i have done wrong in the code that i have posted.
PAUL i modified your program it did work a i want to but i need to know if the same program can be doen using debounce function.