button state wont increase

good evening

can anyone help a newbie

when I press the button I can get the program to increase the state but only as far as 3

what am I missing?

also how would I program it to start back at 0 state?

many thanks

int L1 = 2;
int L2 = 3;
int L3 = 4;
int L4 = 5;
int L5 = 6; //sets the leds to pin number

int buttonPin = 13; //the number of the pushbutton pin

int de=100; // delay time

// variables to hold the new and old switch states
int buttonState = 0;

byte state = 0;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 500; // the debounce time; increase if the output flickers
void setup()
{
pinMode(L1,OUTPUT); // Sets the leds to output
pinMode(L2,OUTPUT);
pinMode(L3,OUTPUT);
pinMode(L4,OUTPUT);
pinMode(L5,OUTPUT);

pinMode(buttonPin, INPUT); // sets button as an input
}

void loop()
{
{
buttonState = digitalRead(buttonPin);

// has the button switch been closed?
if ( buttonState == HIGH )
{
// increase the value of state
state++;
}

}
// Turn on the next LED
// Because the value of state does not change while we are testing it we don't need to use else if
if (state == 1) {
digitalWrite(L1,1);
digitalWrite(L2,0);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,0);
delay(de);

digitalWrite(L1,0);
digitalWrite(L2,1);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,0); //2
delay(de);

digitalWrite(L1,0);
digitalWrite(L2,0);
digitalWrite(L3,1);
digitalWrite(L4,0);
digitalWrite(L5,0); //3
delay(de);

digitalWrite(L1,0);
digitalWrite(L2,0);
digitalWrite(L3,0);
digitalWrite(L4,1);
digitalWrite(L5,0); //4
delay(de);

digitalWrite(L1,0);
digitalWrite(L2,0);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,1); //5
delay(de);

}
if (state == 2) {
digitalWrite(L1,1);
digitalWrite(L2,1);
digitalWrite(L3,1);
digitalWrite(L4,1);
digitalWrite(L5,1);
delay(de);

digitalWrite(L1,0);
digitalWrite(L2,0);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,0); //6
delay(de);
}
if (state == 3) {
digitalWrite(L5,0); // turn the pin on:
digitalWrite(L4,0); // turn the pin on:
digitalWrite(L3,0); // turn the pin on:
digitalWrite(L2,0); // turn the pin on:
digitalWrite(L1,0); // turn the pin on:
delay(200);
digitalWrite(L1,1); // turn the pin on:
delay(100);
digitalWrite(L1,0); // turn the pin off
digitalWrite(L2,1); // turn the pin on:
delay(100);
digitalWrite(L2,0); // turn the pin off
digitalWrite(L3,1); // turn the pin on:
delay(100);
digitalWrite(L3,0); // turn the pin off
digitalWrite(L4,1); // turn the pin on:
delay(100);
digitalWrite(L4,0); // turn the pin off
digitalWrite(L5,1); // turn the pin on:
delay(100);
digitalWrite(L1,1); // turn the pin on:
delay(100);
digitalWrite(L1,0); // turn the pin off
digitalWrite(L2,1); // turn the pin on:
delay(100);
digitalWrite(L2,0); // turn the pin off
digitalWrite(L3,1); // turn the pin on:
delay(100);
digitalWrite(L3,0); // turn the pin off
digitalWrite(L4,1); // turn the pin on:
delay(100);
digitalWrite(L1,1); // turn the pin on:
delay(100);
digitalWrite(L1,0); // turn the pin off
digitalWrite(L2,1); // turn the pin on:
delay(100);
digitalWrite(L2,0); // turn the pin off
digitalWrite(L3,1); // turn the pin on:
delay(100);
digitalWrite(L1,1); // turn the pin on:
delay(100);
digitalWrite(L1,0); // turn the pin off
digitalWrite(L2,1); // turn the pin on:
delay(100);
digitalWrite(L1,1); // turn the pin on:
delay(500);
}
if (state == 4) {
digitalWrite(L1,1);
digitalWrite(L2,1);
digitalWrite(L3,1);
digitalWrite(L4,1);
digitalWrite(L5,1);
}
}

new.ino (4.66 KB)

what am I missing?

</> code-tags and a wiring schematic.

when I press the button I can get the program to increase the state but only as far as 3

well every time the state 3 is executed (this is while it is 3 the conditional part starts every time) all added delays take 2100 ms, somehow if you hold the button down for more than that it should increase, but really check out the use millis() for timing sticky at the top of the topic list.

you code will works unpredictably. You should pay attention to the following:

  1. You did not implement deboucing
  2. Did you use the pull-down or pull up resistor? . See Button FAQ: common mistake - button does NOT work as expected. - Introductory Tutorials - Arduino Forum
  3. For counting, you should detect the state change. . See Detect button change
  4. You should NOT use delay() function when using button. See BlinkWihoutDelay

Also, blink code can be grouped into a function to reduce code size.