Push button state change not getting out of while loop

Hello everyone
This code describes a Traffic Light program i have almost everything figured out.
There is menu i can change the time of every Traffic Light with it and it works fine.
But my problem is with another menu for selecting the priority of the Traffic Light for example i need the cars on the third street to go before the 2nd street and so on.
The Problem IS I can't get out of the loop and am using the same code as the time loop.
anyway here is my code which i have the problem in:

void select_priority()
{
  selectstate=!digitalRead(select);
    while(selectstate != lastselectstate)
    {
      real_time_exe();
      uppstate=!digitalRead(up);
      if(uppstate != lastuppstate)
      {
      priority++;
      if(priority > 3)
      priority = 0;
      Serial.print("priority= ");
      Serial.println(priority);
      delay(100);
      }
      uppstate = lastuppstate;
      downnstate =!digitalRead(down);
      if(downnstate != lastdownnstate)
      {
      priority--;
      if(priority < 0)
      priority = 3;
      Serial.print("priority= ");
      Serial.println(priority);
      delay(100);
      }
      downnstate = lastdownnstate;
      
      if(!digitalRead(select))break;
      delay(300);
    }
    selectstate = lastselectstate;
    delay(100);
}

Notes:
Am using 5 push buttons
1_Enter the menu for the time >> menu.
2_ up button to go up >> up.
3_ down button to go down >> down.
4_ ok button to enter the time interval for each semaphore >> ok.
5_ select button to enter the priority loop >> select.
The UP & DOWN buttons are used in 3 loops to go up and down.
Thanks in advance.

This is my Whole code :

byte menu=2,ok=3,up=4,down=5,select=6;
int updown=0;
int menustate=0;
int lastmenustate=0;
int okstate=0;
int lastokstate=0;
int upstate=0;
int lastupstate=0;
int downstate=0;
int lastdownstate=0;
int upinstate=0;
int lastupinstate=0;
int downinstate=0;
int lastdowninstate=0;
int selectstate=0;
int lastselectstate=0;
bool da = false;
bool das = false;
int priority=0;
byte upp=19,downn=20;
int uppstate=0,lastuppstate=0,downnstate=0,lastdownnstate=0;

