Need help! New to coding Frankensteined this together

Thanks in advance to anyone who can help me. I am relatively new to coding but have managed to frankenstein this together with some help from others codes. I know enough to know this is not the best way to do this but not how to do it better. I have gotten everything to work so far except the timer. I am using an ht16k33 backpack on a 4digit 7 segment display.

Thanks again,
Jare

BattleBot-Arena4.ino (12 KB)

#include <Adafruit_NeoPixel.h>

/*  PINOUT  */
const uint8_t LIGHT_PIN = 22;
const uint8_t DOOR_MOTOR_PIN = 5;
const uint8_t DOOR2_PIN = 6;
const uint8_t DOOR1_PIN = 7;
const uint8_t BUTTON_1_PIN = 8;
const uint8_t BUTTON_2_PIN = 9;
const uint8_t BUTTON_3_PIN = 10;
const uint8_t DOOR_SENSOR_1 = 11;
const uint8_t DOOR_SENSOR_2 = 13;
const uint8_t AUDIO_0 = 25;
const uint8_t AUDIO_1 = 27;
const uint8_t AUDIO_2 = 29;
const uint8_t AUDIO_3 = 31;
const uint8_t AUDIO_4 = 33;
const uint8_t AUDIO_5 = 35;
const uint8_t AUDIO_6 = 37;
const uint8_t AUDIO_7 = 39;
const uint8_t AUDIO_BUSY = 41;

// Player states
bool player_1_ready = false;
bool player_2_ready = false;
bool judges_ready = false;

// Round Stuff
const long ROUND_DELAY =   5000;  //5 seconds
const long ROUND_LENGTH =  180000; //3 Minutes
const long PIT_TIME = 120000; //2 Minutes
long current_round_time = 0;

// Door Stuff
bool door_1_open = true;
bool door_2_open = true;

// Light Stuff
const uint8_t STRIP_SIZE = 50;
typedef uint32_t Color;

//Timer Stuff
#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_LEDBackpack.h"
Adafruit_7segment matrix = Adafruit_7segment();
unsigned long previousSecondMillis = 0UL;
long oneSecond = 1000UL; // milliseconds per second

int minutes = ROUND_LENGTH / 60000 ;
int seconds = 0;
bool blinkColon = false;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIP_SIZE, LIGHT_PIN, NEO_BGR + NEO_KHZ800);
Color RED = strip.Color(0, 255, 0);
Color YELLOW = strip.Color(255, 255, 0);
Color GREEN = strip.Color(255, 0, 0);
Color WHITE = strip.Color(255, 255, 255);
Color BLUE = strip.Color(0, 0, 255);
Color BLACK = strip.Color(0, 0, 0);

