How to check the time a user is allowed to click - memory game

Hi everyone, I came to this forum because I have a question about my project. I have to make a memory game for school and this has worked out quite well. But now also want to add time. The maximum time a user has to push a sequence. When this time has elapsed --> GAME OVER. I've already tried to work with the mills function myself. But now I've noticed that my function Get_sequence actually stops my Mills function. I think this is because of my Get_Sequence function and the while in it. Now, I've tried to get that mils function in the Get_sequence function. And i put them in other places, but right now I'm a bit stuck. If anyone knows a solution to this problem, I'd love to hear it! Thank you so much Barballon

(Arduino code and C++ is also totally new to me, so if the solution is simple don't attack me please XD :grinning:)

// Define pins
byte pinnen[] = {8, 9, 10, 11};
byte knoppen[] = {4, 5, 6, 7};

// Sound
byte buzzer = 3;

// Pinnen
byte knop_geel = 4;
byte knop_groen = 5;
byte knop_rood = 6;
byte knop_blauw = 7;

// Levels
const int MAX_LEVEL = 100;
int patroon[MAX_LEVEL];
int ingevuld_patroon[MAX_LEVEL];
// Current level level
int level = 1;
int flag = 0;

// Wait variabels
bool wacht = true;
bool instellingen_bool = true;
int snelheid = 2000;

long startWaitMillis;      //some global variables available anywhere in the program
long period = 5000; //the value is a number of milliseconds

int test;

void setup()
{
  // PINMODE
  // Buttons --> Input pullup
  for (byte i = 0; i < 4; i++)
  {
    pinMode(knoppen[i], INPUT_PULLUP);
  }
  // Pinnen / Leds --> Output
  for (byte i = 0; i < 4; i++)
  {
    pinMode(pinnen[i], OUTPUT);
  }
  // Pinmode buzzer
  pinMode(buzzer, OUTPUT);

  // Initieer Mils()

  // Flash function --> Parameter --> Aantal keer
  flash(1);

  // ALLE PINNNE IN SETUP LOW ZETTEN
  // Alle pinnen op low zetten --> Parameter --> LOW/HIGH/WAARDE
  writeAllPins(LOW);
  // Buzzer low
  digitalWrite(buzzer, LOW);

  // Serial
  Serial.begin(9600);
  Serial.println("GAME READY");
  Serial.println("========================================================== \n Hoe moeilijk wil je de game maken?\n Geef een cijfer in tussen 1 - 3\n 1 --> Makelijk\n 1 --> Medium\n 3 --> Hard\n==========================================================");
}
void loop()
{
  if (instellingen_bool == true)
  {
    instellingen();
  }

  if (wacht == true && instellingen_bool == false)
  {

    //genereer een patroon;
    generate_sequence();

    // Sequence showen aan de gebruiker
    show_sequence();
    startWaitMillis = millis();
    Serial.println("startWaitMillis:" + String(startWaitMillis));

    // wacht op false zetten omdat arduino code heeft uitgevoerd
    wacht = false;
  }
  // Speler doet dingen
  else if (wacht == false)
  {


    // User plays game.
    Serial.println("Tussentijds" + String(millis() - startWaitMillis));
    if (millis() - startWaitMillis > period)
    {
      Serial.println(millis() - startWaitMillis);
      Serial.println("Ok passed");
      wrong_sequence();
      wacht = true;
    }

    get_sequence();

  }
}

// ============================
// FUNCTIES
// ============================

// Instellingen
void instellingen()
{
  if (Serial.available() > 0)
  {
    String inKomend = Serial.readString();
    // Comando's
    if (inKomend == "1")
    {
      Serial.println("==========================================================\n Gekozen mode --> Makelijk");
      snelheid = 2000;
      Serial.println("LET OP SPEL GAAT BEGINT NA HET FLIKKEREN VAN LEDS! \n==========================================================");
      flash(3);
      instellingen_bool = false;
    }
    else if (inKomend == "2")
    {
      Serial.println("==========================================================\n Gekozen mode --> Medium");
      snelheid = 1334;
      Serial.println("LET OP SPEL GAAT BEGINT NA HET FLIKKEREN VAN LEDS! \n==========================================================");
      flash(3);
      instellingen_bool = false;
    }
    else if (inKomend == "3")
    {
      Serial.println("==========================================================\n Gekozen mode --> Moeilijk");
      snelheid = 668;
      Serial.println("LET OP SPEL GAAT BEGINT NA HET FLIKKEREN VAN LEDS! \n==========================================================");
      flash(3);
      instellingen_bool = false;
    }
  }
}

// Sequence showen
void show_sequence()
{
  // Alle pinnen op low zetten --> Parameter --> LOW/HIGH/WAARDE
  writeAllPins(LOW);
  for (int i = 0; i < level; i++)
  {
    digitalWrite(patroon[i], HIGH);
    delay(snelheid);
    digitalWrite(patroon[i], LOW);
  }
}

// Sequence controleren
void get_sequence()
{
  for (int i = 0; i < level; i++)
  {
    flag = 0;
    while (flag == 0)
    {
      if (digitalRead(4) == LOW)
      {
        digitalWrite(8, HIGH);
        ingevuld_patroon[i] = 8;
        flag = 1;
        delay(200);
        if (ingevuld_patroon[i] != patroon[i])
        {
          wrong_sequence();
          return;
        }
        digitalWrite(8, LOW);
      }

      if (digitalRead(5) == LOW)
      {
        digitalWrite(9, HIGH);
        ingevuld_patroon[i] = 9;
        flag = 1;
        delay(200);
        if (ingevuld_patroon[i] != patroon[i])
        {
          wrong_sequence();
          return;
        }
        digitalWrite(9, LOW);
      }

      if (digitalRead(6) == LOW)
      {
        digitalWrite(10, HIGH);
        ingevuld_patroon[i] = 10;
        flag = 1;
        delay(200);
        if (ingevuld_patroon[i] != patroon[i])
        {
          wrong_sequence();
          return;
        }
        digitalWrite(10, LOW);
      }

      if (digitalRead(7) == LOW)
      {
        digitalWrite(11, HIGH);
        ingevuld_patroon[i] = 11;
        flag = 1;
        delay(200);
        if (ingevuld_patroon[i] != patroon[i])
        {
          wrong_sequence();
          return;
        }
        digitalWrite(11, LOW);
      }

    }
  }
  right_sequence();
  wacht = true;
}

String juiste_volgorde_to_string()
{
  String patroon_string = "";
  for (int i = 0; i < level; i++)
  {
    if (patroon[i] == 8)
    {
      patroon_string = patroon_string + "geel";
    }
    if (patroon[i] == 9)
    {
      patroon_string = patroon_string + "groen";
    }
    if (patroon[i] == 10)
    {
      patroon_string = patroon_string + "rood";
    }
    if (patroon[i] == 11)
    {
      patroon_string = patroon_string + "blauw";
    }
  }
  return patroon_string;
}

// Sequence genereren
void generate_sequence()
{
  // Genereer patroon
  randomSeed(millis());
  for (int i = 0; i < MAX_LEVEL; i++)
  {
    patroon[i] = random(8, 12);
  }
}

// Code voor verkeerde volgorde
void wrong_sequence()
{
  flash(3);
  level = 1;
  instellingen_bool = true;
  wacht = true;
  Serial.println("========================================================== \n Helaas Je GAME OVER! Om opnieuw te starten geef je een cijfer tussen 1-3 in\nGeef een cijfer in tussen 1 - 3\n 1 --> Makelijk\n 1 --> Medium\n 3 --> Hard\n==========================================================");
}

// Code voor de juiste volgorde
void right_sequence()
{
  flash(2);
  if (level < MAX_LEVEL)
  {
    level++;
  }
  snelheid -= 50; // Verminderd wachttijd tussen leds
}

// Lichten laten flikkeren functie
void flash(byte aantalkeer)
{
  for (int i = 0; i < aantalkeer; i++)
  {
    writeAllPins(HIGH);
    delay(500);
    writeAllPins(LOW);
    delay(500);
    writeAllPins(LOW);
  }
}

void wrong_sequence_sound()
{
  tone(buzzer, 277);
  delay(500);
  noTone(buzzer);
  delay(500);
  noTone(buzzer);
}

// Alle pins op zelvde output/input zetten
void writeAllPins(byte waarde)
{
  for (byte i = 0; i < 4; i++)
  {
    digitalWrite(pinnen[i], waarde);
  }
}

I have now found the solution to my problem myself. For all those who still have the problem in the future, here I have found the solution in my case. I just tweaked my Get_Sequence function a bit.

// Sequence controleren
void get_sequence()
{
  for (int i = 0; i < level; i++)
  {
    flag = 0;
    while (flag == 0)
    {
      Serial.println("Hier loopt hij wel");
      Serial.println("Tussentijds: " + String(millis() - startWaitMillis));
      if (millis() - startWaitMillis > period)
      {
        Serial.println("Ok out of time");
        wrong_sequence();
        return;
      }

      if (digitalRead(4) == LOW)
      {
        digitalWrite(8, HIGH);
        ingevuld_patroon[i] = 8;
        flag = 1;
        delay(200);
        if (ingevuld_patroon[i] != patroon[i])
        {
          wrong_sequence();
          return;
        }
        digitalWrite(8, LOW);
      }

      if (digitalRead(5) == LOW)
      {
        digitalWrite(9, HIGH);
        ingevuld_patroon[i] = 9;
        flag = 1;
        delay(200);
        if (ingevuld_patroon[i] != patroon[i])
        {
          wrong_sequence();
          return;
        }
        digitalWrite(9, LOW);
      }

      if (digitalRead(6) == LOW)
      {
        digitalWrite(10, HIGH);
        ingevuld_patroon[i] = 10;
        flag = 1;
        delay(200);
        if (ingevuld_patroon[i] != patroon[i])
        {
          wrong_sequence();
          return;
        }
        digitalWrite(10, LOW);
      }

      if (digitalRead(7) == LOW)
      {
        digitalWrite(11, HIGH);
        ingevuld_patroon[i] = 11;
        flag = 1;
        delay(200);
        if (ingevuld_patroon[i] != patroon[i])
        {
          wrong_sequence();
          return;
        }
        digitalWrite(11, LOW);
      }

    }
  }
  right_sequence();
  wacht = true;
}