C ++: Simon Say Memory Game с использованием Arduino Uno - PullRequest
0 голосов
/ 24 октября 2019

Я пишу код для игры «Память говорит Саймон» с использованием Arduino Uno. Предполагается иметь 4 индикатора мигающих 10 случайных последовательностей, и затем пользователь может нажать кнопку, чтобы следовать последовательностям. У меня возникают трудности при написании функции для waittoStart (), но она выдает ошибку определения функции.

int button1 = A0; // buttons blue
int button2 = A1; // press button
int button3 = A2; // press button
int button4 = A3; // pressbutton

int BUZZER = 10; // speaker
int led12 = 2;   // corressponse w/button1
int led11 = 3;   // corressponse w/button2
int led9 = 4;    // corressponse w/button3
int led8 = 5;    // corressponse w/button4
int buttons[] = {A0, A1, A2, A3};
int leds[] = {5, 4, 3, 2};
int sequence[11];
int START = 0;   // flash led one at a time waiting for button
int PLAY = 1;    // shows sequences
int GAMEOVR = 2; // flash lights 3 times
int gametracker; // keeps track of what part of the game we are in
int waittostart; //
int showSequence;
int readSequence;
int blinkALL;
int buttonpressed;
// int speed =300;
int largestseq = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  //  pinMode(Sbutton, INPUT_PULLUP);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(led12, OUTPUT);
  pinMode(led11, OUTPUT);
  pinMode(led9, OUTPUT);
  pinMode(led8, OUTPUT);
  {
    gametracker = START;
    randomSeed(analogRead(0)); // WITHOUT IT WILL PLAY THE SAME SEQUENCE
                               // EVERYTIME IT RUNS::read to give a different
                               // value generated by random
  }
}

void loop() {
  // make a code that gives 10 different sequences
  // if first sequence is correct goes to the next if not goes back to the start
  // use array to make them???
  // 3 LED blinks for wrong
  // look at tone code
  // led lab 3?? 4leds in chase squence for winner
  // use tone () and no tone()
  // use if to ask a question
  // connect a button to an led

  void waitToStart() {
    // GOES THRU ALL 4 LEDS WAITING FOR THE BUTTON TO BE PRESSED
    int buttonPressed = -1;
    allOff();

    for (int pin = 0; pin < 4; pin++) {
      if (buttonPressed == -1) {
        digitalWrite(leds[pin], HIGH);
        buttonPressed = waitForButton(800);
        // WAITS 800MS  AND TELLS YOU WHICH BUTTON IS PRESSED AFTER IF NO BUTTON
        // IS PRESSED AT END THEN BUTTON PRESSED IS -1
        digitalWrite(leds[pin], LOW);
      }
    }

    // MEANING 0,1,2,OR 3 THEN START THE GAME  WAITS 2 SECS
    if (buttonPressed != -1) {
      // A button was pushed so wait then start playing.
      delay(2000);
      largestIndex = 0; // Restart IF USER WAS ALREADY PLAYING
      gameState = PLAY;
    }
  }

  if (gametracker == START) {
    waittostart();
  } else if (gametracker == PLAY) {
    * / Serial.println("Start");
    showSequence; // if correct show next squence
    readSequence;
  } else if (gametracker == GAMEOVR) {
    // blinks lights 3 times
    Serial.println("Gameover");
    blinkALL;            // blinkall(3)
    gametracker = START; // reset
  }

  if (allOff) {
    // COMMAND OUTPUT
    digitalWrite(led12, LOW);
    digitalWrite(led11, LOW);
    digitalWrite(led9, LOW);
    digitalWrite(led8, LOW);
  }

  void showSequence() {
    // PART SHOW made to show the different levels to challenge user

    sequence[largestIndex] = random(0, 4);
    // 0-3 random sequence decinding which leds to light AKA
    // GOES THRU SEQUENCE ARRAY & SHOWS WHICH LED ARE IN IT
    largestIndex++; // LARGEST INDEX STARTS AS 0

    for (int index = 0; index < largestIndex; index++) {
      delay(300); // NOT READING BUTTONS HERE TIME TO PUSH BUTTONS//FAST DELAY
                  // SO IF SAME COLOR IS PICKED YOU CAN TELL
      digitalWrite(leds[sequence[index]], HIGH);
      // FIRST LED ACCORDING TO SEQUENCE
      delay(700);
      digitalWrite(leds[sequence[index]], LOW);
    }

    void readSequence() {
      // PART READ made to decide weather the user was correct or not

      int positionPressed;
      boolean madeMistake =
          false; // bc you could either be right or wrong =boolean

      for (int index = 0; index < largestIndex & madeMistake == false;
           index++) {
        Serial.println("");
        Serial.println("Should push");
        Serial.println(sequence[index]);

        positionPressed = waitForButton(1000); // 0, 1, 2, or 3

        Serial.println("Pressed");
        Serial.println(positionPressed);
        if (positionPressed == -1 | positionPressed != sequence[index]) {
          madeMistake = true; // Exit the loop.
          gameState = GAMEOVER;
        }
      }
    }
...