Joystick SW cancels rest of the program

Hello, I am working on a binary clock, but the Joystick doesn't work properly. Clicking the joystick is supposed to switch to a mode in which you can select a time when you would like to make your alarm go off. We tried several tests like a plain sketch that reads out the SW value, this worked fine. For some reason the full code deosn't like us clicking the joystick, because if we do so, the rest of the code stops. The whole clock stops until I let go of the joystick button.
We are using an Arduino Mega 2560 and a KY-023 Joystick. Let me know if I need to give more information or wheteher you know how to fix this.

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
//#include <pitches.h>

bool startingTime = true;

const int triggerLeft = 200;
const int triggerRight = 823;
const int triggerUp = 200;
const int triggerDown = 823;

bool snooze = false;
bool alarmBeep = false;
bool alarmOn = false;
bool alarmSet = false;
bool TimeSet = false;
bool pulseTS = false;
bool ampm = true; // true = AM and false = PM
bool pulseMT = false;
bool pulseDST = false;
bool militaryTime = true;
bool DST = true; // If uploading program, change this after Spring (true) or Autumn (false).

// [Keanu] this makes buttons responsive by putting a 1 sec cooldown on pressing a button.
int cooldownAO = 0;
int cooldownAB = 0;
int cooldownTS = 0;
int cooldownMT = 0;
int cooldownDST = 0;
int cooldownAP = 0;
int cooldownX = 0;
int cooldownY = 0;

int AlarmHrs10 = 0;
int AlarmHrs1 = 0;
int AlarmMin10 = 0;
int AlarmMin1 = 0;

int SelHrs10 = 0;
int SelHrs1 = 0;
int SelMin10 = 0;
int SelMin1 = 0;

int SelTimeInd = 1;
int cooldownBlink = 50;
bool blinkOff = false;
bool HrsChange = false;

int CurHrs10 = 0;
int CurHrs1 = 0;
int CurMin10 = 0;
int CurMin1 = 0;
int CurSec10 = 0;
int CurSec1 = 0;

int switch10 = 0;

int SelCurHrs10 = 0;
int SelCurHrs1 = 0;
int SelCurMin10 = 0;
int SelCurMin1 = 0;
int SelCurSec10 = 0;
int SelCurSec1 = 0;

void setup() {
  Serial.begin(9600);

  pinMode(21, INPUT_PULLUP); digitalWrite(21, 1);
  pinMode(22, OUTPUT); digitalWrite(22, 0); // G1
  pinMode(23, OUTPUT); digitalWrite(23, 0); // G2
  pinMode(24, OUTPUT); digitalWrite(24, 0); // G4
  pinMode(25, OUTPUT); digitalWrite(25, 0); // G8
  pinMode(26, OUTPUT); digitalWrite(26, 0); // G10
  pinMode(27, OUTPUT); digitalWrite(27, 0); // G20
  pinMode(28, OUTPUT); digitalWrite(28, 0); // G40
  pinMode(33, OUTPUT); digitalWrite(33, 0); // B1
  pinMode(34, OUTPUT); digitalWrite(34, 0); // B2
  pinMode(35, OUTPUT); digitalWrite(35, 0); // B4
  pinMode(36, OUTPUT); digitalWrite(36, 0); // B8
  pinMode(37, OUTPUT); digitalWrite(37, 0); // B10
  pinMode(38, OUTPUT); digitalWrite(38, 0); // B20
  pinMode(39, OUTPUT); digitalWrite(39, 0); // B40
  pinMode(42, OUTPUT); digitalWrite(42, 0); // R1
  pinMode(41, OUTPUT); digitalWrite(41, 0); // R2 (Was 43!)
  pinMode(44, OUTPUT); digitalWrite(44, 0); // R4
  pinMode(45, OUTPUT); digitalWrite(45, 0); // R8
  pinMode(46, OUTPUT); digitalWrite(46, 0); // R10
  pinMode(47, OUTPUT); digitalWrite(47, 0); // R20
  pinMode(50, OUTPUT); digitalWrite(50, 0); // AM
  pinMode(51, OUTPUT); digitalWrite(51, 0); // PM
  pinMode(52, OUTPUT); digitalWrite(52, 0); // Alarm

}

