3 LED state machine, one input and timers

Hi everyone.

Im engaged in a little project involving states, three outputs (LEDs) and one input (Pushbutton).

Basically what I want is that for every button press, one LED lights up. So after one press, the first LED should light up, after the second press, the second LED should light up and finally after a third press, the third LED should light up.

The LEDs light as wanted but I need each led to turn off after 4000ms of being on. I’m assigning each LED a time; the total runtime of the board at the moment the button is pressed for each case. With an ‘if’ statement I’m trying to tell the board that after that time has surpassed 4000ms, then the LED should turn off but it doesn’t work. I guess I’m not comparing the time properly, but I can’t think of any other way to do it so far.

Below you will find the wiring and code. Also you can check the project at Autodesk Circuits.

Any kind of help would be great. Thanks in advance!

int buttonPIN=2;
int L1=6;
int L2=5;
int L3=4;

int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;


unsigned long t1;
unsigned long t2;
unsigned long t3;


void setup()  
{
  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
  pinMode(L3, OUTPUT);
  pinMode(buttonPIN, INPUT);
  
  Serial.begin(9600);
}

void one()
{
  digitalWrite(L1,1);
}

void two()
{
  digitalWrite(L2,1);
}

void three()
{
  digitalWrite(L3,1);
}


void loop() 
{
  buttonState = digitalRead(buttonPIN);
  unsigned long runtime=millis();
  Serial.println(runtime);
    
  if (buttonState != lastButtonState) 
  {
    if (buttonState == HIGH) 
    {
      buttonPushCounter++;
      if(buttonPushCounter == 4)
      { 
        buttonPushCounter = 1;
      }
      
    } 
    else 
    {
      
    }
  }
  
  
  lastButtonState = buttonState;
  
   switch(buttonPushCounter)
  {
    case 1:
      one();
      t1=runtime;
       if (t1>=t1+4000)
     {
       digitalWrite(L1,0);
     }
      break;

    case 2:
      two();
      t2=runtime;
       if (t2>=t2+4000)
     {
       digitalWrite(L2,0);
     }
      break;

    case 3:
      three();
      t3=runtime;
       if (t3>=t3+4000)
     {
       digitalWrite(L3,0);
     }
      break;     

   }
}

      t1=runtime;
      if (t1>=t1+4000)

How do you think that t1 can ever be higher than t1+4000?

You have to remember the start time, when a LED is turned on. Then compare like this:

  if (runtime-t1 >= 4000)