LED matrix control using arrays and state machine?

OK tested. With some obvious errors and some not-so-obvious errors.

This was harder to get working than I expected, a certain sign of sleep deprivation and bad no design ahead of coding…

The animation runs as long as the button is pressed. The animation completes itself if the button is released within the sequence.

void setup() {
  Serial.begin(115200);
  Serial.println("Hello World!");

  pinMode(3, INPUT_PULLUP);

  turnMachineStep(2);   // leave it as if it had run once? I give up
}

void loop() {
  if (!turnMachineStep(1) and !digitalRead(3)) {
  	turnMachineStep(0);
  }
}

// command 0 - reset this machine.
// command 1 - normal step this machine, return 0 when done/no longer busy
// command 2 - initialise this machine - mystery? kludged here

unsigned char turnMachineStep(unsigned char command)
{
  static byte frame = 0;
  static unsigned long lastTime = 0;
  static unsigned long tween = 1;

  static unsigned char busy = 0;        // not busy!
  static unsigned char returnValue = 0; // not done - will change if not

// I give up, pound a nail in it.

  if (command == 2) {
      tween = 1;
      frame = 0;

      returnValue = 1;	// we done
      busy = 0;				// we not
 //     initialised = true;
      return returnValue;
  }

 if (command == 0) {
    frame = 0;
    lastTime = millis();
    tween = 200;		// next call gets immediate enough attention.
    busy = 1;

    return (1);
  }

  if (!busy) 
    return (0);

  if (millis() - lastTime < tween)
    return (1);

  lastTime = millis();

  switch (frame) {
    case 0 :
      Serial.println("               render frame 0");
      tween = 200;
      frame++;
      break;

    case 1 ... 8 : /* case 1 - 21 elided here */
      Serial.print("               render frame ");
      Serial.println(frame);
      tween = 200;
      frame++;
      break;


    case 9 :
      Serial.println("               render frame 9. Done?");
      tween = 15;
      frame = 0;

      returnValue = 1;		// we done
      busy = 0;				// we not busy
      break;
  }

  return returnValue;
}

Into the wokwi:

Sry, this is a kludge on top of a kluge.

FWIW Wakeman brought it, once again.

a7