如何检查允许用户点击的时间 - 记忆游戏 Arduino

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

我来到这个论坛是因为我对我的项目有疑问。我必须为学校制作一个记忆游戏,结果非常好。不过现在还想补充时间。用户必须推送序列的最长时间。这段时间过去后 --> GAME OVER。我已经尝试过自己使用工厂功能。但现在我注意到我的函数 Get_sequence 实际上停止了我的 Mills 函数。我认为这是因为我的 Get_Sequence 函数和其中的 while。现在,我试图在 Get_sequence 函数中获取该 mils 函数。我把它们放在其他地方,但现在我有点卡住了。如果有人知道这个问题的解决方案,我很想听听!非常感谢 Barballon

(Arduino 代码和 C++ 对我来说也是全新的,所以如果解决方案很简单,请不要攻击我 XD。

// 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);
  }
}

串行输出:

我现在自己找到了解决问题的方法。对于所有那些将来仍然有问题的人,在这里我找到了我的案例的解决方案。我只是稍微调整了我的 Get_Sequence 函数。

// 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;
}