Help clean up my 2 LED blink code

I have a 2 LED display that needs to blink in different patterns. I would like help to fix the code for when (buttonPushCounter == 5), currently all the delays in it makes the process hang so you can't easily cycle to the next button press.

//
//c++
int ledState1 ;  // led1 control
int ledState2 ; // led2 control
int buttonPushCounter = 1;   // counter for the number of button presses
int buttonState;         // current state of the button
int lastButtonState;     // previous state of the button
int led1 = 10;
int led2 = 11;
int ledPin = 13;       // the pin that the LED is attached to
unsigned long previousMillis = 0; //time of last blink
const long interval175 = 175; //175 millisecond blink interval
const long interval200 = 200;


void setup() {
// put your setup code here, to run once:
//Turn off Built in LED
digitalWrite(ledPin, LOW);
pinMode(9,INPUT); // we are setting pin 2 as our input where we are connectign the soft touch switch
pinMode(led1, OUTPUT); // 1st LED light
pinMode(led2, OUTPUT); // 2nd LED light
pinMode(ledPin, OUTPUT); //Built in LED
Serial.begin(9600);
}

void loop() {

  // read the pushbutton input pin:
  buttonState = digitalRead(9);
  unsigned long currentMillis = millis();
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.print("number of button pushes:");
      Serial.println(buttonPushCounter);
    }
    
    // Delay a little bit to avoid bouncing
    delay(20);
  }
    // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
  if(buttonPushCounter==6){
     buttonPushCounter=1;
     Serial.print("number of button pushes:");
     Serial.println(buttonPushCounter);
  }

if(buttonPushCounter == 1){
  digitalWrite(led1, LOW);   // turn the LED off by making the voltage LOW
  digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
 }

else if(buttonPushCounter == 2){
digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
}
else if (buttonPushCounter == 3){

  if (currentMillis - previousMillis >= interval175) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    if (ledState1 == LOW) {
      ledState1 = HIGH;
      ledState2 = LOW;
    } else {
      ledState1 = LOW;
      ledState2 = HIGH;
    } 
    digitalWrite(led1, ledState1);
    digitalWrite(led2, ledState2);
    
}}
else if (buttonPushCounter == 4){

  if (currentMillis - previousMillis >= interval200) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    if (ledState1 == LOW) {
      ledState1 = HIGH;
      ledState2 = HIGH;
    } else {
      ledState1 = LOW;
      ledState2 = LOW;
    } 
    digitalWrite(led1, ledState1);
    digitalWrite(led2, ledState2);
    
  }}
  else if (buttonPushCounter == 5){

  digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(75);
  digitalWrite(led1, LOW);    // turn the LED off by making the voltage LOW
  delay(150);                 // wait
  digitalWrite(led1, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(75);
  digitalWrite(led1, LOW);   // turn the LED off by making the voltage LOW
  delay(150);                 // wait
  digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);
  digitalWrite(led1, LOW);    // turn the LED off by making the voltage LOW
  delay(450);                 // wait
  digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(75);
  digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
  delay(150);                 // wait
  digitalWrite(led2, HIGH);    // turn the LED on (HIGH is the voltage level)
  delay(75);
  digitalWrite(led2, LOW);   // turn the LED off by making the voltage LOW
  delay(150);                 // wait
  digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);
  digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
  delay(450);                 // wait
    
}}

Time to learn about millis!!

I've been trying, I used them for the previous two button pushes. I can't seem to work out how to get it to work with the last pattern since it is not a constant blink.

Congrats You have a code doing the job!
Any crappy, spagetti, code doing what it's wanted to do is a good code. A sophisticated code malfunctioning makes nobody satisfied. Your code is not that bad.
Keep Your eyes open looking at other topics and suck up new ways to do things in the future.

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy and show us the complete sketch.

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.

//
//c++
int buttonPushCounter = 1;   // counter for the number of button presses
int buttonState;         // current state of the button
int lastButtonState;     // previous state of the button
int led1 = 10;
int led2 = 11;
int ledPin = 13;       // the pin that the LED is attached to

void setup() {
  // put your setup code here, to run once:
  pinMode(9, INPUT); // we are setting pin 2 as our input where we are connectign the soft touch switch
  pinMode(led1, OUTPUT); // 1st LED light
  pinMode(led2, OUTPUT); // 2nd LED light
  pinMode(ledPin, OUTPUT); //Built in LED
  Serial.begin(9600);
}

