a problem with my traffic project using a keypad with lcd and arduino

i want to enter a countdown second timer using the keypad buttons for green, yellow and red leds (for each one)
but my code makes the same time for all.
please help!

#include <LiquidCrystal.h>

#include <Keypad.h>
const byte ROWS = 4; 
const byte COLS = 3; 
char keys[ROWS][COLS] = 
{
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
byte rowPins[ROWS] = {31,33,35,37}; 
byte colPins[COLS] = {39,41,43}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd(19,18,17,16,15,14);

const int ledpinG1 = 13;
 const int ledpinY1 = 12;
 const int ledpinR1 = 11;
 const int ledpinG2 = 9;
 const int ledpinR2 = 8;
 
 byte first = 0;
 byte second = 0;
 byte third = 0;
 char key = keypad.getKey();
 
void setup() {
  lcd.begin(16, 2);
  delay(1000);
  
   
 
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
 }

void loop() {
  
    char key = keypad.getKey();
   
     if(key >= '0' && key <= '9'){
      first = key - '0';
 for(int i=first;i>-1;i--){
 digitalWrite(13, HIGH); 
 digitalWrite(8, HIGH);  
 lcd.setCursor(0, 0);
 lcd.print("GREEN");
 lcd.setCursor(0, 1);
 lcd.print("RED");
 lcd.setCursor(10,0);
 lcd.print(i);
 delay(1000);} 
 lcd.clear();
 
    second = key - '0';
 for(int i=second;i>-1;i--){
 digitalWrite(13, LOW);    
 digitalWrite(12, HIGH);
 digitalWrite(8, HIGH);
 lcd.setCursor(0, 0);
 lcd.print("YELLOW");
 lcd.setCursor(0, 1);
 lcd.print("RED");
 lcd.setCursor(10,0);
 lcd.print(i);
 delay(1000);}
 lcd.clear();
 
    third = key - '0';
 for(int i=third;i>-1;i--){
 digitalWrite(12, LOW);
 digitalWrite(8, LOW);  
 digitalWrite(11, HIGH);
 digitalWrite(9,HIGH);  
 lcd.setCursor(0, 0);
 lcd.print("RED");
 lcd.setCursor(0, 1);
 lcd.print("GREEN");
 lcd.setCursor(10,0);
 lcd.print(i);
 delay(1000);}
 lcd.clear();
 digitalWrite(11, LOW);
 digitalWrite(9, LOW); 

   

     

}}

I can't tell from the post, or from the code, exactly what this sketch is supposed to do. Intuitively, though, it looks like this might be the problem:

The sketch gets a key from the keypad at the beginning of loop(), and then uses that same key two more times, until loop() starts again. Is that what you wanted, or did you want to get more than one keystroke?

tmd3:
I can't tell from the post, or from the code, exactly what this sketch is supposed to do. Intuitively, though, it looks like this might be the problem:

The sketch gets a key from the keypad at the beginning of loop(), and then uses that same key two more times, until loop() starts again. Is that what you wanted, or did you want to get more than one keystroke?

i need to get different digits from keypad as in first when i type '5' for green led it counts down then in sequence type another digit as '9' for yellow it counts down then in sequence type another digit as '7' for red it counts down

but my code makes the first pressed key to all leds and i need to enter for each one.

Look at your sketch. At the beginning of loop(), you fetch a keystroke, here:

void loop() {    char key = keypad.getKey();

After that, the sketch executes two more lighting cycles, but it never fetches another keystroke. Whenever variable key is referenced, its value is the value that was assigned in the statement at the beginning of loop(), shown above.

I can't be sure, but I think that you want to fetch anther keystroke before the second lighting cycle, and yet another keystroke before the third cycle.

[Edit: spelling]

You got the same homework as this guy?

traffic light assignment