Two leds blink with one button

I did my blinking code which when I hold the button the LEDs go from one to another but now I am trying to figure out how to make it so when I let go of the button my red LED stays on then the next time I hold the button red turns off the blinking happens and when I let go the green LED comes on and red is off, basically a loop.

This is my current code for the blink:

const int buttonPin = 10; 	// the number of the pushbutton pin
const int ledPin =  12;      // the number of the red LED pin
const int ledPin2 =  8;      // the number of the  green LED pin
// variables will change:
int buttonState = 0;     	// variable for reading the pushbutton status
int mes = 0;                                              	// to keep track of what led is on/off in the loop.
void setup() {
  // initialize the outputs and inputs for the code:
  pinMode(ledPin, OUTPUT);	
  pinMode(ledPin2, OUTPUT); 
  pinMode(buttonPin, INPUT);	
  }
void loop(){
  // read the state of the pushbutton value:
 	buttonState = digitalRead(buttonPin); 
  //turning the loop around from 1-0 and 0-1 using the mes int
 	if (buttonState==HIGH && mes==0){
  	mes=1;
 	}
          	else if(buttonState==HIGH && mes==1){
          	mes=0;
}
	//turns red LED on if the button is pressed:	
	if(buttonState==HIGH && mes==0){
	digitalWrite(ledPin2, HIGH);
	delay(500); //half a second delay before red LED turns off
	digitalWrite(ledPin2, LOW);
	buttonState=LOW;
	}
  //changes LED from red to green
	else if (buttonState==HIGH && mes==1){
	digitalWrite(ledPin,HIGH);
	delay(500);// half a second delay before turning off
	digitalWrite(ledPin,LOW);
	buttonState=LOW;
	} 
   if(buttonState==LOW){
 	digitalWrite(ledPin, LOW);
 	digitalWrite(ledPin2,LOW);
 	}
	}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Adding a "<" in the beginning and a ">" at the end is not the way.
Click on the "</>" symbol, and then paste the IDE autoformatted code.

sorry about that .🏃‍♂️

Your description is very confusing. Is it like this?:

repeat
{
  blink and wait for button press
  red and wait for button press
  green and wait for button press
}

?

No problem, You tried. New game, new bets.

//First clicked 6:th symbol from the left in the bar up in the window.
if ( success ){
   //and formatted
   //code shows  up well
}

Nah, I want to modify the program so that the red LED stays ON after the blink cycle for the first button press (i.e., when I release the button), then the green LED stays on after the cycle for the second button press. Then that sequence should continue for subsequent button presses.

Switch case is perfect for sequencing. Check Arduino reference!