void loop() {
  //Turn off Built in LED
  digitalWrite(ledPin, LOW);
  // read the pushbutton input pin:
  buttonState = digitalRead(9);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.print("number of button pushes:");
      Serial.println(buttonPushCounter);
    }

    // Delay a little bit to avoid bouncing
    delay(20);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
  if (buttonPushCounter == 5) {
    buttonPushCounter = 1;
    Serial.print("number of button pushes:");
    Serial.println(buttonPushCounter);
  }

  if (buttonPushCounter == 1) {
    digitalWrite(led1, LOW);   // turn the LED off by making the voltage LOW
    digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
  }

  else if (buttonPushCounter == 2) {
    digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
  }
  else if (buttonPushCounter == 3) {
    digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
    digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
    delay(150);                 // wait
    digitalWrite(led1, LOW);    // turn the LED off by making the voltage LOW
    digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(150);                 // wait

  }
  else if (buttonPushCounter == 4) {
    digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(75);
    digitalWrite(led1, LOW);    // turn the LED off by making the voltage LOW
    delay(150);                 // wait
    digitalWrite(led1, HIGH);    // turn the LED on (HIGH is the voltage level)
    delay(75);
    digitalWrite(led1, LOW);   // turn the LED off by making the voltage LOW
    delay(150);                 // wait
    digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);
    digitalWrite(led1, LOW);    // turn the LED off by making the voltage LOW
    delay(450);                 // wait
    digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(75);
    digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
    delay(150);                 // wait
    digitalWrite(led2, HIGH);    // turn the LED on (HIGH is the voltage level)
    delay(75);
    digitalWrite(led2, LOW);   // turn the LED off by making the voltage LOW
    delay(150);                 // wait
    digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);
    digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
    delay(450);                 // wait

  }
}

Assume this is the part that you want help with.

Assume you know delay(…) is code blocking.

Describe exactly what you are having trouble doing.

When I get to the 5th button push you have to hold the button down to get it to register the push, I know this is due to all the delays written in the code for that but I can't find a better way to do it.

Button Push 1 is both LEDs off
Button Push 2 is both LEDs on
Button Push 3 is alternating LEDs
Button Push 4 is Blinking together
Button Push 5 is a 3 blink alternating LED pattern

My "problem" is when it is at the 5th button push you have to either hold the button down or push it multiple times before it cycles to the next function.

I know the problem is really the 3 blink alternating LED pattern, I don't know how to make this pattern without using delays and the delays make it not register the button push.

Looks like you might be familiar with using TIMERs based on the millis( ) function.

  • Do you know what an array is ?

You can set up a counter that accesses the elements in an array.

The array holds all the interval values necessary in the final led sequence.

If you iterate thru this array, you can set a common millis based TIMER to each interval.

When all array elements are processed, you have finished your LED sequence.

This way, the 5th push will not be missed while running the buttonPushCounter #4 sequence.

  • Acknowledgment of the 5th push will stop the LED sequence, is that what’s needed ?

I'm not familiar with an array but it sounds like exactly what I need

Read this:

  • Do you understand what is being said in that discussion ?

  • Do you have questions ?

  • Do you know what a State Machine is ?

Thx I'll do some research on it and hopefully post my results in a couple days

We can help you with this but you will need to:

  • answer our questions,
  • ask questions when you don't understand things,
  • try our suggested code changes to see if it is what is needed.

I'm watching some videos on arrays and State Machines now, I'm sure I will have questions right now I'm trying to learn what they are and come up with a idea on how to redo my code

When you are finished with watching the videos, come back and we can start changing your sketch.

  • while you are running the LED sequence #4, what is supposed to happen when you press the switch (i.e. 5th press) ?

After the button is pressed again it will reset to the beginning

 // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
  if(buttonPushCounter==6){
     buttonPushCounter=1;
     Serial.print("number of button pushes:");
     Serial.println(buttonPushCounter);
  }

What sketch are we going to be working with ? post #1 or post #6 ?

Sorry post 1, I didn't realize I copied an older one for post 6

Okay

When you are ready to proceed, post any questions here.

I had also tried a variation with these changes but it didn't help

void blinkLED3() {
  static unsigned long LED3millis = millis();

  if (millis() - LED3millis > 450) {
    digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(75);
    digitalWrite(led1, LOW);    // turn the LED off by making the voltage LOW
    delay(150);                 // wait
    digitalWrite(led1, HIGH);    // turn the LED on (HIGH is the voltage level)
    delay(75);
    digitalWrite(led1, LOW);   // turn the LED off by making the voltage LOW
    delay(150);                 // wait
    digitalWrite(led1, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);
    digitalWrite(led1, LOW);    // turn the LED off by making the voltage LOW
    delay(450);                 // wait
    digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(75);
    digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW
    delay(150);                 // wait
    digitalWrite(led2, HIGH);    // turn the LED on (HIGH is the voltage level)
    delay(75);
    digitalWrite(led2, LOW);   // turn the LED off by making the voltage LOW
    delay(150);                 // wait
    digitalWrite(led2, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);
    digitalWrite(led2, LOW);    // turn the LED off by making the voltage LOW

    LED3millis = millis();
  }
}
  else if (buttonPushCounter == 4) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    // blinkLED1();
    //blinkLED2();
    blinkLED3();
  }