bool checkPlayer(const uint8_t PIN) {
  if (digitalRead(PIN) == LOW) {
    if (PIN == BUTTON_1_PIN) {
      Serial.println("Player 1 ready");
      strip.fill(RED, 0, STRIP_SIZE / 2);
      digitalWrite(AUDIO_1, LOW);
      delay(100);
      digitalWrite(AUDIO_1, HIGH);
    } else {
      if (PIN == BUTTON_2_PIN) {
        Serial.println("Player 2 ready");
        strip.fill(BLUE, STRIP_SIZE / 2, STRIP_SIZE / 2);
        digitalWrite(AUDIO_0, LOW);
        delay(100);
        digitalWrite(AUDIO_0, HIGH);
      }
    }
    strip.show();
    return true;
  }
  return false;
}
bool closeTrapDoor(const uint8_t door, const uint8_t sensor, bool& door_open) {
  if (!door_open) {
    return true;
  }
  if (digitalRead(sensor) == LOW) {
    Serial.print(sensor);
    Serial.println(" trap door closed.");
    // It just closed
    delay(10);
    digitalWrite(door, HIGH);
    delay(100);
    digitalWrite(door, LOW);
    door_open = false;
  }
}
void closeTrapDoors() {
  while (door_1_open || door_2_open) {
    Serial.println("Wainting for doors to close");
    closeTrapDoor(DOOR2_PIN, DOOR_SENSOR_2, door_2_open);
    closeTrapDoor(DOOR1_PIN, DOOR_SENSOR_1, door_1_open);
  }
}
void setup() {
  pinMode(LIGHT_PIN, OUTPUT);
  pinMode(DOOR_MOTOR_PIN, OUTPUT);
  pinMode(DOOR2_PIN, OUTPUT);
  pinMode(DOOR1_PIN, OUTPUT);
  pinMode(BUTTON_1_PIN, INPUT_PULLUP);
  pinMode(BUTTON_2_PIN, INPUT_PULLUP);
  pinMode(DOOR_SENSOR_1, INPUT_PULLUP);
  pinMode(DOOR_SENSOR_2, INPUT_PULLUP);
  pinMode(AUDIO_0, OUTPUT);
  pinMode(AUDIO_1, OUTPUT);
  pinMode(AUDIO_2, OUTPUT);
  pinMode(AUDIO_3, OUTPUT);
  pinMode(AUDIO_4, OUTPUT);
  pinMode(AUDIO_5, OUTPUT);
  pinMode(AUDIO_6, OUTPUT);
  pinMode(AUDIO_7, OUTPUT);

  digitalWrite(DOOR_MOTOR_PIN, LOW);
  digitalWrite(DOOR1_PIN, LOW);
  digitalWrite(DOOR2_PIN, LOW);
  digitalWrite(AUDIO_0, HIGH);
  digitalWrite(AUDIO_1, HIGH);
  digitalWrite(AUDIO_2, HIGH);
  digitalWrite(AUDIO_3, HIGH);
  digitalWrite(AUDIO_4, HIGH);
  digitalWrite(AUDIO_5, HIGH);
  digitalWrite(AUDIO_6, HIGH);
  digitalWrite(AUDIO_7, HIGH);
  
  strip.begin();
  strip.show();
  Serial.begin(9600);
  matrix.begin(0x70);
}
bool is_running = false;
struct Action {
  long ts;
  void (*toDo)();
};
long long int matchStartTime = 0;
void update_timer(int s) {
  //add code to parse s into m:ss via string nonsense or some shit
  int so;
  int si;
  int m = s / 60;
  s = s - (m * 60);
  if (s < 10) {
    so = 0;
    si = s;
  } else {
    si = s % 10;
    so = (s / 10) % 10;
  }
  matrix.writeDigitNum(0, (m / 10));
  matrix.writeDigitNum(1, (m % 10));
  matrix.writeDigitNum(3, (so));
  matrix.writeDigitNum(4, (si));
  matrix.blinkRate(0);
  matrix.drawColon(true);
  matrix.writeDisplay();
}
Action matchActions[] = {
  {
    matchStartTime,
    []() {
      Serial.println("Starting Match");
      matrix.writeDigitNum(0, (8));
      matrix.writeDigitNum(1, (8));
      matrix.writeDigitNum(3, (8));
      matrix.writeDigitNum(4, (8));
      matrix.drawColon(true);
      matrix.blinkRate(1);
      matrix.writeDisplay();
    }
  },
  { matchStartTime + 1000,
    []() {
      strip.fill(BLACK, 0, STRIP_SIZE);
      strip.show();
      digitalWrite(AUDIO_0, LOW);               //3,2,1,GO
      digitalWrite(AUDIO_1, LOW);
    }
  },
  { matchStartTime + 2000,
    []() {
      strip.fill(YELLOW, 0, STRIP_SIZE);
      strip.show();
      matrix.writeDigitNum(0, (3));
      matrix.writeDigitNum(1, (3));
      matrix.writeDigitNum(3, (3));
      matrix.writeDigitNum(4, (3));
      matrix.drawColon(false);
      matrix.blinkRate(0);
      matrix.writeDisplay();
      Serial.println("3");
    }
  },
  { matchStartTime + 2500,
    []() {
      strip.fill(BLACK, 0, STRIP_SIZE);
      strip.show();
    }
  },
  { matchStartTime + 3000,
    []() {
      strip.fill(YELLOW, 0, STRIP_SIZE);
      strip.show();
      matrix.writeDigitNum(0, (2));
      matrix.writeDigitNum(1, (2));
      matrix.writeDigitNum(3, (2));
      matrix.writeDigitNum(4, (2));
      matrix.blinkRate(0);
      matrix.writeDisplay();
      Serial.println("2");
    }
  },
  { matchStartTime + 3500,
    []() {
      strip.fill(BLACK, 0, STRIP_SIZE);
      strip.show();
    }
  },
  { matchStartTime + 4000,
    []() {
      strip.fill(YELLOW, 0, STRIP_SIZE);
      strip.show();
      matrix.writeDigitNum(0, (1));
      matrix.writeDigitNum(1, (1));
      matrix.writeDigitNum(3, (1));
      matrix.writeDigitNum(4, (1));
      matrix.blinkRate(0);
      matrix.writeDisplay();
      Serial.println("1");
    }
  },
  { matchStartTime + 4500,
    []() {
      strip.fill(BLACK, 0, STRIP_SIZE);
      strip.show();
      digitalWrite(AUDIO_0, HIGH);
      digitalWrite(AUDIO_1, HIGH);
    }
  },
  { matchStartTime + 5000,
    []() {
      strip.fill(GREEN, 0, STRIP_SIZE);
      strip.show();
      matrix.writeDigitNum(0, (0));
      matrix.writeDigitNum(1, (0));
      matrix.writeDigitNum(3, (0));
      matrix.writeDigitNum(4, (0));
      matrix.blinkRate(1);
      matrix.writeDisplay();
      Serial.println("GO!");
    }
  },
  { matchStartTime + 6000,
    []() {
      strip.fill(WHITE, 0, STRIP_SIZE);
      strip.show();
    }
  },
  {
    PIT_TIME - 10000,
    []() {
      strip.fill(YELLOW, 0, 5);
      strip.show();
      Serial.println("Pit countdown");
    }
  },
  {PIT_TIME - 9000,
    []() {
      strip.fill(YELLOW, 0, 10);
      strip.show();
      Serial.println("9");
    }
  },
  {PIT_TIME - 8000,
    []() {
      strip.fill(YELLOW, 0, 15);
      strip.show();
      Serial.println("8");
    }
  },
  {PIT_TIME - 7000,
    []() {
      strip.fill(YELLOW, 0, 20);
      strip.show();
      Serial.println("7");
    }
  },
  {PIT_TIME - 6000,
    []() {
      strip.fill(YELLOW, 0, 25);
      strip.show();
      Serial.println("6");
    }
  },
  {PIT_TIME - 5000,
    []() {
      strip.fill(YELLOW, 0, 30);
      strip.show();
      Serial.println("5");
    }
  },
  {PIT_TIME - 4000,
    []() {
      strip.fill(YELLOW, 0, 35);
      strip.show();
      Serial.println("4");
    }
  },
  {PIT_TIME - 3000,
    []() {
      strip.fill(YELLOW, 0, 40);
      strip.show();
      Serial.println("3");
    }
  },
  {PIT_TIME - 2000,
    []() {
      strip.fill(YELLOW, 0, 45);
      strip.show();
      Serial.println("2");
    }
  },
  {PIT_TIME - 1000,
    []() {
      strip.fill(YELLOW, 0, 50);
      strip.show();
      Serial.println("1");
    }
  },
  {PIT_TIME,
    []() {
      door_1_open = true;
      door_2_open = true;
      strip.fill(WHITE, 0, STRIP_SIZE);
      strip.show();
      Serial.println("Open Trap Doors");
      digitalWrite(DOOR_MOTOR_PIN, HIGH); // Energise Door
    }
  },
  {PIT_TIME + 200,
    []() {
      digitalWrite(DOOR_MOTOR_PIN, LOW); // Turn off door
    }
  },
  {ROUND_LENGTH - 10000,
    []() {
      strip.fill(RED, 0, 1);
      strip.show();
      Serial.println("End of Match countdown");
    }
  },
  {ROUND_LENGTH - 9000,
    []() {
      strip.fill(RED, 0, 5);
      strip.show();
      Serial.println("9");
    }
  },
  {ROUND_LENGTH - 8000,
    []() {
      strip.fill(RED, 0, 10);
      strip.show();
      Serial.println("8");
    }
  },
  {ROUND_LENGTH - 7000,
    []() {
      strip.fill(RED, 0, 15);
      strip.show();
      Serial.println("7");
    }
  },
  {ROUND_LENGTH - 6000,
    []() {
      strip.fill(RED, 0, 20);
      strip.show();
      Serial.println("6");
    }
  },
  {ROUND_LENGTH - 5000,
    []() {
      strip.fill(RED, 0, 25);
      strip.show();
      Serial.println("5");
    }
  },
  {ROUND_LENGTH - 4000,
    []() {
      strip.fill(RED, 0, 30);
      strip.show();
      Serial.println("4");
    }
  },
  {ROUND_LENGTH - 3000,
    []() {
      strip.fill(RED, 0, 35);
      strip.show();
      Serial.println("3");
    }
  },
  {ROUND_LENGTH - 2000,
    []() {
      strip.fill(RED, 0, 40);
      strip.show();
      Serial.println("2");
    }
  },
  {ROUND_LENGTH - 1000,
    []() {
      strip.fill(RED, 0, 45);
      strip.show();
      Serial.println("1");
    }
  },
  {ROUND_LENGTH,
    []() {
      strip.fill(RED, 0, STRIP_SIZE);
      strip.show();
      Serial.println("Round Over");
    }
  },
};
const size_t itemCount = sizeof(matchActions) / sizeof(Action);
size_t actionIndex = 0;
bool player1Ready = false;
bool player2Ready = false;
bool judgesReady = false;
void loop() {
  if (!is_running) {
    if (door_1_open || door_2_open) {
      strip.fill(RED, 0, STRIP_SIZE);
      strip.show();
      closeTrapDoors(); //Ensure doors are closed before game begins.
      return;
    }
    if (!player1Ready && !player2Ready) {
      strip.fill(GREEN, 0, STRIP_SIZE);
      strip.show();
    }
    if (!player1Ready) {
      player1Ready = checkPlayer(BUTTON_1_PIN);
    }
    if (!player2Ready) {
      player2Ready = checkPlayer(BUTTON_2_PIN);
    }
    if (!judgesReady) {
      judgesReady = checkPlayer(BUTTON_3_PIN);
    }
    if (player1Ready && player2Ready && judgesReady) {
      matchStartTime = millis();
      is_running = true;
      actionIndex = 0;
      Serial.println("Everyone is ready");
    }
  } else {
    if (actionIndex >= itemCount) {
      is_running = false;
      player1Ready = false;
      player2Ready = false;
      judgesReady = false;
      return;
    }
    long long elapsedTime = millis() - matchStartTime;
    while (actionIndex < itemCount && elapsedTime >= matchActions[actionIndex].ts) {
      matchActions[actionIndex].toDo();
      ++actionIndex;
    }
  }
}

jareball:
I have gotten everything to work so far except the timer.

You need to provide enough information so we can focus our attention on the specific problem. Reading through 12k of code and trying to figure out what it all does (or does not do) is very time consuming.

Ideally create a short program to illustrate the problem.

...R