I attempted to use a switch case and it works halfway it turns on the green led after blink but after going again it doesn't turn on the red LED it just goes back to green.
switch (mes) { case 1: if (buttonState == HIGH){ buttonState = LOW; digitalWrite (ledPin2,HIGH); digitalWrite (ledPin,LOW); delay (100); break; } case 2: digitalWrite (ledPin,LOW); digitalWrite (ledPin2,HIGH);
I already attempted using the same thing as case 1 but changing the Pins for the red one and it doesn't work it goes back to the basic code.

Please post that code.

I put the switch case at the bottom of the code.

const int buttonPin = 10;     // the number of the pushbutton pin
const int ledPin =  12;      // the number of the red LED pin
const int ledPin2 =  8;      // the number of the  green LED pin
// variables will change:

int buttonState = 0;         // variable for reading the pushbutton status
int mes = 0;				// to keep track of what led is on/off in the loop. 

void setup() {
 
  // initialize the outputs and inputs for the code:
  pinMode(ledPin, OUTPUT);    
  pinMode(ledPin2, OUTPUT);  
  pinMode(buttonPin, INPUT);    
  }

void loop(){
  // read the state of the pushbutton value:
     buttonState = digitalRead(buttonPin);  
  //turning the loop around from 1-0 and 0-1 using the mes int
     if (buttonState==HIGH && mes==0){
      mes=1;
     }
	else if(buttonState==HIGH && mes==1){
	mes=0;
}
    //turns red LED on if the button is pressed:    
    if(buttonState==HIGH && mes==0){
    digitalWrite(ledPin2, HIGH);
    delay(500); //half a second delay before red LED turns off 
    digitalWrite(ledPin2, LOW); 
    buttonState=LOW; 
    }
  //changes LED from red to green
    else if (buttonState==HIGH && mes==1){ 
    digitalWrite(ledPin,HIGH);
    delay(500);// half a second delay before turning off 
    digitalWrite(ledPin,LOW); 
    buttonState=LOW;
    }  
   if(buttonState==LOW){
     digitalWrite(ledPin, LOW);
     digitalWrite(ledPin2,LOW);

     } // switch statemnt for when the button isn't pressed the LEDS to stay on
     switch (mes) {
       case 1:
       if (buttonState == HIGH){
       buttonState = LOW;
         digitalWrite (ledPin2,HIGH);
       digitalWrite (ledPin,LOW);// after the button is let go turn on red LED and turn off green LED
      delay (100);
         break;
       }
       case 2:
       digitalWrite (ledPin,LOW);
       digitalWrite (ledPin2,HIGH); // second time after the button is let go green LED on red off

     }
    }

I would put all actions into the cases. The first case will look for the trigger to start the next level, next case. First doing some digitalWrite and some in a case is a mismash in my world. Being in one case looking for the criteria for the next case, performing a closing digitalWrite and stepping up the case variable looks better to me.

Hello
I surfed in my sketch box and I´ve found a similar sketch. Below you will find a modified sketch to your needs. I´m using logical groups for a timer, leds and for the button. All in all I´m using a switch/case based FSM too. Check it out and make the neccessay changes for your project.

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  https://forum.arduino.cc/t/two-leds-blink-with-one-button/923427
*/
#define ProjectName "Two leds blink with one button"
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte ButtonPin {A0};      // portPin o---|button|---GND
constexpr byte LedRedPin {3};       // portPin o---|220|---|LED|---GND
constexpr byte LedGreenPin {5};     // portPin o---|220|---|LED|---GND
unsigned long BlinkDuration {200};  // time in msec
//#define OutPutTest
// CONSTANT DEFINITION
enum {Button_};
enum {LedRed, LedGreen};
constexpr byte Input_[] {ButtonPin};
constexpr byte Output_[] {LedRedPin, LedGreenPin};
constexpr  unsigned long OutPutTestTime {1000};
constexpr  unsigned long DebounceTime {20};
enum {Idle, Blink, RedLed, GreenLed, MaxState};
bool switchCase = false;
String CaseText[] = {"Idle", "Blink", "RedLed", "GreenLed"};
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct TIMER {              // has the following members
  unsigned long duration;   // memory for interval time
  unsigned long stamp;      // memory for actual time
  bool control_;            // control for start/stop
  bool repeat_;             // control for blinking
};
TIMER blink_{BlinkDuration, 0, true, true};
struct BUTTON {
  byte pin;
  bool statusQuo;
  int counter;
  TIMER timer;
} buttons[] {
  {Input_[Button_], false, 0, DebounceTime, 0, true, true},
};
// USER FUNCTIONS
void startTimer(TIMER &timer) {
  timer.control_ = true;
  timer.stamp = currentTime;
}
bool checkTimer(TIMER & time_) {  // generic time handler using TIME struct
  if (currentTime - time_.stamp >= time_.duration && time_.control_) {
    if (time_.repeat_) time_.stamp = currentTime;
    else time_.control_ = false;
    return true;
  } else return false;
}
// -------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  //  https://www.learncpp.com/cpp-tutorial/for-each-loops/
  for (auto Input : Input_) pinMode(Input, INPUT_PULLUP);
  for (auto Output : Output_) pinMode(Output, OUTPUT);
#ifdef OutPutTest
  // check outputs
  for (auto Output : Output_) digitalWrite(Output, HIGH), delay(OutPutTestTime);
  for (auto Output : Output_) digitalWrite(Output, LOW), delay(OutPutTestTime);
#endif
}
void loop () {
  currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);

  if (checkTimer(buttons[Button_].timer)) {
    bool stateNew = !digitalRead(buttons[Button_].pin);
    if (buttons[Button_].statusQuo != stateNew) {
      buttons[Button_].statusQuo = stateNew;
      buttons[Button_].counter++;
      buttons[Button_].counter = buttons[Button_].counter % MaxState;
      if ( buttons[Button_].counter == Blink) {
        startTimer(blink_);
        digitalWrite(Output_[LedRed], HIGH);
      }
      Serial.println(CaseText[buttons[Button_].counter]);
    }
  }
  switch (buttons[Button_].counter) {
    case Idle:
      for (auto LED : Output_) digitalWrite(LED, LOW);
      break;
    case Blink:
      if (checkTimer(blink_)) {
        digitalWrite(Output_[LedRed], !digitalRead(Output_[LedRed]));
        digitalWrite(Output_[LedGreen], !digitalRead(Output_[LedGreen]));
      }
      break;
    case RedLed:
      digitalWrite(Output_[LedRed], HIGH);
      digitalWrite(Output_[LedGreen], LOW);
      break;
    case GreenLed:
      digitalWrite(Output_[LedRed], LOW);
      digitalWrite(Output_[LedGreen], HIGH);
      break;
  }
}

Have a nice day and enjoy coding in C++.