if you need individual LED control with as few pins as possible, you should use charliplexing. Most MCUs don't support the tri-state mode but it should still give you a very reasonable pin count (15 rather than 30). the only downside is you can't have both LEDs on at the same time, so if you want to have both of them on at the same time, a trick is to control with PWM and use a smaller value resistor, so the brightness looks the same.
About coding, you can do a fake "real time timer" by creating variables. I haven't written code for STM32 but the general idea is the same. you create a variable which keeps the time and you use multiple if statements, here is an example from arduino:
unsigned long lastMillis;
int i=0;
void setup() {
pinMode(2,INPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(LED_BUILTIN,OUTPUT);
Serial.begin(9600);
lastMillis=millis();
}
void loop() {
if ((millis() -lastMillis) > 5000){
i++;
lastMillis=millis();
if (i==1){
digitalWrite(3,1);
digitalWrite(4,0);
Serial.println("3 is on");
} else if(i==2) {
digitalWrite(3,0);
digitalWrite(4,1);
Serial.println("4 is on");
} else if(i==3) {
digitalWrite(3,1);
digitalWrite(4,1);
Serial.println("3 and 4 are on");
} else {
digitalWrite(3,0);
digitalWrite(4,0);
i=0;
Serial.println("reset");
}
}
digitalWrite(LED_BUILTIN,digitalRead(2));
}
it's a psudo state machine, where "i" is the state and at each state the digital outputs change. this allows the line outside the "timer" (digitalWrite(LED_BUILTIN,digitalRead(2))
to be updated at maximum frequency (you can test it by connecting the digital pin 2, to a high or low state; that should update the LED status "instantly"), while the state update is "delayed" for 5 seconds. I'm sure you can convert it to work with STM32. A lot of details are missing from your project description, but I hope this helped
The timing accuracy will be a bit off here, but I don't think you require precision timing for driving LEDs anyways.