Blinking LEDs misbehave: why?

Please, help me understand what is wrong with my program. I am experimenting with LEDs again (sounds like something a mum would tell a kid not to do, doesn't is).

I want my program to cycle through some pre-programmed blinking patterns at the press of a button. Button 1 cycles one way and button 2 cycles the other way. I only have 2 patterns, but I plan to make more when the problems will be fixed. The cycling routine is OK, I've already debugged it on the serial monitor. The two patterns are fine, each one on it's own. But, when I put everything together, the LEDs don't blink the way they are supposed to do: they will only blink right the first time I press a button.

This is the sketch. I have a feeling this has something to do with my crude debouncing implementation, but I don't know exactly what is happening.

#define RED    9
#define GREEN  8
#define YELLOW 10

#define BUT1   7
#define BUT2   6


byte leds[3] = {RED, GREEN, YELLOW};


void setup()
{

  for (byte n = 0; n <= 3; n++)
  {
    pinMode(leds[n], OUTPUT);
    digitalWrite(leds[n], 1);
  } 

  pinMode(BUT1, INPUT_PULLUP);
  pinMode(BUT2, INPUT_PULLUP);
}

const byte number_of_programs = 2; // <- update me


void loop()
{ 
  switch ( poll_buttons() )
  {
    case 1:
    program1();
    break;
        
    case 2:
    program2();
    break;
    
   } // end switch
    
} //end loop

byte poll_buttons()
{
  static unsigned int counter = 0;
  if (digitalRead(BUT1) == LOW)
  {
    delay(10);
    if (digitalRead(BUT1) == HIGH)
    {
      counter = counter == number_of_programs ? 1 : counter + 1;
    }
  } // end BUT1
  
  if (digitalRead(BUT2) == LOW)
  {
    delay(10);
    if (digitalRead(BUT2) == HIGH)
    {
      counter = counter < 1 ? number_of_programs : counter - 1;
    }
  } // end BUT 2
  
  return counter;
} // end function


void blink_red(unsigned int period)
{
  static boolean on_or_off = 1;
  static unsigned long elapsed;
  if ( (millis() - elapsed) > period )
  {
    elapsed = millis();
    on_or_off = !on_or_off;
  } // end if
  digitalWrite(RED, on_or_off);
}

void blink_green(unsigned int period)
{
  static boolean on_or_off = 1;
  static unsigned long elapsed;
  if ( (millis() - elapsed) > period )
  {
    elapsed = millis();
    on_or_off = !on_or_off;
  } //end if
  digitalWrite(GREEN, on_or_off);
}

void blink_yellow(unsigned int period)
{
  static boolean on_or_off = 1;
  static unsigned long elapsed;
  if ( (millis() - elapsed) > period )
  {
    elapsed = millis();
    on_or_off = !on_or_off;
  }
  digitalWrite(YELLOW, on_or_off);
}

// OK, let's define some blinking programs

void program1()
{
  blink_red(200);
  blink_yellow(250);
  blink_green(300);
}

void program2()
{
  blink_red(350);
  blink_yellow(350);
  blink_green(350);
}
for (byte n = 0; n <= 3

Oops.

AWOL:

for (byte n = 0; n <= 3

Oops.

Right, that's supposed to be n < 3, but it still doesn't work...

I stop reading at the first boo-boo and wait for it to be fixed.

AWOL:
I stop reading at the first boo-boo and wait for it to be fixed.

What was that comment for? It's not like I'm insisting on YOU fixing my program, is it? If you find my questions uninteresting, why don't you just ignore them and carry on with whatever you were doing?

void blink_red(unsigned int period)
{
  static boolean on_or_off = 1;
  static unsigned long elapsed;
  if ( (millis() - elapsed) > period )
  {
    elapsed = millis();
    on_or_off = !on_or_off;
  } // end if
  digitalWrite(RED, on_or_off);
}

each first! time you want to call this code you need to Set a new "elapsed" time.

so when BUT1 == HIGH set a new "elapsed" time.
or when poll_buttons() != then prev_poll_buttons.

The comment was meant to convey the fact that I stop reading code at the first obvious mistake, and leave it to the OP to fix that mistake and retest, and report back observations.

C, in case you didn't know, allows you to write to stuff you don't own, and when you do that, all other bets about how your code will or won't behave are off.

oww woops never mind i allready see you do that

elapsed = millis();

AWOL:
The comment was meant to convey the fact that I stop reading code at the first obvious mistake, and leave it to the OP to fix that mistake and retest, and report back observations.

C, in case you didn't know, allows you to write to stuff you don't own, and when you do that, all other bets about how your code will or won't behave are off.

You can choose any rule you like for reading or not reading code but, if one should follow your radical stance, one should stop reading a book at the first typo: doesn't make a lot of sense, does it? A typo it was, and after I fixed it, the problem was exactly the same, so that's not what was causing it. I don't know if saying that the problem was the same after I made the correction (read my 2nd post) counts as "reporting back" by your quite unyielding standards, and frankly, finding out exactly what your standards are is not one of my priorities at the moment.

Of course, should you decide to be more helpful, any hint will be more than welcome. Or, if for any reason you don't want to help, then just don't, as I have already said.

Your analogy is a very poor one - books don't self-destruct when I read a typo, and reading a typo doesn't affect how the rest of the book functions.

It may have been a typo, but if you can tell me the effects it would've had when executed, you're a more accomplished AVR programmer than I previously gave you credit for.

I'm out now.

Good luck.