void loop() {
  while (TimeSet == true) {

    tmElements_t tm;
    if (RTC.read(tm) and startingTime == true) {
      CurHrs10 = (tm.Hour / 10U) % 10;
      CurHrs1 = (tm.Hour / 1U) % 10;
      CurMin10 = (tm.Minute / 10U) % 10;
      CurMin1 = (tm.Minute / 1U) % 10;
      CurSec10 = (tm.Second / 10U) % 10;
      CurSec1 = (tm.Second / 1U) % 10;

      Serial.print(CurHrs10);
      Serial.print(CurHrs1);
      Serial.print(":");
      Serial.print(CurMin10);
      Serial.print(CurMin1);
      Serial.print(":");
      Serial.print(CurSec10);
      Serial.println(CurSec1);
      startingTime = false;
    }
    else if (RTC.read(tm) and startingTime == false) {
      CurSec1 = (tm.Second / 1U) % 10;

      if (CurSec1 == 9) {
        switch10 = true;
      }
      if (switch10 == true and CurSec1 == 0) {
        CurSec10++;
        switch10 = false;
      }
    }

    // Deactivates time set pulse.
    if (pulseTS == true) {
      pulseTS = false;
    }

    // If button is pressed, exit setting alarm.
    if (digitalRead(21) == 0 and cooldownTS == 0) {
      TimeSet = false;
      pulseTS = true;
      cooldownTS = 25;
    }

    if (militaryTime == true) {
      MilitaryRules();
    }

    if (militaryTime == false) {
      nonMilitaryRules();
    }

    // If joystick is steered left, move one integer to the left.
    if (analogRead(A0) < triggerLeft and cooldownX == 0) {
      SelTimeInd--;
      cooldownX = 25;
    }

    // If joystick is steered right, move one integer to the right.
    if (analogRead(A0) > triggerRight and cooldownX == 0) {
      SelTimeInd++;
      cooldownX = 25;
    }

    // If integer reaches 0, go to integer 4.
    if (SelTimeInd == 0) {
      SelTimeInd = 4;
    }
    // If integer reaches 5, go to integer 1.
    if (SelTimeInd == 5) {
      SelTimeInd = 1;
    }

    if (analogRead(A1) < triggerUp and cooldownY == 0) {
      switch (SelTimeInd) {
        case 1:
          SelHrs10++;
          HrsChange = true;
          break;
        case 2:
          SelHrs1++;
          break;
        case 3:
          SelMin10++;
          break;
        case 4:
          SelMin1++;
          break;
      }
      cooldownY = 25;
    }

    if (analogRead(A1) > triggerDown and cooldownY == 0) {
      switch (SelTimeInd) {
        case 1:
          SelHrs10--;
          HrsChange = true;
          break;
        case 2:
          SelHrs1--;
          break;
        case 3:
          SelMin10--;
          break;
        case 4:
          SelMin1--;
          break;
      }
      cooldownY = 25;
    }

    if (SelHrs10 == 3) {
      SelHrs10 = 0;
    }
    if (SelHrs10 == -1) {
      SelHrs10 = 2;
    }


    // If the 10's of the hours are changing, force the 1's to go back to 0 to prevent hours above 24.
    if (HrsChange == true) {
      SelHrs1 = 0;
      HrsChange = false;
    }

    // In case of 00:00 or 10:00, allow the 1's to go to 9.
    if (SelHrs10 == 0 or SelHrs10 == 1) {
      // If 10 hours are reached, reset to 0 hours.
      if (SelHrs1 == 10) {
        SelHrs1 = 0;
      }
      if (SelHrs1 == -1) {
        SelHrs1 = 9;
      }
    }

    // in case of 20:00, allow the 1's to go to 5.
    if (SelHrs10 == 2) {
      // If 5 hours are reached, reset to 0 hours.
      if (SelHrs1 == 4) {
        SelHrs1 = 0;
      }
      if (SelHrs1 == -1) {
        SelHrs1 = 3;
      }
    }

    // If 60 minutes are reached, reset to 0 minutes.
    if (SelMin10 == 6) {
      SelMin10 = 0;
    }
    if (SelMin10 == -1) {
      SelMin10 = 5;
    }

    // If 10 minutes are reached, reset to 0.
    if (SelMin1 == 10) {
      SelMin1 = 0;
    }
    if (SelMin1 == -1) {
      SelMin1 = 9;
    }

    Serial.print("Alarm: ");
    Serial.print(SelHrs10); Serial.print(SelHrs1);
    Serial.print(":");
    Serial.print(SelMin10); Serial.println(SelMin1);

    // Blink every 500ms if not currently selected
    if (SelTimeInd != 1 and blinkOff == true) {
      digitalWrite(46, 0);
      digitalWrite(47, 0);
    }
    else {
      switch (SelHrs10) {
        case 1:
          digitalWrite(46, HIGH);
          digitalWrite(47, 0);
          break;
        case 2:
          digitalWrite(46, 0);
          digitalWrite(47, HIGH);
          break;
        case 0:
          digitalWrite(46, 0);
          digitalWrite(47, 0);
          break;
      }
    }

    if (SelTimeInd != 2 and blinkOff == true) {
      digitalWrite(42, 0);
      digitalWrite(41, 0);
      digitalWrite(44, 0);
      digitalWrite(45, 0);
    }
    else {
      switch (SelHrs1) {
        case 1:
          digitalWrite(42, HIGH);
          digitalWrite(41, 0);
          digitalWrite(44, 0);
          digitalWrite(45, 0);
          break;
        case 2:
          digitalWrite(42, 0);
          digitalWrite(41, HIGH);
          digitalWrite(44, 0);
          digitalWrite(45, 0);
          break;
        case 3:
          digitalWrite(42, HIGH);
          digitalWrite(41, HIGH);
          digitalWrite(44, 0);
          digitalWrite(45, 0);
          break;
        case 4:
          digitalWrite(42, 0);
          digitalWrite(41, 0);
          digitalWrite(44, HIGH);
          digitalWrite(45, 0);
          break;
        case 5:
          digitalWrite(42, HIGH);
          digitalWrite(41, 0);
          digitalWrite(44, HIGH);
          digitalWrite(45, 0);
          break;
        case 6:
          digitalWrite(42, 0);
          digitalWrite(41, HIGH);
          digitalWrite(44, HIGH);
          digitalWrite(45, 0);
          break;
        case 7:
          digitalWrite(42, HIGH);
          digitalWrite(41, HIGH);
          digitalWrite(44, HIGH);
          digitalWrite(45, 0);
          break;
        case 8:
          digitalWrite(42, 0);
          digitalWrite(41, 0);
          digitalWrite(44, 0);
          digitalWrite(45, HIGH);
          break;
        case 9:
          digitalWrite(42, HIGH);
          digitalWrite(41, 0);
          digitalWrite(44, 0);
          digitalWrite(45, HIGH);
          break;
        case 0:
          digitalWrite(42, 0);
          digitalWrite(41, 0);
          digitalWrite(44, 0);
          digitalWrite(45, 0);
          break;
      }
    }

    if (SelTimeInd != 3 and blinkOff == true) {
      digitalWrite(37, 0);
      digitalWrite(38, 0);
      digitalWrite(39, 0);
    }
    else {
      switch (SelMin10) {
        case 1:
          digitalWrite(37, HIGH);
          digitalWrite(38, 0);
          digitalWrite(39, 0);
          break;
        case 2:
          digitalWrite(37, 0);
          digitalWrite(38, HIGH);
          digitalWrite(39, 0);
          break;
        case 3:
          digitalWrite(37, HIGH);
          digitalWrite(38, HIGH);
          digitalWrite(39, 0);
          break;
        case 4:
          digitalWrite(37, 0);
          digitalWrite(38, 0);
          digitalWrite(39, HIGH);
          break;
        case 5:
          digitalWrite(37, HIGH);
          digitalWrite(38, 0);
          digitalWrite(39, HIGH);
          break;
        case 0:
          digitalWrite(37, 0);
          digitalWrite(38, 0);
          digitalWrite(39, 0);
          break;
      }
    }

    if (SelTimeInd != 4 and blinkOff == true) {
      digitalWrite(33, 0);
      digitalWrite(34, 0);
      digitalWrite(35, 0);
      digitalWrite(36, 0);
    }
    else {
      switch (SelMin1) {
        case 1:
          digitalWrite(33, HIGH);
          digitalWrite(34, 0);
          digitalWrite(35, 0);
          digitalWrite(36, 0);
          break;
        case 2:
          digitalWrite(33, 0);
          digitalWrite(34, HIGH);
          digitalWrite(35, 0);
          digitalWrite(36, 0);
          break;
        case 3:
          digitalWrite(33, HIGH);
          digitalWrite(34, HIGH);
          digitalWrite(35, 0);
          digitalWrite(36, 0);
          break;
        case 4:
          digitalWrite(33, 0);
          digitalWrite(34, 0);
          digitalWrite(35, HIGH);
          digitalWrite(36, 0);
          break;
        case 5:
          digitalWrite(33, HIGH);
          digitalWrite(34, 0);
          digitalWrite(35, HIGH);
          digitalWrite(36, 0);
          break;
        case 6:
          digitalWrite(33, 0);
          digitalWrite(34, HIGH);
          digitalWrite(35, HIGH);
          digitalWrite(36, 0);
          break;
        case 7:
          digitalWrite(33, HIGH);
          digitalWrite(34, HIGH);
          digitalWrite(35, HIGH);
          digitalWrite(36, 0);
          break;
        case 8:
          digitalWrite(33, 0);
          digitalWrite(34, 0);
          digitalWrite(35, 0);
          digitalWrite(36, HIGH);
          break;
        case 9:
          digitalWrite(33, HIGH);
          digitalWrite(34, 0);
          digitalWrite(35, 0);
          digitalWrite(36, HIGH);
          break;
        case 0:
          digitalWrite(33, 0);
          digitalWrite(34, 0);
          digitalWrite(35, 0);
          digitalWrite(36, 0);
          break;
      }
    }

    // Turn off seconds LEDs
    digitalWrite(22, 0);
    digitalWrite(23, 0);
    digitalWrite(24, 0);
    digitalWrite(25, 0);
    digitalWrite(26, 0);
    digitalWrite(27, 0);
    digitalWrite(28, 0);

    if (cooldownTS > 0) {
      cooldownTS--;
    }
    if (cooldownX > 0) {
      cooldownX--;
    }
    if (cooldownY > 0) {
      cooldownY--;
    }
    if (cooldownAO > 0) {
      cooldownAO--;
    }
    if (cooldownBlink > 0) {
      cooldownBlink--;
    }
    if (cooldownBlink == 0) {
      blinkOff = !blinkOff;
      cooldownBlink = 50;
    }

    delay(10);
  }

  while (TimeSet == false) {
    tmElements_t tm;
    if (RTC.read(tm) and startingTime == true) {
      CurHrs10 = (tm.Hour / 10U) % 10;
      CurHrs1 = (tm.Hour / 1U) % 10;
      CurMin10 = (tm.Minute / 10U) % 10;
      CurMin1 = (tm.Minute / 1U) % 10;
      CurSec10 = (tm.Second / 10U) % 10;
      CurSec1 = (tm.Second / 1U) % 10;

      Serial.print(CurHrs10);
      Serial.print(CurHrs1);
      Serial.print(":");
      Serial.print(CurMin10);
      Serial.print(CurMin1);
      Serial.print(":");
      Serial.print(CurSec10);
      Serial.println(CurSec1);
      startingTime = false;
    }
    else if (RTC.read(tm) and startingTime == false) {
      CurSec1 = (tm.Second / 1U) % 10;

      if (CurSec1 == 9) {
        switch10 = true;
      }
      if (switch10 == true and CurSec1 == 0) {
        CurSec10++;
        switch10 = false;
      }
    }

    // Deactivates time set pulse and turns on and updates alarm.
    if (pulseTS == true) {
      pulseTS = false;
      alarmOn = true;
      AlarmHrs10 = SelHrs10;
      AlarmHrs1 = SelHrs1;
      AlarmMin10 = SelMin10;
      AlarmMin1 = SelMin1;
    }
    
    Serial.println(digitalRead(21));
    // If button is pressed, toggle set alarm.
    if (digitalRead(21) == 0 and cooldownTS == 0) {
      Serial.println("good");
      TimeSet = true;
      pulseTS = true;
      cooldownTS = 25;
    }

    // If button is pressed, toggle alarm.
    if (analogRead(A5) == 1023 and cooldownAO == 0) {
      alarmOn = !alarmOn;
      alarmBeep = false;
      cooldownAO = 25;
      Serial.print("Alarm turned ");
      if (alarmOn == true) {
        Serial.println("on.");
      }
      else if (alarmOn == false) {
        Serial.println("off.");
      }
    }

    if (alarmOn == true) {
      digitalWrite(52, HIGH);
    }
    else {
      digitalWrite(52, 0);
    }

    // If button is pressed, toggle military time.
    if (analogRead(A4) == 1023 and cooldownMT == 0) {
      militaryTime = !militaryTime;
      pulseMT = true;
      cooldownMT = 25;
    }

    // If button is pressed, toggle Daylight Saving Time.
    if (analogRead(A3) == 1023 and cooldownDST == 0) {
      DST = !DST;
      pulseDST = true;
      cooldownDST = 25;
    }

    if (militaryTime == true) {
      MilitaryRules();
    }

    if (militaryTime == false) {
      nonMilitaryRules();
    }

    // If the pulse comes in and the Daylight Saving Time is set to true, speed the time up by an hour.
    // If in non-militaryTime and the time is 11:00, speed the time up by an hour and toggle AM or PM.
    if (DST == true and pulseDST == true) {
      if (militaryTime == false and CurHrs10 == 1 and CurHrs1 == 1) {
        CurHrs1++;
        ampm = !ampm;
        pulseDST = false;
      }
      else {
        CurHrs1++;
        pulseDST = false;
      }
    }

    // If the pulse comes in and the Daylight Saving Time is set to false, set the time back by an hour.
    // In case of an exact ten hours or day (10:00, 20:00, 00:00) set the hours back manually by an hour.
    // If the time mode is set to non-military time and it's 1 o' clock (01:00 AM, 01:00 PM) set the time back manually to 12 o' clock.
    if (DST == false and pulseDST == true) {
      if (militaryTime == false and CurHrs10 == 0 and CurHrs1 == 1) {
        CurHrs10 = 1;
        CurHrs1 = 2;
        pulseDST = false;
      }
      else if (militaryTime == false and CurHrs10 == 1 and CurHrs1 == 2) {
        CurHrs10 = 1;
        CurHrs1 = 1;
        ampm = !ampm;
        pulseDST = false;
      }
      else if (CurHrs10 == 0 and CurHrs1 == 0) {
        CurHrs10 = 2;
        CurHrs1 = 3;
        pulseDST = false;
      }
      else if (CurHrs10 == 1 and CurHrs1 == 0) {
        CurHrs10 = 0;
        CurHrs1 = 9;
        pulseDST = false;
      }
      else if (CurHrs10 == 2 and CurHrs1 == 0) {
        CurHrs10 = 1;
        CurHrs1 = 9;
        pulseDST = false;
      }
      else {
        CurHrs1--;
        pulseDST = false;
      }
    }

    Serial.print("Time: ");

    // Turn on and off the correct lights corresponding to the binary code of the current time for the tens of the hours.
    switch (CurHrs10) {
      case 1:
        Serial.print("1");
        digitalWrite(46, HIGH);
        digitalWrite(47, 0);
        break;
      case 2:
        Serial.print("2");
        digitalWrite(46, 0);
        digitalWrite(47, HIGH);
        break;
      case 0:
        Serial.print("0");
        digitalWrite(46, 0);
        digitalWrite(47, 0);
        break;
    }

    // Turn on and off the correct lights corresponding to the binary code of the current time for the ones of the hours.
    switch (CurHrs1) {
      case 1:
        Serial.print("1");
        digitalWrite(42, HIGH);
        digitalWrite(41, 0);
        digitalWrite(44, 0);
        digitalWrite(45, 0);
        break;
      case 2:
        Serial.print("2");
        digitalWrite(42, 0);
        digitalWrite(41, HIGH);
        digitalWrite(44, 0);
        digitalWrite(45, 0);
        break;
      case 3:
        Serial.print("3");
        digitalWrite(42, HIGH);
        digitalWrite(41, HIGH);
        digitalWrite(44, 0);
        digitalWrite(45, 0);
        break;
      case 4:
        Serial.print("4");
        digitalWrite(42, 0);
        digitalWrite(41, 0);
        digitalWrite(44, HIGH);
        digitalWrite(45, 0);
        break;
      case 5:
        Serial.print("5");
        digitalWrite(42, HIGH);
        digitalWrite(41, 0);
        digitalWrite(44, HIGH);
        digitalWrite(45, 0);
        break;
      case 6:
        Serial.print("6");
        digitalWrite(42, 0);
        digitalWrite(41, HIGH);
        digitalWrite(44, HIGH);
        digitalWrite(45, 0);
        break;
      case 7:
        Serial.print("7");
        digitalWrite(42, HIGH);
        digitalWrite(41, HIGH);
        digitalWrite(44, HIGH);
        digitalWrite(45, 0);
        break;
      case 8:
        Serial.print("8");
        digitalWrite(42, 0);
        digitalWrite(41, 0);
        digitalWrite(44, 0);
        digitalWrite(45, HIGH);
        break;
      case 9:
        Serial.print("9");
        digitalWrite(42, HIGH);
        digitalWrite(41, 0);
        digitalWrite(44, 0);
        digitalWrite(45, HIGH);
        break;
      case 0:
        Serial.print("0");
        digitalWrite(42, 0);
        digitalWrite(41, 0);
        digitalWrite(44, 0);
        digitalWrite(45, 0);
        break;
    }

    Serial.print(":");

    // Turn on and off the correct lights corresponding to the binary code of the current time for the tens of the minutes.
    switch (CurMin10) {
      case 1:
        Serial.print("1");
        digitalWrite(37, HIGH);
        digitalWrite(38, 0);
        digitalWrite(39, 0);
        break;
      case 2:
        Serial.print("2");
        digitalWrite(37, 0);
        digitalWrite(38, HIGH);
        digitalWrite(39, 0);
        break;
      case 3:
        Serial.print("3");
        digitalWrite(37, HIGH);
        digitalWrite(38, HIGH);
        digitalWrite(39, 0);
        break;
      case 4:
        Serial.print("4");
        digitalWrite(37, 0);
        digitalWrite(38, 0);
        digitalWrite(39, HIGH);
        break;
      case 5:
        Serial.print("5");
        digitalWrite(37, HIGH);
        digitalWrite(38, 0);
        digitalWrite(39, HIGH);
        break;
      case 0:
        Serial.print("0");
        digitalWrite(37, 0);
        digitalWrite(38, 0);
        digitalWrite(39, 0);
        break;
    }

    // Turn on and off the correct lights corresponding to the binary code of the current time for the ones of the minutes.
    switch (CurMin1) {
      case 1:
        Serial.print("1");
        digitalWrite(33, HIGH);
        digitalWrite(34, 0);
        digitalWrite(35, 0);
        digitalWrite(36, 0);
        break;
      case 2:
        Serial.print("2");
        digitalWrite(33, 0);
        digitalWrite(34, HIGH);
        digitalWrite(35, 0);
        digitalWrite(36, 0);
        break;
      case 3:
        Serial.print("3");
        digitalWrite(33, HIGH);
        digitalWrite(34, HIGH);
        digitalWrite(35, 0);
        digitalWrite(36, 0);
        break;
      case 4:
        Serial.print("4");
        digitalWrite(33, 0);
        digitalWrite(34, 0);
        digitalWrite(35, HIGH);
        digitalWrite(36, 0);
        break;
      case 5:
        Serial.print("5");
        digitalWrite(33, HIGH);
        digitalWrite(34, 0);
        digitalWrite(35, HIGH);
        digitalWrite(36, 0);
        break;
      case 6:
        Serial.print("6");
        digitalWrite(33, 0);
        digitalWrite(34, HIGH);
        digitalWrite(35, HIGH);
        digitalWrite(36, 0);
        break;
      case 7:
        Serial.print("7");
        digitalWrite(33, HIGH);
        digitalWrite(34, HIGH);
        digitalWrite(35, HIGH);
        digitalWrite(36, 0);
        break;
      case 8:
        Serial.print("8");
        digitalWrite(33, 0);
        digitalWrite(34, 0);
        digitalWrite(35, 0);
        digitalWrite(36, HIGH);
        break;
      case 9:
        Serial.print("9");
        digitalWrite(33, HIGH);
        digitalWrite(34, 0);
        digitalWrite(35, 0);
        digitalWrite(36, HIGH);
        break;
      case 0:
        Serial.print("0");
        digitalWrite(33, 0);
        digitalWrite(34, 0);
        digitalWrite(35, 0);
        digitalWrite(36, 0);
        break;
    }

    Serial.print(":");

    // Turn on and off the correct lights corresponding to the binary code of the current time for the tens of the seconds.
    switch (CurSec10) {
      case 1:
        Serial.print("1");
        digitalWrite(26, HIGH);
        digitalWrite(27, 0);
        digitalWrite(28, 0);
        break;
      case 2:
        Serial.print("2");
        digitalWrite(26, 0);
        digitalWrite(27, HIGH);
        digitalWrite(28, 0);
        break;
      case 3:
        Serial.print("3");
        digitalWrite(26, HIGH);
        digitalWrite(27, HIGH);
        digitalWrite(28, 0);
        break;
      case 4:
        Serial.print("4");
        digitalWrite(26, 0);
        digitalWrite(27, 0);
        digitalWrite(28, HIGH);
        break;
      case 5:
        Serial.print("5");
        digitalWrite(26, HIGH);
        digitalWrite(27, 0);
        digitalWrite(28, HIGH);
        break;
      case 0:
        Serial.print("0");
        digitalWrite(26, 0);
        digitalWrite(27, 0);
        digitalWrite(28, 0);
        break;
    }

    // Turn on and off the correct lights corresponding to the binary code of the current time for the ones of the seconds.
    switch (CurSec1) {
      case 1:
        Serial.print("1");
        digitalWrite(22, HIGH);
        digitalWrite(23, 0);
        digitalWrite(24, 0);
        digitalWrite(25, 0);
        break;
      case 2:
        Serial.print("2");
        digitalWrite(22, 0);
        digitalWrite(23, HIGH);
        digitalWrite(24, 0);
        digitalWrite(25, 0);
        break;
      case 3:
        Serial.print("3");
        digitalWrite(22, HIGH);
        digitalWrite(23, HIGH);
        digitalWrite(24, 0);
        digitalWrite(25, 0);
        break;
      case 4:
        Serial.print("4");
        digitalWrite(22, 0);
        digitalWrite(23, 0);
        digitalWrite(24, HIGH);
        digitalWrite(25, 0);
        break;
      case 5:
        Serial.print("5");
        digitalWrite(22, HIGH);
        digitalWrite(23, 0);
        digitalWrite(24, HIGH);
        digitalWrite(25, 0);
        break;
      case 6:
        Serial.print("6");
        digitalWrite(22, 0);
        digitalWrite(23, HIGH);
        digitalWrite(24, HIGH);
        digitalWrite(25, 0);
        break;
      case 7:
        Serial.print("7");
        digitalWrite(22, HIGH);
        digitalWrite(23, HIGH);
        digitalWrite(24, HIGH);
        digitalWrite(25, 0);
        break;
      case 8:
        Serial.print("8");
        digitalWrite(22, 0);
        digitalWrite(23, 0);
        digitalWrite(24, 0);
        digitalWrite(25, HIGH);
        break;
      case 9:
        Serial.print("9");
        digitalWrite(22, HIGH);
        digitalWrite(23, 0);
        digitalWrite(24, 0);
        digitalWrite(25, HIGH);
        break;
      case 0:
        Serial.print("0");
        digitalWrite(22, 0);
        digitalWrite(23, 0);
        digitalWrite(24, 0);
        digitalWrite(25, 0);
        break;
    }


    // If the time passes 12 hours in non-military time, toggle AM or PM.
    if (militaryTime == false and ampm == true) {
      digitalWrite(50, HIGH);
      digitalWrite(51, 0);
      Serial.println(" AM");
    }
    else if (militaryTime == false and ampm == false) {
      digitalWrite(50, 0);
      digitalWrite(51, HIGH);
      Serial.println(" PM");
    }
    else {
      digitalWrite(50, 0);
      digitalWrite(51, 0);
      Serial.println("");
    }

    if (alarmOn == true and CurHrs10 == AlarmHrs10 and CurHrs1 == AlarmHrs1 and CurMin10 == AlarmMin10 and CurMin1 == AlarmMin1 and cooldownAB == 0) {
      alarmBeep = true;
      cooldownAB = 6000;
    }
    if (alarmBeep == true) {
      Serial.println("Wake up!");
    }


    //[Keanu] if the cooldown is active it will decrease by one every loop until it reaches 0.
    if (cooldownAO > 0) {
      cooldownAO--;
    }
    if (cooldownAB > 0) {
      cooldownAB--;
    }
    if (cooldownTS > 0) {
      cooldownTS--;
    }
    if (cooldownMT > 0) {
      cooldownMT--;
    }
    if (cooldownDST > 0) {
      cooldownDST--;
    }
    if (cooldownAP > 0) {
      cooldownAP--;
    }

    delay(10);
  }
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

void MilitaryRules() {

  if (pulseMT == true and ampm == true and CurHrs10 == 1 and CurHrs1 == 2) {
    CurHrs10 = 0;
    CurHrs1 = 0;
    pulseMT = false;
  }
  else if (pulseMT == true and ampm == false and CurHrs10 == 1 and CurHrs1 == 2) {
    CurHrs10 = 1;
    CurHrs1 = 2;
    pulseMT = false;
  }
  // If the typography of the non-military time was PM, add 12 hours.
  else if (pulseMT == true and ampm == false) {
    CurHrs10 = CurHrs10 + 1;
    CurHrs1 = CurHrs1 + 2;
    pulseMT = false;
  }
  else {
    pulseMT = false;
  }

  // If 10 seconds have passed, reset to 0 seconds and add 1 to the tens.
  if (CurSec1 == 10) {
    CurSec1 = 0;
    CurSec10++;
  }
  // If 60 seconds have passed, reset to 0 seconds and add 1 to the minutes.
  if (CurSec10 == 6) {
    CurSec10 = 0;
    CurMin1++;
  }
  // If 10 minutes have passed, reset to 0 minutes and add 1 to the tens.
  if (CurMin1 == 10) {
    CurMin1 = 0;
    CurMin10++;
  }
  // If 60 minutes have passed, reset to 0 minutes and add 1 to the hours.
  if (CurMin10 == 6) {
    CurMin10 = 0;
    CurHrs1++;
  }
  // If 10 hours have passed, reset to 0 hours and add 1 to the tens.
  if (CurHrs1 == 10) {
    CurHrs1 = 0;
    CurHrs10++;
  }
  // If 24 hours have passed, reset to 0 hours.
  if (CurHrs10 == 2 and CurHrs1 == 4) {
    CurHrs10 = 0;
    CurHrs1 = 0;
  }
}

void nonMilitaryRules() {

  if (pulseMT == true) {
    // If the time was 00:00, set time to 12 and set the typography to AM.
    if (CurHrs10 == 0 and CurHrs1 == 0) {
      CurHrs10 = 1;
      CurHrs1 = 2;
      ampm = true;
    }
    // If the time was already past 13, subtract 12 hours and set the typography to PM.
    else if ((CurHrs10 == 1 and CurHrs1 >= 3) or (CurHrs10 == 2 and CurHrs1 >= 2)) {
      CurHrs10 = CurHrs10 - 1;
      CurHrs1 = CurHrs1 - 2;
      ampm = false;
    }
    // If the time was 20, set time to 8 and set the typography to PM.
    else if (CurHrs10 == 2 and CurHrs1 == 0) {
      CurHrs10 = 0;
      CurHrs1 = 8;
      ampm = false;
    }
    // If the time was 21, set time to 9 and set the typography to PM.
    else if (CurHrs10 == 2 and CurHrs1 == 1) {
      CurHrs10 = 0;
      CurHrs1 = 9;
      ampm = false;
    }
    // If the time was 11:00 or below, don't subtract anything and set the typography to AM.
    else if (CurHrs10 <= 1 and CurHrs1 <= 1) {
      ampm = true;
    }
    // If the time was 12:00, don't subtract anything and set the typography to PM.
    else if (CurHrs10 == 1 and CurHrs1 == 2) {
      ampm = false;
    }
    pulseMT = false;
  }

  // If 10 seconds have passed, reset to 0 seconds and add 1 to the tens.
  if (CurSec1 == 10) {
    CurSec1 = 0;
    CurSec10++;
  }
  // If 60 seconds have passed, reset to 0 seconds and add 1 to the minutes.
  if (CurSec10 == 6) {
    CurSec10 = 0;
    CurMin1++;
  }
  // If 10 minutes have passed, reset to 0 minutes and add 1 to the tens.
  if (CurMin1 == 10) {
    CurMin1 = 0;
    CurMin10++;
  }
  // If 60 minutes have passed, reset to 0 minutes and add 1 to the hours.
  if (CurMin10 == 6) {
    CurMin10 = 0;
    CurHrs1++;
  }
  // If 10 hours have passed, reset to 0 hours and add 1 to the tens.
  if (CurHrs1 == 10) {
    CurHrs1 = 0;
    CurHrs10++;
  }
  // If hours reaches 13, reset to 1.
  if (CurHrs10 == 1 and CurHrs1 == 3) {
    CurHrs10 = 0;
    CurHrs1 = 1;
  }
  // If 12 hours have passed, toggle AM/PM LED.
  if (CurHrs10 == 1 and CurHrs1 == 2 and CurMin10 == 0 and CurMin1 == 0 and CurSec10 == 0 and CurSec1 == 0 and cooldownAP == 0) {
    ampm = !ampm;
    cooldownAP = 100;
  }
}

This is the code that should switch to our deseired "Time Set Mode":

if (digitalRead(21) == 0 and cooldownTS == 0) {
      Serial.println("this should work");
      TimeSet = !TimeSet;
      pulseTS = true;
      cooldownTS = 25;
    }

Could You create and post the schematics? A block diagram showing the logic of the build would help.
Using a joystick for a clock sounds very strange...

Yikes. Your code is way over-complicated. First off, you should get into the habit of defining all your pins with human understandable names, not use just random numbers throughout your code. For example

const int joystickButtonPin = 21;
...
void setup() {
  pinMode(joystickButtonPin, INPUT_PULLUP);
  ...
}
void loop() {
  ...
  if ( digitalRead(joystickButtonPin) == LOW ) ...
  ...
}

Much, much more readable.

Second, your while() loops inside loop() are not necessary, let loop() do what is was designed for - looping. Those while() loops would then become if() statements. You are also packing a TON of things inside each of those loops. It would be better to break things down into smaller, specific functions.

void loop() {
  if ( TimeSet == true ) {
    SetTime();
  }
  else {
    SetAlarm();
  }
}

You would also be way better off dealing with time as seconds from midnight vs having all those individual variables for hours10, hours1, etc.

To answer your questions, yes I know the script is probably way over complicated. I am a new programmer and still have a lot of exercise and practise to do...
The joystick is used to set the alarm. For example, pressing the joystick turns on alarm-setting mode, moving up and down changes the digit of the selected variable up and down and left to right switches which hour or minute you're setting.
The reason that the variables have been split up into Hrs10, Hrs1, Min10, Min1 is because it's a binary clock. I figured it was easier to split up the time into different variables in order to convert and display them in binary.

I totally understand that this way of coding is much more readable, but does it fix the joystick problem? I am kind of desperate for the joystick to work and no so much for cleaning up the code. If it does work or is does help you with understanding I could most certainly clean up the whole code.

Last but not certainly not least, I did already have a Data Flow Diagram lying around. I will attach that as well as a new block diagram of the wiring of the hardware.


The point of cleaning the code is to make it easier to understand, for you especially, but also those trying to help you. It's much harder to build a mental model of what your code is doing as it stands than it would be with decent names and reduced complexity.

The data flow picture helps but the block diagram is not really useful. It needs to be an actual schematic showing ALL the wires. For example, do those LEDs have current limiting resistors? How are the buttons wired up. A joystick typically has 5 connectors - how are they wired up?

The problem is that there are 23 LEDs, it would be quite some overkill to draw every single one of them. They are all connected to GND and each LED has its own digital output pin. They also each have their own 150 ohm resistor connected in between the LED and the GND. However, I don't think that the wiring of the LEDs matters that much.
The joystick wiring, I understand, is crucial. I will add another drawing for those wires for you right now.
WhatsApp Image 2022-02-07 at 11.09.12 AM

Fantastic. If you look at the pinout for a Mega 2560, you will notice that D21 is also SCL for I2C communication so you can not use that pin and use i2c to talk to your DS3231 module. Move your joystick button to another pin.

Thank you so much! that actually helps alot. What pin do you recommend? any other pins I need to look out for?

(edit)
Nevermind pin 2 seems to work fine! Again, thank you so much it honestly means the world to me.