byte g1=7,r1=8,g2=9,r2=10,g3=11,r3=12,g4=13,r4=14,y1=15,y2=16,y3=17,y4=18;
unsigned long prev=0;
unsigned long inter[8]={4000,inter[0]/4,4000,inter[2]/4,4000,inter[4]/4,4000,inter[6]/4};
byte counter=0;
void traffic(byte a,byte b,byte c,byte d,byte e,byte f,byte g,byte h,byte k,byte l,byte m,byte n)
{
    digitalWrite(r1,a);
    digitalWrite(r2,b);
    digitalWrite(r3,c);
    digitalWrite(r4,d);

    
    digitalWrite(g1,e);
    digitalWrite(g2,f);
    digitalWrite(g3,g);
    digitalWrite(g4,h);

    digitalWrite(y1,k);
    digitalWrite(y2,l);
    digitalWrite(y3,m);
    digitalWrite(y4,n);
}
void select_priority()
{
  selectstate=!digitalRead(select);
    while(selectstate != lastselectstate)
    {
      real_time_exe();
      uppstate=!digitalRead(up);
      if(uppstate != lastuppstate)
      {
      priority++;
      if(priority > 3)
      priority = 0;
      Serial.print("priority= ");
      Serial.println(priority);
      delay(100);
      }
      uppstate = lastuppstate;
      downnstate =!digitalRead(down);
      if(downnstate != lastdownnstate)
      {
      priority--;
      if(priority < 0)
      priority = 3;
      Serial.print("priority= ");
      Serial.println(priority);
      delay(100);
      }
      downnstate = lastdownnstate;
      
      if(!digitalRead(select))break;
      delay(300);
    }
    selectstate = lastselectstate;
    delay(100);
}
void real_time_exe()
{
  select_priority();
  if (priority ==0 )
  {
    switch(counter)
    {
      case 0:traffic(0,1,1,1,1,0,0,0,0,0,0,0);break;
      case 1:traffic(0,1,1,1,0,0,0,0,1,0,0,0);break;
      case 2:traffic(1,0,1,1,0,1,0,0,0,0,0,0);break;
      case 3:traffic(1,0,1,1,0,0,0,0,0,1,0,0);break;
      case 4:traffic(1,1,0,1,0,0,1,0,0,0,0,0);break;
      case 5:traffic(1,1,0,1,0,0,0,0,0,0,1,0);break;
      case 6:traffic(1,1,1,0,0,0,0,1,0,0,0,0);break;
      case 7:traffic(1,1,1,0,0,0,0,0,0,0,0,1);break;
    }
  }
  if (priority ==1 )
    {
    switch(counter)
    {
      case 0:traffic(0,1,1,1,1,0,0,0,0,0,0,0);break;
      case 1:traffic(0,1,1,1,0,0,0,0,1,0,0,0);break;
      case 2:traffic(1,1,1,0,0,0,0,1,0,0,0,0);break;
      case 3:traffic(1,1,1,0,0,0,0,0,0,0,0,1);break;
      case 4:traffic(1,1,0,1,0,0,1,0,0,0,0,0);break;
      case 5:traffic(1,1,0,1,0,0,0,0,0,0,1,0);break;
      case 6:traffic(1,0,1,1,0,1,0,0,0,0,0,0);break;
      case 7:traffic(1,0,1,1,0,0,0,0,0,1,0,0);break;
    }
    }
  if (priority ==2 )
    {
     switch(counter)
    {
      case 0:traffic(0,1,1,1,1,0,0,0,0,0,0,0);break;
      case 1:traffic(0,1,1,1,0,0,0,0,1,0,0,0);break;
      case 2:traffic(1,1,0,1,0,0,1,0,0,0,0,0);break;
      case 3:traffic(1,1,0,1,0,0,0,0,0,0,1,0);break;
      case 4:traffic(1,1,1,0,0,0,0,1,0,0,0,0);break;
      case 5:traffic(1,1,1,0,0,0,0,0,0,0,0,1);break;
      case 6:traffic(1,0,1,1,0,1,0,0,0,0,0,0);break;
      case 7:traffic(1,0,1,1,0,0,0,0,0,1,0,0);break;
    }
    }
    if (priority ==3 )
    {
     switch(counter)
    {
      case 0:traffic(0,1,1,1,1,0,0,0,0,0,0,0);break;
      case 1:traffic(0,1,1,1,0,0,0,0,1,0,0,0);break;
      case 2:traffic(1,1,1,0,0,0,0,1,0,0,0,0);break;
      case 3:traffic(1,1,1,0,0,0,0,0,0,0,0,1);break;
      case 4:traffic(1,0,1,1,0,1,0,0,0,0,0,0);break;
      case 5:traffic(1,0,1,1,0,0,0,0,0,1,0,0);break;
      case 6:traffic(1,1,0,1,0,0,1,0,0,0,0,0);break;
      case 7:traffic(1,1,0,1,0,0,0,0,0,0,1,0);break;
    }
  }
  
  if(millis()-prev>=inter[counter])
  {
  prev=millis();  
  counter++;
  if(counter>7)
  counter=0;
  }
}
void setup() 
{
  for(int j=2;j<7;j++)
  {
    pinMode(j,INPUT_PULLUP);
  }
   for(int i=7;i<19;i++)
  {
   pinMode(i,OUTPUT);
   digitalWrite(i,LOW); 
  }
  pinMode(upp,INPUT_PULLUP);
  pinMode(downn,INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() 
{
real_time_exe();
  menustate=!digitalRead(menu);
  while(menustate != lastmenustate)//checking if the menu button is pressed
  {
      if(da == false)
  {
  Serial.print("Press Menu");
    Serial.println("Light a");
    Serial.println("Light b");
    Serial.println("Light c");
    Serial.println("Light d");
  
  da=true;
  }
    upstate =!digitalRead(up);
    if(upstate != lastupstate)//checking if the up button is pressed
    {
    updown++;
    if(updown > 3)
    updown=0;
    Serial.print("updown= ");
    Serial.println(updown);
    delay(100);
    }
    upstate = lastupstate;
    downstate =!digitalRead(down);
    if(downstate != lastdownstate)
    {
      updown--;
      if(updown < 0)
      updown=3;
      Serial.print("updown= ");
      Serial.println(updown);
    delay(100);
    }
    downstate = lastdownstate;
    f();
    g();
    h();
    k(); 
          delay(300);
if(!digitalRead(menu))
break;

  
  }
  lastmenustate = menustate;
  da=false;
delay(100);
}


void f()
{
  real_time_exe();
  okstate=!digitalRead(ok);
    while((okstate != lastokstate) && (updown == 0))
    {
      //real_time_exe();
      upinstate=!digitalRead(up);
      if(upinstate != lastupinstate)
      {
      inter[0]+=1000;
      if(inter[0] >= 90000)
      inter[0] = 90000;
      Serial.print("ainter= ");
      Serial.println(inter[0]);
      delay(100);
      }
      upinstate = lastupinstate;
      downinstate =!digitalRead(down);
      if(downinstate != lastdowninstate)
      {
      inter[0] = inter[0]-1000;
      if(inter[0] >= 90000)
      inter[0] = 0;
      Serial.print("ainter= ");
      Serial.println(inter[0]);
      delay(100);
      }
      downinstate = lastdowninstate;
      if(!digitalRead(menu))break;
      
    }
    okstate = lastokstate;
}
void g()
{
    real_time_exe();
    okstate=!digitalRead(ok);
    while((okstate != lastokstate) && (updown == 1))
    {
      //real_time_exe();
      upinstate=!digitalRead(up);
      if(upinstate != lastupinstate)
      {
      inter[2]+=1000;
      if(inter[2] >= 90000)
      inter[2] = 90000;
      Serial.print("binter= ");
      Serial.println(inter[2]);
      delay(100);
      }
      upinstate = lastupinstate;
      downinstate =!digitalRead(down);
      if(downinstate != lastdowninstate)
      {
      inter[2]-=1000;
      if(inter[2] >= 90000)
      inter[2] = 0;
      Serial.print("binter= ");
      Serial.println(inter[2]);
      delay(100);
      }
      downinstate = lastdowninstate;
      if(!digitalRead(menu))break;
      
      }
    okstate = lastokstate;
}
void h()
{
      real_time_exe();
         okstate=!digitalRead(ok);
    while((okstate != lastokstate) && (updown == 2))
    {
      //real_time_exe();
      upinstate=!digitalRead(up);
      if(upinstate != lastupinstate)
      {
      inter[4]+=1000;
      if(inter[4] >= 90000)
      inter[4] = 90000;
      Serial.print("cinter= ");
      Serial.println(inter[4]);
      delay(100);
      }
      upinstate = lastupinstate;
      downinstate = !digitalRead(down);
      if(downinstate != lastdowninstate)
      {
      inter[4]-=1000;
      if(inter[4] >= 90000)
      inter[4] = 0;
      Serial.print("cinter= ");
      Serial.println(inter[4]);
      delay(100);
      }
      
      downinstate = lastdowninstate;
    if(!digitalRead(menu))break;
   
    }
    okstate = lastokstate;
}
void k()
{
  real_time_exe();
      okstate=!digitalRead(ok);
    while((okstate != lastokstate) && (updown == 3))
    {
      //real_time_exe();
      upinstate=!digitalRead(up);
      if(upinstate != lastupinstate)
      {
      inter[6]+=1000;
      if(inter[6] >= 90000)
      inter[6] = 90000;
      Serial.print("dinter= ");
      Serial.println(inter[6]);
      delay(100);
      }
      upinstate = lastupinstate;
      downinstate = !digitalRead(down);
      if(downinstate != lastdowninstate)
      {
      inter[6]-=1000;
      if(inter[6] >= 90000)
      inter[6] = 0;
      Serial.print("dinter= ");
      Serial.println(inter[6]);
      delay(100);
      }
      downinstate = lastdowninstate;
      
      if(!digitalRead(menu))break;
    
    }
    okstate = lastokstate;
}
[code]

[/code]

nothing to point out ? am really disparate for this cause i couldn't find any logical answer ty again

Your code as it stands is very difficult to understand what is going on.
You have a very large number of very similar repeated sections of code like this:

          downinstate = !digitalRead(down);
          if (downinstate != lastdowninstate)
            {
            inter[0] = inter[0] - 1000;
            if (inter[0] >= 90000)
              inter[0] = 0;
            Serial.print("ainter= ");
            Serial.println(inter[0]);
            delay(100);
            
    }
          downinstate = lastdowninstate;

I think you need to make a function() for this and call it as necessary, rather than repeating the same structure.
Also try to avoid using very short names e.g. e,f,g because they are meaningless to anyone trying to understand what you are trying to do.

I have not studied your code carefully but your description of the problem leads me to think that you should replace all the WHILE loops with IF clauses and allow loop() to do the repetition.

In general WHILE and FOR should only be used if they are guaranteed to complete in about a millisec or less.

Also I suggest that you read all the buttons at the same time at the start of an iteration of loop() and save the values for use elsewhere in the program.

Have a look at how the code is organized in Several Things at a Time and in Planning and Implementing a Program.

...R

Really hard to understand, but I think I have spotted something very wrong

  lastmenustate = menustate;

This is the only line there a last... variable change value

You have a huge amount of

      uppstate = lastuppstate;
      downnstate = lastdownnstate;
    selectstate = lastselectstate;
    upstate = lastupstate;
    downstate = lastdownstate;
      upinstate = lastupinstate;
      downinstate = lastdowninstate;
    okstate = lastokstate;
      upinstate = lastupinstate;
      downinstate = lastdowninstate;

All your variables are initialized as 0, so you are resetting your state... variables instead of keeping track of previous state. I doubt that is what you intended to do.

Thanks everyone my problem is solved now.
I have moved the select_priority() function inside the menu and changed the break out of the loops button.
Organizing the code is important an i have followed ur advises and it was helpful.
Thanks.